locked
PowerShell: Invoke-Command with arguments such as full path and keys RRS feed

  • Question

  • Hello All, 

    I am trying to invoke a command on a remote hosts. The command looks like this:  

    [string]$cmd  = $("'" + '"' + $execFile.FullName + '"' + ' -e '  +  ' -p ' + $password + ' -i ' + $instanceName + ' -f ' + '"' + $fileName + '"' + "'")
    
    Invoke-Command -ComputerName <HostNAme>-ScriptBlock { param($p1) Out-String -InputObject( echo "Y" |  cmd /c  $p1)} -args $cmd 

    When I try to gather a string I receive an error

    The filename, directory name, or volume label syntax is incorrect.
        + CategoryInfo          : NotSpecified: (The filename, d...x is incorrect.:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
        + PSComputerName        : <Hostname>

    In case when a command string is static everything works fine. 

    $cmd = '"C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn\RSKeyMgmt.exe" -e  -p <Passwrod>-i <Instance> -f "<filePath>"'

    I wounder if there is any work around. 

    Any ideas?


    Tuesday, December 17, 2019 1:34 AM

All replies

  • Your code doesn't make much sense but this is how to do this.

    $sb = {
        param(
            $instanceName,
            $password,
            $fileName
        )
        
        cd 'C:\Program Files (x86)\Microsoft SQL Server\120\Tools\Binn'
        'Y' | .\RSKeyMgmt.exe -e -p $password -i $instanceName -f $fileName
    }
    
    Invoke-Command -ComputerName '<HostNAme>' -ScriptBlock $sb -args $instanceName,$password,$fileName


    \_(ツ)_/


    • Edited by jrv Tuesday, December 17, 2019 3:55 AM
    Tuesday, December 17, 2019 3:50 AM
  • I am sorry it is my fault. I should have made it more clear. The whole idea is to pass a path as a variable. In my case, a path is not static.  I hope it makes more sense now.  

     
    Tuesday, December 17, 2019 1:18 PM
  • Eventually my code looks like this:

    $path = $execFile.Directory
    Invoke-Command -ComputerName $computer.Name -ScriptBlock { Set-Location $using:path ; echo 'Y' | .\RSKeyMgmt.exe -e -f $using:fileName -p $using:password -i $using:instanceName } -ArgumentList $path, $fileName, $password, $instanceName  -ErrorAction Stop

    Works in my case.

    Thanks a lot. 

       
    Tuesday, December 17, 2019 1:55 PM
  • I am sorry it is my fault. I should have made it more clear. The whole idea is to pass a path as a variable. In my case, a path is not static.  I hope it makes more sense now.  

     
    So just add the path as an argument and replace the string with the variable. 

    \_(ツ)_/

    Tuesday, December 17, 2019 3:26 PM