我有一个脚本可以在远程机器上(从 DC)安装远程桌面服务。
我现在正处于检查 RDS 是否安装在连接代理(服务器)和连接主机(服务器)上的阶段。
我想使用 invoke-command 因为远程 powershell session 似乎太复杂了。
这是我的代码:
$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
if((Get-WindowsFeature -Name "Remote-Desktop-Services").Installed -eq 1)
{
#i need this output (true or false or a string)
}
else
{
#i need this output (true or false or a string)
}
}
Write-Host $res
但我的问题是,如何将 invoke-command 中脚本 block 的输出封装在 DC 可以访问的变量中?如果 RDS 安装成功或失败,我将尝试写到日志文件中
我们如何封装函数的输出并将其传递给运行它的机器?
谢谢
最佳答案
通常,对于从命令\函数返回某些内容的 powershell,您希望生成任何类型的输出。因此,只要在您的代码中使用“bla-bla”,就会将“bla-bla”返回给调用者。这样你的案例就简化了:
$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
(Get-WindowsFeature -Name "Remote-Desktop-Services").Installed
}
do something with $res here
关于powershell - 将 invoke 命令的输出封装在一个变量中 - PowerShell,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42580034/