Submitted By: Kent Finkle

Retrieves service data and then displays that information on an Excel spreadsheet.

PowerShell
Edit|Remove
$comments = @'
Script name: List-ServiceSheet.ps1
Created on: Monday, April 23, 2007
Author: Kent Finkle
Purpose: How can I use Windows Powershell to
List Service Data in a Spreadsheet?
'@

# -----------------------------------------------------
function Release-Ref ($ref) {
([System.Runtime.InteropServices.Marshal]::ReleaseComObject(
[System.__ComObject]$ref) -gt 0)
[System.GC]::Collect()
[System.GC]::WaitForPendingFinalizers()
}
# -----------------------------------------------------

$objExcel = new-object -comobject excel.application
$objExcel.Visible = $True
$objWorkbook = $objExcel.Workbooks.Add()
$objWorksheet = $objWorkbook.Worksheets.Item(1)

$x = 1

$sql = "Select * From Win32_Service"
$colServices = gwmi -query $sql

foreach ($objService in $colServices) {
    $objWorksheet.Cells.Item($x, 1) = $objService.Name
    $objWorksheet.Cells.Item($x, 2) = $objService.State
    $x++
}

$a = Release-Ref($objWorksheet)
$a = Release-Ref($objWorkbook)
$a = Release-Ref($objExcel)