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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM