powershell - 将 invoke 命令的输出封装在一个变量中 - PowerShell

标签 powershell variables scripting invoke-command storing-data

我有一个脚本可以在远程机器上(从 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/

相关文章:

c++ - 从从文本文件导入的不同线型创建变量

linux - 逐行读取文本文件并检查该行是否包含特定字符串

python - 如何通过CMD安装python 2.7

Powershell 在文件中 move 行

php - 关于 PHP 中的作用域和 OOP

powershell - 通过 PowerShell 脚本跟踪 AD 中的更改

php - "Notice: Undefined variable"、 "Notice: Undefined index"、 "Warning: Undefined array key"和 "Notice: Undefined offset"使用 PHP

batch-file - 如何通过批处理文件在随机时间安排任务?

azure - 如何在powershell/azure中将格式输出到文件中

powershell - ExecutionPolicy错误后如何强制Powershell设置ERRORLEVEL变量