Monday, June 28, 2010

Powershell Remote Execution (invoke-command) with Username Password Authentication

Powershell adds remote execution with 2.0. The commands are executed on the remote shell using the current local user or some other domain user specified using the -credential parameter.

Using just username and password is not as easy and of course not very secure because the password is saved in the script. Nevertheless sometimes you just have to and here is how.

$pass = convertto-securestring "P@ssW0rd" -asplaintext -force
$mycred = new-object -typename System.Management.Automation.PSCredential -argumentlist "DOMAIN\User",$pass
invoke-command remote-machine {get-process} -credential $mycred

Tuesday, June 22, 2010

Start Stop IIS7 or Any Other Service With Powershell

The IIS7 service can be controlled via Powershell using the following commandline.

(Get-WmiObject Win32_Service -Filter "Name='w3svc'").InvokeMethod("StopService",$null)

Use StopService to stop the service and StartService to start the service.

Use any other service name instead of w3svc to start or stop an arbitrary service.