PowerShell and Excel - Can't minimize excel

I have a powershell script that pulls some basic information from servers and dumps it to an excel sheet.  It works fine as long as I leave the excel window open.  If I minimize the window I get blanks in my worksheet.  Any idea how I can still get the results to populate even if I minimize the window?

$erroractionpreference = "SilentlyContinue"

$a = New-Object -comobject Excel.Application
$a.visible = $true

$b = $a.Workbooks.Add()
$c = $b.Worksheets.Item(1)

$c.Cells.Item(1,1)  = "Server Name"
$c.Cells.Item(1,2)  = "RAM(GB)"
$c.Cells.Item(1,3)  = "Model"

$d = $c.UsedRange
$d.Interior.ColorIndex = 19
$d.Font.ColorIndex = 11
$d.Font.Bold = $True

$intRow = 2

$colComputers = gc $env:userprofile\desktop\list.txt
$colComputers = $colComputers.Split(",")

foreach ($strComputer in $colComputers){

$comp = gwmi Win32_computersystem -computername $strComputer

$c.Cells.Item($intRow,1)  = $comp.name
$c.Cells.Item($intRow,2)  = "{0:N0}" -f ($comp.TotalPhysicalMemory/1GB)
$c.Cells.Item($intRow,3)  = $comp.Model
$intRow = $intRow + 1
}
$d.EntireColumn.AutoFit()
cls


September 14th, 2015 11:07am

Is there a reason to have it visible during its creation? If not, then just set $a.Visible to false, then open the spreadsheet after it is created, you would also need to call the save method to save the document.

So since you are not saving the spreadsheet, what is the purpose of putting this information int

Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 11:10am

Remove this line and fix your error: $erroractionpreference = "SilentlyContinue"

NEVER use that line without understanding the consequences of such a foolish action.  This should never be used blindly in any programming or scripting system.

September 14th, 2015 12:19pm

I guess that will have to do.  I really just wanted to have it run without having to save.  Thanks clayman2
Free Windows Admin Tool Kit Click here and download it now
September 14th, 2015 1:10pm

Thanks for the tip jrv. 
September 14th, 2015 1:11pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics