Search code examples
powershellremote-execution

how to pass parameters to Powershell script and run it on a remote computer


I am trying to pass 2 parameters to a PS script which will patch the SQL Server on a remote machine. Here is the code i used :

$Patcher = "\\remoteShareLocation\SQLpatcher.ps1"
$ver = "SQL2012"
$inst = "NEWINST"

Invoke-Command -ComputerName RMTMACHINE -credential dmn\usrname -ScriptBlock {$Patcher $ver $inst}

This asks for credentials but doesn't run and show any output also. Anything wrong with syntax ? Any alternate cmdlet please ?


Solution

  • if you have powershell V3 or higher you can use the using prefix to "transmit" the local vars to remote host :

    Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock {$using:Patcher $using:ver $using:inst}
    

    for olders versions look at the -argumentlist parameter of invoke-command