2

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 ?

user3180704
  • 89
  • 2
  • 8

2 Answers2

6

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

Loïc MICHEL
  • 22,418
  • 8
  • 68
  • 94
  • Just to complement: If $Patcher, $ver and $inst are commands then you have to put "&" before that. Ex.: Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock { & $using:Patcher $using:ver $using:inst} – wallybh Feb 19 '15 at 10:40
0

Try

$cred = Get-Credential
Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock {$Patcher $ver $inst}

or create a credential object like this:

$username = "username"
$password = "password" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)

You'll need to pass a credential object instead of Domain\username.

And to run a script file remotely please have a look at https://stackoverflow.com/a/10446318/4550393

Community
  • 1
  • 1
Norman
  • 2,789
  • 3
  • 19
  • 37
  • Did not work with credentials also, so tried sending parameters : $ScriptBlockContent = { param ($p, $v, $i) powershell.exe -ExecutionPolicy Bypass $p $v $i } Invoke-Command -ComputerName RMTMACHINE -credential $cred -ScriptBlock $ScriptBlockContent -ArgumentList $Patcher, $ver, $inst. This also throws error saying : The term '\\remoteShareLocation\Patcher.ps1' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,verify that the path is correct and try again. – user3180704 Feb 19 '15 at 11:28