Question : Powershell WMI Query

Hello,

I have written a Powershell script to query some information about our servers. I was wondering if someone could tell me how to omit the words "Hotfix" and "Update" from being written to the excel spreadsheet? Also, in between the program listings, there are sometimes huge gaps betweeen programs. Can you please tell me what is causing this and how to fix it?

Thank you
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
$strComputer = “.”

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “Computer”
$Sheet.Cells.Item(1,2) = “Drive Letter”
$Sheet.Cells.Item(1,3) = “Description”
$Sheet.Cells.Item(1,4) = “FileSystem”
$Sheet.Cells.Item(1,5) = “Size in GB”
$Sheet.Cells.Item(1,6) = “Free Space in GB”


$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True

$intRow = 2
$colItems = Get-wmiObject -class “Win32_LogicalDisk” -namespace “root\CIMV2" `
-computername $strComputer

foreach ($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.SystemName
$Sheet.Cells.Item($intRow,2) = $objItem.DeviceID
$Sheet.Cells.Item($intRow,3) = $objItem.Description
$Sheet.Cells.Item($intRow,4) = $objItem.FileSystem
$Sheet.Cells.Item($intRow,5) = $objItem.Size / 1GB
$Sheet.Cells.Item($intRow,6) = $objItem.FreeSpace / 1GB

$intRow = $intRow + 1

$WorkBook.EntireColumn.AutoFit()

}

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(7,1) = “Product”
$Sheet.Cells.Item(7,2) = “Vendor”
$Sheet.Cells.Item(7,3) = “Version”
$Sheet.Cells.Item(7,4) = “Install Date”

$intRow = 7
$Keys = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall

$Items = $keys |foreach-object {Get-ItemProperty $_.PsPath}

foreach ($item in $items){
$Sheet.Cells.Item($intRow,1) = $Item.DisplayName
$Sheet.Cells.Item($intRow,2) = $Item.Publisher
$Sheet.Cells.Item($intRow,3) = $Item.DisplayVersion
$Sheet.Cells.Item($intRow,4) = $Item.InstallDate

$intRow = $intRow + 1

$WorkBook.EntireColumn.AutoFit()
}

Clear

Answer : Powershell WMI Query

Hi Brent,

I've made a few tweaks that should do what you're looking for.  I will post again shortly explaining each change.

Cheers,

D
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
51:
52:
53:
54:
55:
56:
57:
58:
59:
60:
61:
62:
63:
64:
$strComputer = “.”

$Excel = New-Object -Com Excel.Application
$Excel.visible = $True
$Excel = $Excel.Workbooks.Add()

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item(1,1) = “Computer”
$Sheet.Cells.Item(1,2) = “Drive Letter”
$Sheet.Cells.Item(1,3) = “Description”
$Sheet.Cells.Item(1,4) = “FileSystem”
$Sheet.Cells.Item(1,5) = “Size in GB”
$Sheet.Cells.Item(1,6) = “Free Space in GB”


$WorkBook = $Sheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True

$intRow = 2
$colItems = Get-wmiObject -class “Win32_LogicalDisk” -namespace “root\CIMV2" `
-computername $strComputer

foreach ($objItem in $colItems) {
$Sheet.Cells.Item($intRow,1) = $objItem.SystemName
$Sheet.Cells.Item($intRow,2) = $objItem.DeviceID
$Sheet.Cells.Item($intRow,3) = $objItem.Description
$Sheet.Cells.Item($intRow,4) = $objItem.FileSystem
$Sheet.Cells.Item($intRow,5) = $objItem.Size / 1GB
$Sheet.Cells.Item($intRow,6) = $objItem.FreeSpace / 1GB

$intRow = $intRow + 1

$WorkBook.EntireColumn.AutoFit()

}

$intRow = $intRow + 1

$Sheet = $Excel.WorkSheets.Item(1)
$Sheet.Cells.Item($introw,1) = “Product”
$Sheet.Cells.Item($introw,2) = “Vendor”
$Sheet.Cells.Item($introw,3) = “Version”
$Sheet.Cells.Item($introw,4) = “Install Date”

$introw = $introw + 1
$Keys = Get-ChildItem HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall

$Items = $keys | foreach-object {Get-ItemProperty $_.PsPath} | where-object {($_.DisplayName -notmatch "Update|Hotfix") -and ($_.DisplayName -ne $null)}

foreach ($item in $items){
$Sheet.Cells.Item($intRow,1) = $Item.DisplayName
$Sheet.Cells.Item($intRow,2) = $Item.Publisher
$Sheet.Cells.Item($intRow,3) = $Item.DisplayVersion
$Sheet.Cells.Item($intRow,4) = $Item.InstallDate

$intRow = $intRow + 1

}

$WorkBook.EntireColumn.AutoFit()

Clear
Random Solutions  
 
programming4us programming4us