1st PS scriopt

  • Follow


Hello All,

I am new to Powershell (scripting in fact) and am having problems with my 
first script.  I am trying to query a list of servers and pull information 
and export it to an Excel Spreadsheet.  Powershell error:

Exception getting "Item": "Invalid index. (Exception from HRESULT: 
0x8002000B (DISP_E_BADINDEX))"
At C:\gt\bios.ps1:4 char:35
+ $sheet = $workbook.worksheets.Item <<<< ("Bios Data")
    + CategoryInfo          : NotSpecified: (:) [], 
GetValueInvocationException
    + FullyQualifiedErrorId : 
CatchFromBaseAdapterParameterizedPropertyGetValueTI

==============================================
Script:

# Varibles
$excel = New-object -comobject Excel.Application
$workbook = $excel.workbooks.add()
$sheet = $workbook.worksheets.Item("Bios Data")
$bios = systeminfo | find /i "bios version"
$Host = systeminfo | find /i "Host Name"
$x = 2
###########
$excel
$workbook
$sheet
$sheet.cells.item(1,1) = "Server Name"
$sheet.cells.item(1,2) = "Bios Date"

$workbook.worksheet.item(1).Name = "Server data"

#Get Bios Data
ForEach ($S in Get-Content(“c:\servers.txt”)) { $bios }
{
$sheet.cells.item($x, 1) = $host
$sheet.cells.item($x, 2) = $bios
$x++
}


Any help would be greatly appreciated.  Thanks
-- 
Look at the pricetag, where they do that at?
0
Reply Utf 12/2/2009 8:02:01 PM

The cause of the error is that you're referencing a nonexistant sheet. Try 
the code below, it gets the fist sheet and then renames it. Also note that 
the BIOS information is obtained through Get-WMIObject, this information 
needs to be concatenated to match that returned by systeminfo.exe, still, 
Get-WMIObject is faster:

$excel = New-object -comobject Excel.Application
$excel.Visible = $true
$excel.DisplayAlerts = $false
$workbook = $excel.Workbooks.Add()
# get fist sheet
$sheet = $workbook.Worksheets.Item(1)
# rename first sheet
$sheet.Name = 'Bios Data'
$sheet.cells.item(1,1) = "Server Name"
$sheet.cells.item(1,2) = "Bios Data"
$x = 2
ForEach ($S in Get-Content C:\servers.txt ) {
 $bios = gwmi Win32_BIOS -ComputerName $S
 $biosDate = $bios.ReleaseDate -replace '^(....)(..)(..).+$', '$2/$3/$1'
 $strBios = $bios.Manufacturer + ' ' + $bios.SMBIOSBIOSVersion +
 ', ' + $biosDate
 $sheet.cells.item($x, 1) = $bios.__SERVER
 $sheet.cells.item($x, 2) = $strBios
 $x++
}
# adjust width of columns
[void]$sheet.UsedRange.Columns.AutoFit()
# save workbook
$workbook.SaveAs('C:\Bios Data.xlsx')
$workbook.Close()
$excel.Quit()

-- 
Robert
0
Reply Utf 12/3/2009 4:34:02 AM


1 Replies
411 Views

(page loaded in 0.021 seconds)

Similiar Articles:
















7/17/2012 2:31:16 PM


Reply: