Solved

Powerscript

Posted on 2014-04-14
3
228 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 28

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 28

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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Synchronize a new Active Directory domain with an existing Office 365 tenant
This tutorial demonstrates a quick way of adding group price to multiple Magento products.
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

758 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

24 Experts available now in Live!

Get 1:1 Help Now