powershell的一个脚本,用来测试命令的
2012-12-19 17:47
260 查看
# $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 hostname if(-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 element if( "${EXT}[-1]" -eq "gen") { ${GENERATOR}="sh" } else { ${GENERATOR}="cat" } # create a directory,create a file ${TMP}=".\tmprunner\testrunner.ps1" # test if the directory is exist if (-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 @' & date echo "${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 code function 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 parameter function 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 directory if (test-path -path .\tmprunner) { & remove-item -path .\tmprunner -recurse }
相关文章推荐
- PSAttack:一个包含所有的渗透测试用例的攻击型Powershell脚本框架
- 记录一个测试用的PowerShell脚本
- secureCRT中实现一个脚本VBScript, 用来让连接到主机的测试板子一直run一条指令。
- 如何在shell脚本中,判断一个基本命令执行是否成功?
- SP2010和Windows PowerShell 2.0--PowerShell脚本方法和创建自定义命令
- 一个扩展时间段的dir命令的vbs脚本
- shell脚本-输入一个数字对应一个命令
- 一个测试用的VBS脚本样例
- 脚本和web页共用同一个文件测试
- 一个用来测试正确的尾调用的迷宫小游戏
- csc.exe命令,用来将一个 类文件 cs文件编译为DLL文件
- 测试网站页面网速的一个简单Python脚本
- 每天一个linux命令(62):sh命令 /Linux中执行shell脚本的4种方法总结
- 创建一个UI输入框,以向一个重置密码的powershell脚本输入参数
- 一个学习grep、tee、awk、cut命令的很好的示例脚本
- 测试一个域名DNS查询时间的shell脚本
- Zabbix(二):自定义脚本、远程命令、报警升级测试实例
- shell跑一个PHP脚本的简单命令
- 一个用来自动管理大容量表的Sql脚本
- 20行代码实现的一个CSS覆盖率测试脚本