Solved

Powerscript

Posted on 2014-04-14
3
234 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 35

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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Set OWA language and time zone in Exchange for individuals, all users or per database.
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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