遠程執行命令:html
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
app
遠程執行腳本(腳本位於本地計算機,非遠程):post
invoke-command -ComputerName $servername -Credential $cred -FilePath $script_path -ArgumentList $servername,$serverpass |Out-File $task_result -appendui
========================================================url
invoke-command -computer remotecomputer 腳本中的變量執行結果不會返回到本地計算機。若是在腳本塊中直接讓結果顯示在控制檯,則能夠顯示。server
取在遠程計算機執行結果到本地htm
$UserName = "administrator"
$serverpass = "6019"blog
$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
}ip
$abc[1].captionrem
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,$TaskCheckScriptPathDo {Start-Sleep -Milliseconds 500$JobState = (Get-Job -Name $JobName).State}Until ($JobState -eq "Completed")Receive-Job -Name $JobNameGet-Job -Name $JobName |Remove-Job