Monday, December 05, 2011

Installing a windows service using PowerShell

There is a hard way and an easy way. Here is the easy way:

Use Invoke-Command to call InstallUtil instead of New-Service:

Invoke-Command -ScriptBlock { C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /install "path to windows service.exe"}

Invoke-Command is better than Invoke-Item because the result of the execution is piped back to the console. (With Invoke-Item, any error messages don’t get sent back to caller).

Now here is an additional tip – if you need to specify the username and password under which the service should be run, you can use the undocumented InstallUtil parameters: username and password as shown below

Invoke-Command -ScriptBlock { C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /username=domain\account /password=password /install "path to windows service.exe"}

Finally, if you need to run this command on a remote machine, use the –ComputerName field that’s part of Invoke-Command

Invoke-Command -ComputerName machineName -ScriptBlock { C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /install "path to windows service.exe"}

And to uninstall a service:

Invoke-Command -ScriptBlock { C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /uninstall "path to windows service.exe"}

Useful powershell functions for installing, uninstalling, starting and stopping windows services on a remote machine

function StartService()
{
    param ([string] $serviceName, [string] $targetServer)
    Write-Host "starting service $serviceName on $targetServer"
    Invoke-Command -ComputerName $targetServer -ScriptBlock {param($sn) START-Service $sn} -Args $serviceName
    Invoke-Command -ComputerName $targetServer -ScriptBlock {param($sn) $servicePrior = Get-Service $sn; Write-Host $servicePrior.status} -Args $serviceName
}
function StopService()
{
    param ([string] $serviceName, [string] $targetServer)
    Write-Host "stopping service $serviceName on $targetServer"
    Invoke-Command -ComputerName $targetServer -ScriptBlock {param($sn) STOP-Service $sn} -Args $serviceName
    Invoke-Command -ComputerName $targetServer -ScriptBlock {param($sn) $servicePrior = Get-Service $sn; Write-Host $servicePrior.status} -Args $serviceName
}

function UninstallService()
{
    param ([string] $serviceName, [string] $servicePath, [string] $targetServer)
       
    Write-Host "uninstalling service $serviceName on $targetServer from path $servicePath"
    Invoke-Command -ComputerName $targetServer -ScriptBlock {param($sp) C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe /uninstall "$sp"} -Args $servicePath
}

function InstallService()
{
    param ([string] $serviceName, [string] $servicePath, [string] $targetServer)
       
    Write-Host "uninstalling service $serviceName on $targetServer from path $servicePath"
    Invoke-Command -ComputerName es1sbweb -ScriptBlock {param($sp) C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe `
    /username=domain\username /password=password `
    /install `
    "$sp"} -Args $servicePath
}

No comments: