This should be the finished product. Copy and make changes as needed.
#======================================================
Get Server Information
Modify the path to get list of servers
#======================================================
param (
[string]$ServerArray = (Get-Content -Path ListServers.txt)
)
#======================================================
Create new Excel workbook
#======================================================
Write-Verbose “$(Get-Date): Script begins!”
Write-Verbose “$(Get-Date): Opening Excel…”
$Excel = New-Object -ComObject Excel.Application
$Excel.Visible = $True
$Excel = $Excel.Workbooks.Add()
$Sheet = $Excel.Worksheets.Item(1)
$Sheet.Name = “Server Services”
#=======================================================
Place Column Header Names in Excel file
#=======================================================
$Sheet.Cells.Item(1,1) = “Server”
$Sheet.Cells.Item(1,2) = “Service Name”
$Sheet.Cells.Item(1,3) = “Status”
$Sheet.Cells.Item(1,4) = “Startup Type”
$Sheet.Cells.Item(1,5) = “Service Account”
#=======================================================
Format the Top Header Rows
#=======================================================
$intRow = 2
$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 40
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
#=======================================================
Main Loop
Gets the computer name from the txt file, searches and removes any notlike
Places results in the Excel file
#=======================================================
ForEach ($Server in $ServerArray)
{
$Sheet.Cells.Item($intRow, 1) = $Server
$Sheet.Cells.Item($intRow, 2) = “”
$Sheet.Cells.Item($intRow, 3) = “”
$Sheet.Cells.Item($intRow, 4) = “”
$Sheet.Cells.Item($intRow, 5) = “”
$a=Get-WmiObject win32_service -ComputerName $Server | where {$.startname -notlike “LocalService*” -and $.startname -notlike “localsystem*” -and $_.startname -notlike “NetworkService*”}
$a | % {
If ($Server -notlike “SERVERTEST”)
{
If ($Server -ne $_.systemname)
{
$intRow ++
}
else
{
$Sheet.Cells.Item($intRow, 1) = $_.systemname
$Sheet.Cells.Item($intRow, 2) = $_.Name
$Sheet.Cells.Item($intRow, 3) = $_.State
$Sheet.Cells.Item($intRow, 4) = $_.StartMode
$Sheet.Cells.Item($intRow, 5) = $_.StartName
$intRow ++
}
}
$WorkBook.EntireColumn.AutoFit() | Out-Null
}
}
#=======================================================
Format the Ending Row
#=======================================================
$intRow ++
$Sheet.Cells.Item($intRow,1) = “Server Inventory Completed”
$Sheet.Cells.Item($intRow,1).Font.Bold = $True
$Sheet.Cells.Item($intRow,1).Interior.ColorIndex = 40
$Sheet.Cells.Item($intRow,2).Interior.ColorIndex = 40
$Sheet.Cells.Item($intRow,3).Interior.ColorIndex = 40
Write-Verbose “$(Get-Date): Completed!”