locked
Pass "SwitchParameter" to function executed remotely RRS feed

  • Question

  • I am trying to run the Get-SpeculationControlSettings cmdlet on a remote machine.  This works fine:

    invoke-command -ComputerName SOMESERVER ${function:Get-SpeculationControlSettings}

    But if I want to use the Quiet mode, I cannot get it to accept that in the argument list.  Everything I have found indicates to include the -ArgumentList parameter with Invoke-Command to accomplish this.

    I have tried both of these, but they both fail fail:
    invoke-command -ComputerName SOMESERVER ${function:Get-SpeculationControlSettings} -ArgumentList 'Quiet'
    invoke-command -ComputerName SOMESERVER ${function:Get-SpeculationControlSettings} -ArgumentList '-Quiet'

    It looks like -Quiet is a SwitchParameter, so how does one pass a switchparameter argument to a remotely executed function?

    Thanks

    NK


    Tuesday, February 18, 2020 4:25 PM

Answers

  • I was making this harder than it needed to be.  I found I can just pipe it to a gridview, CSV, or some other format and it skips all the text at the top and gives me just the results I was looking for (it still shows all the text on the screen, but the piped results do not include it.)

    invoke-command -ComputerName SOMESERVER1, SOMESERVER2, SOMESERVER3 ${function:Get-SpeculationControlSettings} | Export-CSV results.csv -notypeinformation

    Thanks

    NK

    • Marked as answer by NJK-Work Wednesday, February 19, 2020 9:52 PM
    Wednesday, February 19, 2020 9:52 PM

All replies

  • Have you tried this?

    -Quiet:$true


    --- Rich Matheisen MCSE&I, Exchange Ex-MVP (16 years)

    Tuesday, February 18, 2020 4:39 PM
  • Hi,

    Thank you for the reply.  Yes, sorry I should have included that in my original post, but I that also that along with just $true, and the number 1.  And probably a few only things that I am forgetting.

    

    Tuesday, February 18, 2020 4:47 PM
  • Your syntax is very bad.  Her is how to call a function remotely

    $sb = {
        Get-SpeculationControlSettings -Quiet
    }
    invoke-command -ScriptBlock $sb -ComputerName SOMESERVER 


    \_(ツ)_/

    Tuesday, February 18, 2020 6:02 PM
  • To make it an argument do this:

    $sb = {
        Get-SpeculationControlSettings -Quiet $using:quiet
    }
    $quiet = $true
    invoke-command -ScriptBlock $sb -ComputerName SOMESERVER 


    \_(ツ)_/


    • Edited by jrv Tuesday, February 18, 2020 6:04 PM
    Tuesday, February 18, 2020 6:04 PM
  • This would be better and easier to understand.

    $sb = {
        param(
            [bool]$Quiet
        )
        Get-SpeculationControlSettings -Quiet:$Quiet
    }
    invoke-command -ScriptBlock $sb -ComputerName SOMESERVER -ArgumentList $true


    \_(ツ)_/

    Tuesday, February 18, 2020 6:06 PM
  • I am looking for a solution that does not require me putting the SpeculationControl module on every target machine, so hence the "function:" technique as provide by Mike Robbins:

    https://mikefrobbins.com/2018/01/04/using-powershell-to-check-remote-windows-systems-for-cve-2017-5754-meltdown-and-cve-2017-5715-spectre/

    Unfortunately, his example does not show how to pass the -quiet parameter.  So I posted the question here.

    NK

     
    Tuesday, February 18, 2020 7:40 PM
  • If you use the function body then you will not be able to pass arguments.  Modify the function to permanently use that switch and use that instead.  Copy, rename the function and add "Quiet" to teh end of the name then assign the switch value in the new function.

    You can also place the function in a file and call it like this:

    Invoke-Command -FilePath .\myscript.ps1 -ArgumentList $true

    In the file call the function with the passed argument.


    \_(ツ)_/

    Tuesday, February 18, 2020 7:48 PM
  • I was making this harder than it needed to be.  I found I can just pipe it to a gridview, CSV, or some other format and it skips all the text at the top and gives me just the results I was looking for (it still shows all the text on the screen, but the piped results do not include it.)

    invoke-command -ComputerName SOMESERVER1, SOMESERVER2, SOMESERVER3 ${function:Get-SpeculationControlSettings} | Export-CSV results.csv -notypeinformation

    Thanks

    NK

    • Marked as answer by NJK-Work Wednesday, February 19, 2020 9:52 PM
    Wednesday, February 19, 2020 9:52 PM