Abstract
Table of Contents
- Get-WMIObject
- Get-CimInstance:(Supported in PowerShell 3.0 onwards)
- Get-Service
Querying WMI
- Properties Information that can be retrieved.
- Instances of the class objects (services, Processes, Disks) each instance with Methods and Properties.
- Events are actions that WMI can monitor for and take action when they happen.
- Caveats of WMI

get-service -ComputerName hqdbsp18| Where-Object {$_.Name -like 'A*'} | select -expand name get-service -ComputerName hqdbsp18| Where-Object {$_.Name -match '^M' -or $_.Name -match '^SQ' } | select -expand nameGet-WMIObject Win32_Service -Filter "Name LIKE '[M-S]%'" -ComputerName hqdbsp18| Select *|format-table -AutoSizeGet-WMIObject Win32_Service -Filter "Name LIKE '[^M-S]%'" -ComputerName hqdbsp18| Select *|format-table -AutoSizeGet-WMIObject Win32_Service -Filter "Name LIKE '_Q_S%'" -ComputerName hqdbsp18| Select *|format-table -AutoSizeGet-wmiobject win32_service -ComputerName HQDBSP18 | group startname |select * |Format-Table -AutoSizeQuerying Get-CimInstance
The Get-CimInstance cmdlet available in PowerShell V3 can be used to retrieve WMI information from a remote computer using the WSMAN protocol instead of the legacy WMI service that uses DCOM and RPC. However, the remote computers must be running PowerShell 3 and WSMAN protocol version 3. When querying a remote computer, Get-CIMInstance setups a temporary CIMSession. However, if the remote computer is running PowerShell 2.0 this will fail. You have to manually create a CIMSession with a CIMSessionOption to use the DCOM protocol. This Script does it for you and creates a CimSession depending on the remote Computer capabilities.
Get-cimInstance win32_service -computer hqdbsp18 -filter "startname like '%localsystem%' and name like '%App%'"| Select Name,startmode,state,startname,systemname |Format-Table -AutoSizeGet-wmiobject win32_service -computer hqdbsp18 -filter "startname like '%localsystem%' and name like '%App%'"| Select Name,startmode,state,startname,systemname |Format-Table -AutoSizeGet-Service
Get-Command | findstr ServiceQuerying Get-Service
Credentials
$password = read-host -prompt "Enter your Password"$secure = ConvertTo-SecureString $password -force -asPlainText ConvertFrom-SecureString $secure |Out-File c:\SecurePassword.txtInvoke-Command { Get-Service "*SQL*" } -ComputerName HQDBSP18 -Credential $Credentials |select Name,displayname,Status |Format-Table -AutoSize# Hard code the the Credentials details$User = "CDW\DDOV810"$Pass = ConvertTo-SecureString "thanVitha@2016" -AsPlainText -Force#contain the username and password in a variable$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$PassGet-WMIObject Win32_Service -computer hqdbsp18 -credential $Credentials |where-object {$_.name -like "*SQL*"} |select Name,startmode,state,startname,systemname |Format-Table -AutoSize$User =
"hadt\sdsdfs"$Pass = ConvertTo-SecureString "sdfsdf@2016" -AsPlainText -Force#contain the username and password in a variable$Credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$Pass$CimSession = New-CimSession -ComputerName abcd -Credential $Credentials$service = Get-CimInstance -ClassName Win32_service -CimSession $CimSession$serviceRemove-CimSession -CimSession $CimSessionSimple way to get the services Details in a CSV file
# The output CSV file $CSVOutputfile="c:\Services.csv"#Input file server details location$input ="c:\server.txt"# query win32_service classGet-WmiObject Win32_Service -ComputerName (Get-Content $input )|Select-Object -Property __Server,Name,startmode,state,serviceaccount,displayname |Export-Csv -Path $CSVOutputfile#The Invoke-Item cmdlet provides a way to open a file from within Windows PowerShell.invoke-item $CSVOutputfileService details in Excel
PowerShell is made available to use Excel component by invoking excel.application COM object that will allow us to work with excel to add data and format that data. The New-object -ComObject creates a new excel object using COM components and it made visible for troubleshooting using visible property set to true. Once everything is defined, we can call the respective methods and properties to create workbook by adding sheet as its item
#Create a new Excel object using COM$Excel = New-Object -ComObject Excel.Application#you can see the Excel sheet and helpful in troubleshooting$Excel.visible = $True#After instantiating the excel objects, It's a time use its respective methods and properties$Excel = $Excel.Workbooks.Add()$Sheet = $Excel.Worksheets.Item(1)Workbook number and details
FileFormat numbers in Mac Excel. These are the main file formats in Windows Excel 2007-2016:
$xlOpenXMLWorkbook=[int]51<# .SYNOPSIS Name : Disk Space Utilization excel Report (Get-ServiceExcelReports.ps1) Description : Get disk space usage informations from remote server(s) with WMI and output CSV file Author : Prashanth Jayaram * Select list of servers from a text file * Get remote Servers informations with WMI and Powershell * service (Servername,Name,startmode,state,serviceaccount,displayname + display a Excel output) .INPUT Input Server text file Service list .OUTPUTS Excel output .NOTES Version: 1.0 Author: Prashanth Jayaram Creation Date: 2017-02-02 Purpose/Change: Initial script development .EXAMPLE .\Get-ServiceExcelReports.ps1 -ServerList C:\server.txt -includeService "VM","Dhcp","SQL"#> ######################################################################################### param ( [Parameter(Mandatory=$true)][string]$ServerList, [Parameter(Mandatory=$true)][string[]]$includeService ) $excel = New-Object -ComObject Excel.Application $workbook = $excel.Workbooks.Add()$worksheet = $workbook.Worksheets.Item(1) $worksheet.Cells.Item(1,1) = "Server Name"$worksheet.Cells.Item(1,2) = "Service Name"$worksheet.Cells.Item(1,3) = "State"$worksheet.Cells.Item(1,4) = "StartUp Type"$worksheet.Cells.Item(1,5) = "Service Account"$worksheet.Cells.Item(1,6) = "DisplayName" $range = $worksheet.UsedRange$range.Interior.ColorIndex = 19$range.Font.ColorIndex = 11$range.Font.Bold = $true $row = 2 $IncludeArray=@() #List of programs to exclude#$IncludeArray = $includeService Foreach($ServerName in (Get-Content $ServerList)){$service = Get-WmiObject Win32_Service -ComputerName $servernameif ($Service -ne $NULL){foreach ($item in $service) { #$item.DisplayName Foreach($include in $includeService) { #write-host $include if(($item.name).Contains($include) -eq $TRUE) { $worksheet.Cells.Item($row, 1) = $servername $worksheet.Cells.Item($row, 2) = $item.name $worksheet.Cells.Item($row, 3) = $item.Status $worksheet.Cells.Item($row, 4) = $item.startmode $worksheet.Cells.Item($row, 5) = $item.startname $worksheet.Cells.Item($row, 6) = $item.displayname $row++ } } }}} $range.EntireColumn.AutoFit()$excel.Visible = $TrueService Details in CSV file
<# .SYNOPSIS Name : Disk Space Utilization csv Report (Get-ServiceExcelReports.ps1) Description : Get disk space usage informations from remote server(s) with WMI and output CSV file Author : Prashanth Jayaram * Select list of servers from a text file * Get remote Servers informations with WMI and Powershell * service (Servername,Name,startmode,state,serviceaccount,displayname + display a csv output) .INPUT Input Server text file Service list .OUTPUTS CSV output, console and Grid output .NOTES Version: 1.0 Author: Prashanth Jayaram Creation Date: 2017-02-02 Purpose/Change: Initial script development .EXAMPLE .\Get-Servicecsv.ps1 -ServerList C:\server.txt -includeService "VM","Dhcp","SQL"#> ######################################################################################### param ( [Parameter(Mandatory=$true)][string]$ServerList, [Parameter(Mandatory=$true)][string[]]$includeService, [Parameter(Mandatory=$true)][string]$CSVoutputFile ) # Check if the output file CSV exist, if exists then delete it. if (test-path $CSVoutputFile ) { rm $CSVoutputFile } #Custom object to maintain the order of the output columns$props=@()Foreach($ServerName in (Get-Content $ServerList)) { $service = Get-WmiObject Win32_Service -ComputerName $servernameif ($Service -ne $NULL) { foreach ($item in $service) { #$item.DisplayName Foreach($include in $includeService) { #write-host $include if(($item.name).Contains($include) -eq $TRUE) { $props += [pscustomobject]@{ Servername = $servername name = $item.name Status = $item.Status startmode = $item.startmode state = $item.state serviceaccount=$item.startname DisplayName =$item.displayname} } } } } } $props | Format-Table Servername,Name,startmode,state,serviceaccount,displayname -AutoSize$props | Select-Object Servername,Name,startmode,state,serviceaccount,displayname |Export-Csv -Path $CSVoutputFileInvoke-item $CSVoutputFileService Details in HTML file
<# .SYNOPSIS Name : Disk Space Utilization excel Report (Get-ServiceHTML.ps1) Description : Get disk space usage informations from remote server(s) with WMI and output HTML file Author : Prashanth Jayaram * Select list of servers from a text file * Get remote Servers informations with WMI and Powershell * Disk (Disk type, letter, capacity in GB, free space in GB, % free , Status + display a HTML output) .INPUT Input Server text file Service list Output HTML file .OUTPUTS HTML output Console Output .NOTES Version: 1.0 Author: Prashanth Jayaram Creation Date: 2017-02-02 Purpose/Change: Initial script development .EXAMPLE Get-ServiceHTMLReport.ps1 -ServerList "\\hq6021\c$\server.txt" -includeService "VM","Dhcp","SQL" -OutputHTML "e:\CU2\ServericeReport.htm"#> #########################################################################################param ( [Parameter(Mandatory=$true)][string]$ServerList, [Parameter(Mandatory=$true)][String[]]$includeService, [Parameter(Mandatory=$true)][string]$OutputHTML )<#[String]$ServerList="\\hq6021\c$\server.txt"[String[]]$includeService= "VM","Dhcp","SQL"#>$props=@() Foreach($ServerName in (Get-Content $ServerList)) { $service = Get-WmiObject Win32_Service -ComputerName $servernameif ($Service -ne $NULL) { foreach ($item in $service) { #$item.DisplayName Foreach($include in $includeService) { #write-host $include if(($item.name).Contains($include) -eq $TRUE) { $props += [pscustomobject]@{ Servername = $servername name = $item.name Status = $item.Status startmode = $item.startmode state = $item.state serviceaccount=$item.startname DisplayName =$item.displayname} } } } } } $props |format-table Servername,Name,startmode,state,serviceaccount,displayname -autosize$a = "<style>"$a = $a + "BODY{background-color:peachpuff;}"$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;}"$a = $a + "</style>"$props |Select-Object Servername,Name,startmode,state,serviceaccount,displayname| ConvertTo-HTML -head $a | Out-File $OutputHTMLinvoke-item $OutputHTML









