远程执行命令: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