June 1, 2009

Remote Execution of Powershell

Powershell 2 has some really cool features. One of the features I really like is the remoting portion. Although does not work on Vista, for me I just needed it to work on Win2k3. You will also need powershell installed on that server you are trying to connect to.

Here is some sample code and a helper script (thanks to my co-worker 'AC' for this one) to get it to work:
function ExecuteScriptRemotely($script, $scriptArgs, $server, $cred, $logFile)
{
$cmd = " & { $script } $scriptArgs "

if($logFile)
{
$cmd += " > `"{0}`"" -f $logFile.Replace('"', '')
}

$encodedCommand = EncodeCommand $cmd
$commandLine = "powershell -ExecutionPolicy Bypass -EncodedCommand $encodedCommand"
StartRemoteProcess $commandLine $server $cred
}

function StartRemoteProcess($commandLine, $server, $cred)
{
if($cred)
{
Invoke-WMIMethod -path win32_process -name create -argumentList $commandLine -credential $cred -computerName $server
}
else
{
Invoke-WMIMethod -path win32_process -name create -argumentList $commandLine -computerName $server
}
}

function EncodeCommand($expression)
{
#Convert the command into an encoded command for PowerShell
$commandBytes = [System.Text.Encoding]::Unicode.GetBytes($expression)
$encodedCommand = [Convert]::ToBase64String($commandBytes)
$encodedCommand
}

function CreateCredentials($user, $password)
{
$password = convertto-securestring $password -asplaintext -force
New-Object System.Management.Automation.PSCredential $user, $password
}
Here is how you would use it:
$MY_DIR = Split-Path -Path $MyInvocation.MyCommand.Definition -Parent
. (Join-Path $MY_DIR RemHelper.ps1)

# Create c:\bar.txt
$script = '"foo" > "c:\bar.txt"'

# run notepad
$script = 'notepad'

$scriptArgs = $null
$server = "MyServer"
$user = "Admin"
$password = "Password"

$cred = CreateCredentials $user $password
ExecuteScriptRemotely $script $scriptArgs $server $cred "c:\log.txt"

No comments:

Post a Comment