List Remote or Local Windows Services

In a lot of IT projects, you often need to be able to quickly and easily see the state of of certain Windows services on certain machines.  And in a large company, it may take time to get in touch with the right person to give you the information quickly and it may not be up to date information.  But there are ways to get this information yourself if you have the right access.

This script takes a CSV file as input with Computer Name, User Name and Password.  The Computer name is for the machine you would like to query and the credentials should be for the user that has access to that machine.  It also has a parameter called “State” which allows filtering by the state of the service (Running, Stopped or All).  The script will then enumerate through the list of machines provided in the CSV file and generate a quick report.

Param ( [string]$Computers, [string]$State = "all" )

$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$dtFileDate = Get-Date -format "MMddyyyy"
$strPassFile = "$scriptPath\password.txt"

# Prompt for credentials and generate secure file
function genSecurePassFile() {
	write-host ""
	write-host "Please enter a password below to create a SecureString and store it to a file."
	write-host ""
	$strSecPwd = Read-Host "Password: " -AsSecureString
	$strSecPwd | ConvertFrom-SecureString | Out-File $strPassFile
	write-host ""
	write-host "SecureString password saved to:" $strPassFile
	write-host ""
	exit
}

# Computers parameter
Switch ($Computers.ToLower()) {
	"password" { # Generate a secure string password file
		write-host ""
		genSecurePassFile
		write-host ""
		break
	}
	default { # Assume its a path to CSV file by default
		# Read file data to arrays
		$AllComputers = Import-Csv $Computers
		break
	}
}

# State parameter
Switch ($State.ToLower()) {
	"running" { # Only running services
		$myQuery = "select * from win32_service where state='running'"
		$st = $State.ToLower()
		break
	}
	"stopped" { # Only stopped services
		$myQuery = "select * from win32_service where state='stopped'"
		$st = $State.ToLower()
		break
	}
	default { # All services
		$myQuery = "select * from win32_service"
		$st = "all"
		break
	}
}

$cnt = $AllComputers.Length
write-host "Getting all $st services for $cnt computers: "
foreach ($Computer in $AllComputers) {
	$CompName = $Computer.Name.ToUpper()
	$OutFile = $Computer.Name.ToLower()
	try {
		Write-Host "  $CompName... " -NoNewLine
		$securePassword = $Computer.Password | ConvertTo-SecureString
		$credential = New-Object System.Management.Automation.PSCredential $Computer.User, $securePassword
		Get-WmiObject -Computer $CompName -Query $myQuery -Credential $credential -ErrorAction Stop | Select-Object SystemName, DisplayName, Name, Status, PathName, ServiceType, StartMode, StartName, State | Export-CSV "$scriptPath\$OutFile-$st-$dtFileDate.csv" -notype
		Write-Host "SUCCESS!" -ForegroundColor Green
	} catch {
		$myError = $_.Exception.Message.ToString().Replace(",", "")
		Write-Host "ERROR - $myError" -BackgroundColor Black -ForegroundColor Red
		"$CompName : ERROR - $myError"  | Out-File -FilePath "$scriptPath\$OutFile-$st-$dtFileDate.csv"
	}
}

Write-Host "Completed!"
About Vikram 10 Articles
I am just an IT person trying to share as much knowledge as possible hoping someone will find it helpful. It just takes time to organize everything from my head into something that makes sense.