将 invoke 命令的输出封装在一个变量中 - PowerShell [英] Encapsulate the output of invoke command in a variable - PowerShell
问题描述
我有一个脚本可以在远程机器(来自 DC)上安装远程桌面服务.
I have a script that installs Remote Desktop Services on remote machines (from the DC).
我现在处于检查连接代理(服务器)和连接主机(服务器)上是否安装了 RDS 的阶段.
I'm now at the phase where I check if RDS installed on the connection broker (server) and connection host (server).
我想使用 invoke-command 因为远程 powershell 会话似乎太复杂了.
I want to use invoke-command since a remote powershell session seemed too complicated.
这是我的代码:
$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 中脚本块的输出封装在 DC 可以访问的变量中?如果 RDS 已成功安装或未能写入日志文件,我正在尝试写入
But my question is, how do I encapsulate the output of the scriptblock in the invoke-command in a variable that the DC can access? I'm trying to write away if RDS is succesfully installed or failed to a log-file
我们如何封装函数的输出并将其传递给运行它的机器?
How do we encapsulate the output of the function and pass it to the machine who's running it?
谢谢
推荐答案
通常对于 powershell 从命令\函数返回某些内容,您希望产生任何类型的输出.因此,代码中的bla-bla"会将bla-bla"返回给调用者.这样你的情况就简化了:
Generally for powershell to return something from a command\function, you want to produce any kind of output. So just "bla-bla" inside your code will return "bla-bla" to the caller. With that your case simplified:
$res = Invoke-Command -ComputerName "testpc.eil.local" -ScriptBlock {
(Get-WindowsFeature -Name "Remote-Desktop-Services").Installed
}
do something with $res here
这篇关于将 invoke 命令的输出封装在一个变量中 - PowerShell的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!