Rocho.J

人脑是不可靠的, 随时记录感悟并且经常重复!

 

[转]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:

  1. # initialize the credential   
  2. $account="account"  
  3. $password='4rfv$RFV'  
  4. $password  
  5. $secpwd= convertto-securestring $password -asplaintext -force  
  6. $cred=new-object -typename System.Management.Automation.PSCredential -argumentlist $account,$secpwd  
  7. $cred  
  8. # initialize the machine and the folder to be clean up   
  9. #$machine="192.168.0.116"   
  10. $machine="ccprnrtstgen570"  
  11. invoke-command -computername $machine -command {Remove-Item -path C:/zzz -force -recurse} -Credential $cred  

 

 

posted on 2013-05-24 12:07  RJ  阅读(683)  评论(0)    收藏  举报

导航