Solved

powershell where condition

Posted on 2014-01-30
4
558 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 84

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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

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 …
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)

840 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