invoke-command 遠程執行命令: invoke-command -ComputerName $server -Credential $cred -ScriptBlock{param($server,$UserName,$serverpass,$starttime,$startdate)$ ...
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