Record an Error in Excel

If I get an error, Access denied or RPC service is unavailable, how
would I Print the error message out to  $sh.Cells.Item($row, 2)

And the answer is:

$cred = Get-Credential ‘YourAdminName’ #adname or name@domain

$row = 2

$xl = New-Object -c excel.application

$xl.visible = $true

$wb = $xl.workbooks.add()

$sh = $wb.sheets.item(1)

$strComputer = "dogbert"

$Var3 = gwmi -Class "Win32_OperatingSystem" -namespace "rootCIMV2" -computername $strComputer -cred $cred -ea continue

# the above is all one line

if ($Var3) {$sh.Cells.Item($row, 3) = $Var3.Caption }
Else
{ $sh.Cells.Item($row, 2) = $error[0].tostring() }

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s