Solved

Powerscript

Posted on 2014-04-14
3
229 Views
Last Modified: 2014-04-20
I have a simple powershell that checks the disk size for servers. I find that in case any server has access issue, the result can't be generated.  
eg. Get-WmiObject : Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESS
DENIED))

Is it possible to trap this error and bypass it such that the sequent servers can be checked ? Tks


$body = Get-WmiObject Win32_LogicalDisk -filter “DriveType=3" -computer (Get-Content .\computer.csv) | Select SystemName,DeviceID, @{Name=”Free (%)”;Expression={“{0:N0}” -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name=”Size (GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”Free (GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} |
  ConvertTo-Html
Send-MailMessage -SmtpServer exdag.abc.com.cn -From dc02@abc.com.cn -To cngit-admin@abc.com.cn -Subject 'Servers Disk Space Analysis' -Body ($body -join "`n") -BodyAsHtml
0
Comment
Question by:AXISHK
  • 2
3 Comments
 
LVL 34

Assisted Solution

by:Dan Craciun
Dan Craciun earned 200 total points
ID: 40000806
Have you tried –ErrorAction silentlycontinue?
I mean
Get-WmiObject Win32_LogicalDisk -filter “DriveType=3" -computer (Get-Content .\computer.csv) –ErrorAction silentlycontinue

Open in new window


LE: On second thought, it might not work:
The ErrorAction parameter has no effect on terminating errors (such as
        missing data, parameters that are not valid, or insufficient
        permissions) that prevent a command from completing successfully.

HTH,
Dan
0
 
LVL 29

Accepted Solution

by:
becraig earned 300 total points
ID: 40001955
Simply do a try catch
$body = @()
(Get-Content .\computer.csv) | % {
Try {
$body += Get-WmiObject Win32_LogicalDisk -filter “DriveType=3" -computer $_ | Select SystemName,DeviceID, @{Name=”Free (%)”;Expression={“{0:N0}” -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name=”Size (GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”Free (GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} |
  ConvertTo-Html
	}
	Catch {
		write-host "$_ failed !!" -fore RED
	}


}

Send-MailMessage -SmtpServer exdag.abc.com.cn -From dc02@abc.com.cn -To cngit-admin@abc.com.cn -Subject 'Servers Disk Space Analysis' -Body ($body -join "`n") -BodyAsHtml 

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40010565
Ok so I will update the final solution here and on the other thread:
I realized I was creating multiple html files :(

So what I did here was pipe in your original serverlist
Then test the access to query wmi
Then created a clean server list then ran the command.

$ErrorActionPreference= 'silentlycontinue'
$servers = @()
(Get-Content .\computers.csv) | % {
$error.clear()
$gwmival = Get-WmiObject Win32_LogicalDisk -computer $_
if ($error -ne $null)
{write-host "$_ failed with errors" -fore RED}
else {$servers += $_; }
}
$servers | out-file validlist.txt
 Get-WmiObject Win32_LogicalDisk -filter "DriveType=3" -computer (gc validlist.txt) | Select SystemName,DeviceID, @{Name=”Free (%)”;Expression={“{0:N0}” -f (($_.freespace /1gb) / ($_.size /1gb) * 100) }}, @{Name=”Size (GB)”;Expression={“{0:N1}” -f($_.size/1gb)}},@{Name=”Free (GB)”;Expression={“{0:N1}” -f($_.freespace/1gb)}} | ConvertTo-Html


Send-MailMessage -SmtpServer exdag.abc.com.cn -From dc02@abc.com.cn -To cngit-admin@abc.com.cn -Subject 'Servers Disk Space Analysis' -Body ($body -join "`n") -BodyAsHtml 

Open in new window

0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Utilizing an array to gracefully append to a list of EmailAddresses
This article will help you understand what HashTables are and how to use them in PowerShell.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

28 Experts available now in Live!

Get 1:1 Help Now