[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[问题求助] 跪求高手解答PowerShell传参至new-PSSession

  1. function Connect_RemoteMachine ($username,$pwd,$ip_remote)
  2. {
  3.     $pwd=ConvertTo-SecureString $pwd -AsPlainText -Force
  4.     $cred=New-Object System.Management.Automation.PSCredential($username,$pwd)
  5.     $s=new-PSSession -ComputerName $ip_remote -Credential $cred
  6.     if($s -gt $null)
  7.     {
  8.         return $s
  9.     }
  10. }
  11. function reset-comupter
  12. {      
  13.      
  14.       
  15.        $a=1
  16.        $session = Connect_RemoteMachine "administrator"  "11111111" localhost
  17.        Invoke-Command  -Session $session -ScriptBlock {
  18.       
  19.         
  20.         $b=2
  21.         $c=$a+$b
  22.         Write-Output $c
  23.                     
  24.         
  25.         }-ArgumentList $a,$b,$c
  26.         Remove-PSSession $session;
  27.       
  28. }
  29. reset-comupter
复制代码
在以上代码中,$a无法在新的session中被调用,如果我想在新session中调用之前定义好的变量,应该怎么做呢?
1

评分人数

    • pcl_test: 勿发笼统无意义的标题PB -2

  1. $a = 1
  2. Invoke-Command -Session $session -ScriptBlock { param($n);$c=2+$n;Write-Output $c } -Args $a
复制代码

TOP

谢谢,通过你的指点,我搞定了

TOP

返回列表