2

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

Kahn Kah
  • 1,389
  • 7
  • 24
  • 49

1 Answers1

6

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
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
  • Thank you very much, it's weird that your method works but mine doesn't hehe! – Kahn Kah Mar 03 '17 at 13:38
  • 1
    just like I said, yours doesn't because you don't actually OUTPUT anything. You need to OUTPUT stuff to have something to capture – 4c74356b41 Mar 03 '17 at 13:40
  • Okay but I was wondering why I couldn't just write `Write-Host "Installed!"` When I included it in my first script. (if test) but if I understand you correctly, it's because it's gets printed in the 'window' of the remote server and not the one from the DC? – Kahn Kah Mar 03 '17 at 13:55
  • 1
    yeah, don't use `write-host`, generally speaking, its messed up, use `write-output` – 4c74356b41 Mar 03 '17 at 13:58
  • 1
    Thanks for the answer! I've understood it thanks to your explanation – Kahn Kah Mar 08 '17 at 10:09