powershell的一个脚本,用来测试命令的

来源:互联网 发布:购买淘宝4心店铺 编辑:程序博客网 时间:2024/06/01 10:15
# $1: testcase script#   xxx.test: testcase script#   directory specified: run.ps1 for each *.test under directory#execute this script in windows powershell#: ${1:?Specify test-script-file}if (${args}.Count -eq 0){    echo "Specify test-script-file"    exit 2}$test_file=${args}[0]# set DST_SVR by hostnameif(-not ${MYHOSTNAME}){    $local:MYHOSTNAME    $local:DST_SVR    $local:SPEC_HOSTNAME    $local:OWN_PSID    $local:LOGFILE    $ini_result =$(select-string -path "C:\Win32app\AdvancedCopyManager\etc\swnode.ini" -pattern MyHostName=) -split "="    ${MYHOSTNAME}=${ini_result}[1]## get the second parameter    ${DST_SVR}=${args}[1]               if ( "${DST_SVR}" -ne "")    {        ${SPEC_HOSTNAME} = " -h ${DST_SVR}"    }    elseif($(select-string -path "C:\Win32app\AdvancedCopyManager\etc\swstg.ini" -pattern FJSVswssc=))     {        ${SPEC_HOSTNAME} = " -h ${MYHOSTNAME}"        ${DST_SVR}=${MYHOSTNAME}    }    else     {        ${DST_SVR}=${MYHOSTNAME}        ${SPEC_HOSTNAME} = ""    } }# if the first parameter is a directory, then execute all the file like "*.test"if($(test-path -PathType Container ${test_file})){    $file_arr = $(Get-ChildItem -Path ${test_file}\*.test -Name )    foreach($file in ${file_arr})    {        if (${file})        {            & $MyInvocation.MyCommand.Path ${test_file}\${file}        }    }    exit 0}# get the suffix name. "simplematch" is used for recognize "." ${EXT}= $("$test_file"  -split ".", 0, "simplematch")# "-1" is the last elementif( "${EXT}[-1]" -eq "gen"){    ${GENERATOR}="sh"}else{    ${GENERATOR}="cat"}# create a directory,create a file ${TMP}=".\tmprunner\testrunner.ps1"# test if the directory is existif (-not $(test-path -Path  ".\tmprunner\")){    & New-Item -Path ".\tmprunner" -ItemType "directory"}if($(test-path -Path  ${TMP})){    #clear the file content    & clear-content -Path ${TMP}}else{    & New-Item -Path ${TMP} -ItemType "file"}# 1. preprocess#  VAR_FILE(CONFIG) file name depend HostName: #  because, to protect destroy device-data if fixed conf-file name.(copied other system)# write the function into tmp file @'& dateecho "${test_file}"${NG_Count}=0${OK_Count}=0${VAR_FILE}="$(hostname)"+".vars"function setVars(){    $result=$(cat ${VAR_FILE})    foreach( $i in $result)    {        if (("$i" -ne " ") -and $($i  -match "^[^#]"))        {            ${varname}=$(${i} -split "=")[0]            ${varvalue}=$(${i} -split "=")[1]        }        & set-variable -name ${varname} -value ${varvalue} -scope "script"        echo "+ ${varname}=${varvalue} "     }   }# check each command after executed, the first parameter is  # the first parameter is this command# the second parameter is respect exit codefunction checkResult(){    echo "----------------------------------"    $command=$args[0]    echo "|  command:$command"    echo "|  exitcode: $LastExitCode"        if($LastExitCode -eq $args[1])    {        echo "|  result:OK"set-variable OK_Count ($OK_Count+1) -scope 1    }    else    {        echo "|  result:NG"set-variable NG_Count ($NG_Count+1) -scope 1    }    echo "----------------------------------"}# at the end of test log# print the sum nummber of result# no parameterfunction Show_Report(){$Total=${OK_Count}+${NG_Count}echo "---------Test Report---------"echo "total number: $Total"echo "OK number: ${OK_Count}"echo "NG number: ${NG_Count}"}setVars'@ >> ${TMP}${result}=$(cat ${test_file})foreach($i in ${result}){    if (("$i" -ne " ") -and $($i  -match "^[^#]"))    {if ($( ${i} |select-string -pattern "echo")){#echo "$i" >>${TMP}}elseif ( -not $( ${i} |select-string -pattern "checkResult")) {        echo "echo `"+ $i `" " >>${TMP}}echo "& $i" >>${TMP}    }}# 2. logging#  determine logfile name${result}=$("${test_file}" -split "\\")${result}[-1]=${result}[-1].split(".")[0]${result}[-1]=${result}[-1]+".log"${TESTCASE_LOG_NAME}=""foreach($i in ${result}){    if ($i -and ($i -ne '.') -and ($i -ne '..'))    {        ${TESTCASE_LOG_NAME}="${TESTCASE_LOG_NAME}"+"_${i}"    }}${HOSTNAME}=$(hostname)${LOGFILE}="..\logs\"+"${HOSTNAME}"+"${TESTCASE_LOG_NAME}"if( -not $(test-path -Path  ${TMP})){      & New-Item -Path ${LOGFILE} -ItemType "file"}# 3. executing& ${TMP} 2>&1 | tee-object -filepath ${LOGFILE}sleep 3# 3. delete tmp directoryif (test-path -path .\tmprunner){  & remove-item -path .\tmprunner -recurse}