I'm trying to combine two inventory scripts and have it create just one Excel worksheet with all the information on it and have the worksheet named whatever the computer name is.
I've been wrestling around with this for over a week but I'm just too ignorant/dumb to figure it out. Any help would be greatly appreciated.
The script below will make a list of installed software.
#===============================================================================================
# Microsoft PowerShell
# ==============================================================================================
Param(
[String[]]$Computer = $env:COMPUTERNAME,
[Switch]$Save,
[String]$As = "$($PWD.Path)\Inventory.xls",
[Management.Automation.PsCredential]$Credential
)
Function Open-Excel ([Switch]$Visible)
{
New-Variable -Name Excel -Scope Script
$Script:Excel = New-Object -Com Excel.Application
[Void]$Script:Excel.Workbooks.Add()
$Script:Excel.Visible = $Visible
New-Variable -Name ExcelWorksheetItems -Scope Script -Value @{}
}
Function Close-Excel
{
$Script:Excel.Workbooks.Item(1).SaveAs($As)
$Script:Excel.Quit()
}
Function Write-Worksheet($WorksheetName, $Object)
{
$Workbook = $Script:Excel.Workbooks.Item(1)
If ($Script:ExcelWorksheetItems.$WorksheetName -ne $Null)
{
$Worksheet = $Workbook.Worksheets.Item($WorksheetName)
}
Else
{
$Script:ExcelWorksheetItems.Add($WorksheetName, 2)
If ($Script:ExcelWorksheetItems.Count -lt 4)
{
# Overwrite Sheet1, Sheet2 and Sheet3
$Worksheet = $Workbook.Worksheets.Item($Script:ExcelWorksheetItems.Count)
}
Else
{
$Worksheet = $Workbook.Worksheets.Add()
}
$Worksheet.Name = $WorksheetName
# Write the header line
$i = 1; ([Array]$Object)[0].PsObject.Properties | %{ $Worksheet.Cells.Item(1, $i) = $_.Name; $i++ }
}
$Object | %{
$i = 1
ForEach ($Property in $_.PsObject.Properties)
{
$Worksheet.Cells.Item($Script:ExcelWorksheetItems.$WorksheetName, $i) = $_.$($Property.Name)
$i++
}
$Script:ExcelWorksheetItems.$WorksheetName += 1
}
}
Function Get-InstalledSoftwareInfo($Params)
{
$Product = Get-WMIObject Win32_Product @Params
Return $Product | Select-Object `
@{n='Device_Name';e={ $Params["ComputerName"] }},
@{n='Software_Installed';e={ $_.Name }}
}
Open-Excel -Visible
$Computer | %{
$Params = @{"ComputerName"=$_}
If ($Credential) { $Params.Add("Credential") = $Credential }
Write-Worksheet "$env:COMPUTERNAME" $(Get-InstalledSoftwareInfo $Params)
}
If ($Save) { Close-Excel }