【问题标题】:Enter-PSSession not returning variablesEnter-PSSession 不返回变量
【发布时间】:2017-08-25 21:13:20
【问题描述】:

我正在尝试输入我拥有的列表中的每台计算机并从中删除软件。

当我尝试代码(不使用 foreach)时,代码运行良好。但是当我尝试循环它时,它根本不起作用。

只要有“做某事”,它就什么都不做。例如,我尝试这样做:

$product = Get-WmiObject -Class Win32_Product | Where-Object {$_.name -like "7-zip*"} 

在下一行,在屏幕上打印$product 变量。但它总是空的。

当我手动输入 pssession 并运行此命令时,它会返回一些内容。

我在这里做错了什么?我已经浪费了几个小时试图弄清楚这一点,但没有运气:(

foreach ($computer in $computers){

    if(Test-Connection "$computer" -Count 1 -ErrorAction SilentlyContinue){

        Enter-PSSession -ComputerName $computer

        "do something"

        Exit-PSSession

    }else{

        Write-Warning "computer is offline."

    }
}

【问题讨论】:

标签: powershell


【解决方案1】:

使用调用命令 示例:

$ReturnValue = Invoke-Command -ComputerName $ComputerName -ScriptBlock{
    #DoSomethingHere
}
Write-host $Returnvalue #Will print the output of the scriptblock

【讨论】:

  • 啊,太好了!它现在起作用了!非常感谢!
猜你喜欢
  • 2014-04-18
  • 1970-01-01
  • 2019-01-09
  • 2015-11-11
  • 1970-01-01
  • 2022-12-31
  • 1970-01-01
  • 1970-01-01
  • 2020-04-03
相关资源
最近更新 更多