Solved

powershell where condition

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

Assisted Solution

by:oBdA
oBdA earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
Much better formatting thank you very much
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
This article will help you understand what HashTables are and how to use them in PowerShell.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

744 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

18 Experts available now in Live!

Get 1:1 Help Now