简体   繁体   中英

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 ?

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM