Skip to content

Instantly share code, notes, and snippets.

@murarisumit
Created March 5, 2015 20:11
Show Gist options
  • Save murarisumit/000579c7e642e1ce2c99 to your computer and use it in GitHub Desktop.
Save murarisumit/000579c7e642e1ce2c99 to your computer and use it in GitHub Desktop.
Remotely execute powershell script
#Script that will be invoked when invoke-command will be executed
$script = [scriptblock]::create("get-host
hostname")
$node = "1.12.3.4"
#UserName that will deploy it
$username = "username"
$pw = "password"
# Create Credentials
$securepw = ConvertTo-SecureString $pw -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argument $username, $securepw
# Create and use session
$session = New-PSSession -credential $cred -ComputerName $node
Invoke-Command -Session $session -ScriptBlock $script
Remove-PSSession $session
============================================================
#To allow communication via IP.
#It could be simply done by just giving username if you're in same domain.
Set-Item WSMan:\localhost\Client\TrustedHosts -Value "*" -Force
============================================================
#Reference: http://superuser.com/questions/646566/how-to-make-a-remote-computer-run-powershell-script-on-the-remote-computer-itsel
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment