[转]Invoke-Command in PowerShell --- http://blog.csdn.net/writehappy/article/details/6531424
supported by PowerShell V2!!!!
$c=get-Credential
invoke-command -computername [computername] -command {Get-Date} -Credential $c
1, 目前知道,invoke-command是要在另一台台机器上去执行,如果是用PS在本机上执行,就会出现类似于递归的情况,导致执行失败。
比如:
+ CategoryInfo : OpenError: (:) [], PSRemotingTransportException + FullyQualifiedErrorId : PSSessionStateBroken
2, [computername]不能换成IP, 要用Hostname.
sample:
- # initialize the credential
- $account="account"
- $password='4rfv$RFV'
- $password
- $secpwd= convertto-securestring $password -asplaintext -force
- $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $account,$secpwd
- $cred
- # initialize the machine and the folder to be clean up
- #$machine="192.168.0.116"
- $machine="ccprnrtstgen570"
- invoke-command -computername $machine -command {Remove-Item -path C:/zzz -force -recurse} -Credential $cred