Solved

powershell where condition

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

MIM Survival Guide for Service Desk Managers

Major incidents can send mastered service desk processes into disorder. Systems and tools produce the data needed to resolve these incidents, but your challenge is getting that information to the right people fast. Check out the Survival Guide and begin bringing order to chaos.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
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...
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

738 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