[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Powerscript

Posted on 2014-04-14
3
Medium Priority
?
249 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 35

Assisted Solution

by:Dan Craciun
Dan Craciun earned 800 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 1200 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

 The Evil-ution of Network Security Threats

What are the hacks that forever changed the security industry? To answer that question, we created an exciting new eBook that takes you on a trip through hacking history. It explores the top hacks from the 80s to 2010s, why they mattered, and how the security industry responded.

Question has a verified solution.

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

The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Loops Section Overview
Screencast - Getting to Know the Pipeline

872 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