Solved

powershell where condition

Posted on 2014-01-30
4
547 Views
Last Modified: 2014-01-30
Hi guys, i have the following code which worked until i tried to use the where condition. Im looking for only values less than 5 to be part of the output. i think im almost there. Any advice would be great

thanks

$Servers = Get-Content  C:\Users\Downloads\exchange.txt


foreach ( $Server1 in $Servers ) {
@"
$server1
"@

$disk = Get-WmiObject Win32_LogicalDisk -ComputerName $Server1 -Filter "DeviceID='C:'" |
  Where-Object {$_.FreeSpace -lt "5"}

$disk.FreeSpace /1GB -as [int]
 }
0
Comment
Question by:cwstad2
  • 2
4 Comments
 
LVL 83

Assisted Solution

by:oBdA
oBdA earned 200 total points
ID: 39820964
Assuming you want servers with less than 5GB available space, you handled it correctly in the output you're generating: FreeSpace is in bytes.
You don't get any output because in all likelihood, you don't have servers with less than 5 bytes available.
Try Where-Object {$_.FreeSpace -lt 5GB}
0
 
LVL 15

Author Comment

by:cwstad2
ID: 39821019
Damn would you believe it. Thanks. Is there a way to supress the results with the ones greater than 5GB. Its showing the results for all.
0
 
LVL 40

Accepted Solution

by:
Subsun earned 300 total points
ID: 39821198
You can try this code to display the output with server name and free space..
Get-Content  C:\Users\Downloads\exchange.txt | % {
Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter "DeviceID='C:'" | 
 Select __SERVER,@{N="FreeSpace GB";E={[math]::Round(($_.FreeSpace /1GB),2)}} |
 Where-Object {$_."FreeSpace GB" -lt 5}
}

Open in new window

Or you can use export-csv to export the details to a csv file..
Get-Content  C:\Users\Downloads\exchange.txt | % {
Get-WmiObject Win32_LogicalDisk -ComputerName $_ -Filter "DeviceID='C:'" | 
 Select __SERVER,@{N="FreeSpace GB";E={[math]::Round(($_.FreeSpace /1GB),2)}} |
 Where-Object {$_."FreeSpace GB" -lt 5}
} | Export-csv c:\report.csv -nti

Open in new window

0
 
LVL 15

Author Closing Comment

by:cwstad2
ID: 39821243
Much better formatting thank you very much
0

Featured Post

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

Question has a verified solution.

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

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

932 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

12 Experts available now in Live!

Get 1:1 Help Now