Powershell 输入PSSession不返回变量
我正试图把每一台计算机都输入到我的列表中,并从中删除一个软件 当我尝试不使用foreach的代码时,代码运行良好。但当我尝试循环它时,它根本不起作用 有人做某事,它什么也不做。例如,我试着做:Powershell 输入PSSession不返回变量,powershell,Powershell,我正试图把每一台计算机都输入到我的列表中,并从中删除一个软件 当我尝试不使用foreach的代码时,代码运行良好。但当我尝试循环它时,它根本不起作用 有人做某事,它什么也不做。例如,我试着做: $product = Get-WmiObject -Class Win32_Product | Where-Object {$_.name -like "7-zip*"} 在下一行中,在屏幕上打印$product变量。但它总是空的 当我手动输入pssession并运行此命令时,它会返回一些信息 我做错
$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."
}
}
使用invoke命令
例如:
-Enter PSSession不用于脚本中。只供互动使用。啊,太好了!现在成功了!非常感谢你!
$ReturnValue = Invoke-Command -ComputerName $ComputerName -ScriptBlock{
#DoSomethingHere
}
Write-host $Returnvalue #Will print the output of the scriptblock