locked
Using argumentlist in invoke-comand RRS feed

  • Question

  • $test = 'sxx'
    Invoke-Command -ComputerName ********* -ScriptBlock {
        icacls O:\hjemmeområde\$($args[0]).ssb /grant $($args[0])':(oi)(CI)(F)' /t 
        } -ArgumentList $test

    This will return

    Invalid parameter "sxx"
        + CategoryInfo          : NotSpecified: (Invalid parameter "sxx":String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
        + PSComputerName        : ***********

    Anyone know why? If i remove the variables and just type sxx manually it will run fine.

    Thursday, February 22, 2018 2:01 PM

All replies

  • Maybe the remote PC has an older version of PowerShell that won't automatically run native commands. Try this:

    $test = 'sxx'
    Invoke-Command -ComputerName ********* -ScriptBlock {
        Invoke-Expression "icacls O:\hjemmeområde\$($args[0]).ssb /grant $($args[0])':(oi)(CI)(F)' /t"
    } -ArgumentList $test

    If you're on PoSH 3 or greater you could also try using the remote variables instead of the Argumentlist:

    $test = 'sxx'
    Invoke-Command -ComputerName ********* -ScriptBlock {
        Invoke-Expression "icacls O:\hjemmeområde\$using:test.ssb /grant $using:test':(oi)(CI)(F)' /t"
    }
    If these options do not work try replacing Invoke-Expression with Write-Host to check if the command you're trying to run is what you'd expect.


    MicaH

    itmicah.wordpress.com





    • Edited by MicaH_Z Thursday, February 22, 2018 2:35 PM
    Thursday, February 22, 2018 2:17 PM
  • icacls "O:\hjemmeområde\$($args[0]).ssb" /grant "$($args[0]):(oi)(CI)(F)" /t


    \_(ツ)_/



    Thursday, February 22, 2018 5:13 PM