A N

August 31, 2016

Manage Windows Services with Powershell

Filed under: Tips — donrsh @ 9:58 am

Manage Windows Services with Powershell. With this script you can connect to a server(s) and change logon account or just show the services.

#REQUIRES -Version 4.0
<#
	.NOTES
	===========================================================================
	 Created on:   	2016-08-30
	 Created by:   	Arash Nabi - arash@nabi.nu
	 Created for:	-
	===========================================================================
 
	.SYNOPSIS
		Manage service accounts
	
	
	.EXAMPLE
        
        PS C:\WINDOWS> Set-Service -Servers Server001,Server002,Server003 -ServiceName fake* -UpdateServices
        WARNING: No Service with name fake* found in Server003 


        Name         DisplayName        ServerName PathName               StartMode    State  
        ----         -----------        ---------- --------               ---------    -----  
        Fake         Fake Service       Server001  C:\windows\notepad.exe DC\SysScript Stopped
        Fake         Fake Service       Server002  C:\windows\notepad.exe DC\SysScript Stopped
        
     
    .EXAMPLE
             
        PS C:\WINDOWS> Set-Service -Servers Server001,Server002,Server003 -ServiceName fake* -ShowServices

        WARNING: No Service with name fake* found in Server003 

        DisplayName        State   StartName    SystemName
        -----------        -----   ---------    ----------
        Fake Service       Stopped DC\SysScript Server001   
        Fake Service       Stopped DC\SysScript Server002 
#>

function Set-Service
{
	[CmdletBinding()]
	param
	(
		[Parameter(Mandatory = $true)]
		$Servers,
		[Parameter(Mandatory = $true)]
		[string]$ServiceName,
		[switch]$ShowServices,
		[switch]$UpdateServices
	)
	
	Function Change-Service()
	{
		$cred = Get-Credential -Credential Domain\UserName
		$ServiceObj = @()
		foreach ($Server in $Servers)
		{
			# CIM stuff
			$objOption = New-CimSessionOption -Protocol Dcom
			$objSession = New-CimSession -ComputerName $server -SessionOption $objOption
			if ($objSession)
			{
				$params = @{
					'ServerName' = $Server
				}
				$getServices = Get-CimInstance win32_service -CimSession $objSession | Where-Object { $_.Name -like $ServiceName }
				if ($getServices)
				{
					foreach ($Service in $getServices)
					{
						$X = $Service | Invoke-CimMethod -Name Change -Arguments @{ StartName = "$($cred.UserName)"; StartPassword = "$($cred.Password)" }
						$ListService = Get-CimInstance win32_service -CimSession $objSession | Where-Object { $_.Name -like $ServiceName }
						$params.Add("DisplayName", "$($ListService.DisplayName)")
						$params.Add("StartMode", "$($ListService.StartName)")
						$params.Add("Name", "$($ListService.Name)")
						$params.Add("State", "$($ListService.State)")
						$params.Add("PathName", "$($ListService.PathName)")
					}
					$ServiceObj += New-Object psObject -Property $params
				}
				else
				{
					Write-Warning -Message "No Service with name $serviceName found in $server"
				}
				
			}
			else
			{
				Write-Warning -Message "ohh nooo! No Session established to $server"
			}
		}
		
		$ServiceObj | Select-Object -Property Name, DisplayName, ServerName, PathName, StartMode, State | Format-Table -AutoSize
	}
	
	if ($UpdateServices)
	{
		Change-Service
	}
	
	
	
	if ($ShowServices)
	{
		foreach ($Server in $servers)
		{
			$objOption = New-CimSessionOption -Protocol Dcom
			$objSession = New-CimSession -ComputerName $server -SessionOption $objOption
			if ($objSession)
			{
				$ListServices = Get-CimInstance win32_service -CimSession $objSession | Where-Object { $_.Name -like $ServiceName } |
				Select-Object -Property DisplayName, State, StartName, SystemName
				if ($ListServices)
				{
					$ListServices
				}
				else
				{
					Write-Warning -Message "No Service with name $serviceName found in $server `n"
				}
			}
			else
			{
				Write-Warning -Message "ohh nooo! No Session established to $server"
			}
			
		}
	}
}
Advertisements

Leave a Comment »

No comments yet.

RSS feed for comments on this post. TrackBack URI

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: