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

posted on 2014-05-08 15:12  momingliu11  阅读(4113)  评论(0编辑  收藏  举报