invoke-command
远程执行命令:
invoke-command -computername $server -credential $cred -scriptblock{param($server,$username,$serverpass,$starttime,$startdate)
$hotfix_setup = schtasks /query |select-string "hotfix_setup" -quiet
if ($hotfix_setup -eq "true")
{schtasks /delete /tn "hotfix_setup" /f |out-null}
schtasks /create /tn "hotfix_setup" /sc once /ru $username /rp $serverpass /st $starttime /sd $startdate /tr d:\hotfix\hotfix_win2003\2014-04\hotfix_setup.bat
} -argumentlist $server,$username,$serverpass,$starttime,$startdate
远程执行脚本(脚本位于本地计算机,非远程):
invoke-command -computername $servername -credential $cred -filepath $script_path -argumentlist $servername,$serverpass |out-file $task_result -append
========================================================
invoke-command -computer remotecomputer 脚本中的变量执行结果不会返回到本地计算机。如果在脚本块中直接让结果显示在控制台,则可以显示。
取在远程计算机执行结果到本地
$username = "administrator"
$serverpass = "6019"
$server = "10.4.19.60"
$password = convertto-securestring $serverpass -asplaintext –force
$cred = new-object system.management.automation.pscredential($username,$password)
$abc = invoke-command -computername $server -credential $cred -scriptblock {
hostname
gwmi win32_operatingsystem
}
$abc[1].caption
invoke-command -asjob [<switchparameter>]
在远程计算机上将命令作为后台作业运行。使用此参数可运行需要较长时间才能完成的命令。
asjob 参数类似于使用 invoke-command 远程运行 start-job 命令。但是,对于 asjob,作业是在本地计算机上创建的,即使作业运行在远程计算机上也是如此,而且远程作业的结果会自动返回本地计算机。
$jobname = "jobupdatecheck"
$null = start-job -name $jobname -scriptblock ${function:tasksch} -argumentlist $taskcheckname,$username,$userpass,$taskchecktime,$taskcheckdate,$taskcheckscriptpath
do {
start-sleep -milliseconds 500
$jobstate = (get-job -name $jobname).state
}
until ($jobstate -eq "completed")
receive-job -name $jobname
get-job -name $jobname |remove-job
上一篇: (13)ASP.NET Core 中的选项模式(Options)
下一篇: HTML5--Table