Created
March 5, 2015 20:11
-
-
Save murarisumit/000579c7e642e1ce2c99 to your computer and use it in GitHub Desktop.
Remotely execute powershell script
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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