Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

Writing to different Excel sheets

$
0
0

I have this code that "almost" works.   I am looking for machines that have a Model = Optiplex 780"  So if Model -contains 780 I want that data written to a separate sheet in Excel.  What happens when I run this code is any machine that has Model -contains 780 is written to the first sheet and the second sheet.   And the second sheet has a blank line for every Non 780 computer.  I want the 780 machines to only be written to the second sheet and I don't want either sheet to have blank lines.  

 

$erroractionpreference=“SilentlyContinue”
$a=New-Object-comobjectExcel.Application
$a.visible=$True
$b=$a.Workbooks.Add()
$b.worksheets.add()
$c=$b.Worksheets.Item(1)
$c.Cells.Item(1,1) =“Computer Name”
$c.Cells.Item(1,2) =“OS Version”
$c.Cells.Item(1,3) =“IP vLan”
$c.Cells.Item(1,4) =“Description”
$c.Cells.Item(1,5) =“Model”
$c.Cells.Item(1,6) ="BIOS Version"
$d=$c.UsedRange
$d.Interior.ColorIndex= 37
$d.Font.ColorIndex= 1
$d.Font.Bold=$True
$d.EntireColumn.AutoFit()
$intRow= 2

$c2=$b.Worksheets.Item(2)
$c2.Cells.Item(1,1) =“Computer Name”
$c2.Cells.Item(1,2) =“OS Version”
$c2.Cells.Item(1,3) =“IP vLan”
$c2.Cells.Item(1,4) =“Description”
$c2.Cells.Item(1,5) =“Model”
$c2.Cells.Item(1,6) ="BIOS Version"
$d2=$c2.UsedRange
$d2.Interior.ColorIndex= 37
$d2.Font.ColorIndex= 1
$d2.Font.Bold=$True
$d2.EntireColumn.AutoFit()
$intRow= 2


$pc= (Get-ADComputer-Filter*-SearchBase"ou=1350 Smith;ou=mgh_workstations,dc=acme,dc=org").Name

foreach ($machinein$pc)
{

$computerName=Get-WMIObject-class Win32_ComputerSystem -computername$machine | Select-Object-ExpandProperty name;
$computerOS=Get-WmiObject-Class Win32_OperatingSystem -ComputerName$machine | Select-Object-ExpandProperty version;
$getip= ([version](Test-Connection$machine-Count 1).IPV4Address.IPAddressToString);
$desc= (Get-ADComputer$machine-Properties Description).description
$model= (Get-WmiObject-Class Win32_ComputerSystem -ComputerName$machine).model
$BIOSVer= (Get-WmiObject-Class Win32_BIOS -computername$machine-Property SMBIOSBIOSVersion).SMBIOSBIOSVersion



$c.Cells.Item($intRow, 1) =$computerName
$c.Cells.Item($intRow, 2) =$computerOS
$c.Cells.Item($intRow, 3) =$getip
$c.Cells.Item($intRow, 4) =$desc
$c.Cells.Item($intRow, 5) =$model
If ($model.contains("780")) {
$c2.Cells.Item($intRow, 1) =$computerName
$c2.Cells.Item($intRow, 2) =$computerOS
$c2.Cells.Item($intRow, 3) =$getip
$c2.Cells.Item($intRow, 4) =$desc
$c2.Cells.Item($intRow, 5) =$model
$c2.Cells.Item($intRow, 6) =$BIOSVer
$intRow+= 1
$d2.EntireColumn.AutoFit()
}

$c.Cells.Item($intRow, 6) =$BIOSVer

$intRow+= 1
$d.EntireColumn.AutoFit()

} 


Viewing all articles
Browse latest Browse all 6937

Trending Articles