Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

powershell where condition

Posted on 2014-01-30
4
Medium Priority
?
591 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
[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
4 Comments
 
LVL 85

Assisted Solution

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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Learn the basics of while and for loops in Python.  while loops are used for testing while, or until, a condition is met: The structure of a while loop is as follows:     while <condition>:         do something         repeate: The break statement m…
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)
Suggested Courses

730 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