Saturday, August 11, 2012

PowerShell How to execute script file remotely on another machine

Scenario: You have a Books.ps1 file you want to execute the code in Books.ps1 file on another machine.How to do that?

Solution:
try
{

$securePassword= ConvertTo-SecureString "YourPassword" -AsPlainText -force

$cred = New-Object System.Management.Automation.PsCredential("Domain\username",$securePassword)

Invoke-Command -Authentication credssp -Credential $cred -FilePath "C:\Srini\Books.ps1" -ComputerName "Machine_FQDN"
}

catch

{
Write-Host $_
}