Encapsulate the output of invoke command in a variable - PowerShell

社会主义新天地 提交于 2021-01-27 19:23:43

问题


I have a script that installs Remote Desktop Services on remote machines (from the DC).

I'm now at the phase where I check if RDS installed on the connection broker (server) and connection host (server).

I want to use invoke-command since a remote powershell session seemed too complicated.

This is the code I have:

$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

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?

Thanks


回答1:


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


来源:https://stackoverflow.com/questions/42580034/encapsulate-the-output-of-invoke-command-in-a-variable-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!