3

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 ?

2 Answers 2

7

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

Sign up to request clarification or add additional context in comments.

1 Comment

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}
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

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.