Solved

Need help with a simple powershell script

Posted on 2013-11-21
4
315 Views
Last Modified: 2013-11-22
Studying powershell, I made my first script but doesn't work. Can you tell me what's wrong with below? It seems that the minspace is being inserted to scriptblock of invoke-command. Why?


<#
.SYNOSIPS
Get-DiskSpaceUsedInThreshold retrieves disks which are under given free space threshold. 
.DESCRIPTION
returns disks under given free space threshold. 
.PARAMETER ComputerName
One or more computer names 
.PARAMETER FreeSpaceThreshold 
free space threshold in float number which filters disks. Disks under this thresold will be in output. 
.EXAMPLE
Get-DiskUsedSpaceInThreshold -ComputerName srv2 -FreeSpaceThreshold .1

shows disks with free space is less than 10%

#>

Param(
    $ComputerName='localhost',
    $FreeSpaceThreshold=90
)

#Conver to double number
$minspace= $FreeSpaceThreshold/100

Invoke-Command -ComputerName $ComputerName -ScriptBlock { gwmi  -class win32_logicaldisk -filter "DriveType=3" |
Where-Object  { $_.FreeSpace / $_.Size -lt $minspace} | 
select –Property DeviceID,FreeSpace,Size}

Open in new window

0
Comment
Question by:crcsupport
  • 2
4 Comments
 
LVL 1

Author Comment

by:crcsupport
ID: 39667351
I'm just wondering if variable can't not be put in the middle of pipeline. Is it?
0
 
LVL 68

Accepted Solution

by:
Qlemo earned 300 total points
ID: 39667391
Vars are evaluated on run time. However, you have a completely different scope (a different computer), and there is no relation to the script var defined outside of Invoke-Command. So it isn't the pipeline, it is Invoke-Command building its own scope, and not having access to the outside script vars.

You can move all starting from the where to outside of the Invoke-Command, but that means all WMI objects have to be passed to the local PC for further processing, and then got filtered. Doing that is costly for PowerShell.

Better to stuff the limit in as an parameter (I'm only showing the relevant part, to not distract):
Invoke-Command -ComputerName $ComputerName -ScriptBlock {
   param($minspace)
   gwmi  -class win32_logicaldisk -filter "DriveType=3" |
      Where-Object  { $_.FreeSpace / $_.Size -lt $minspace} | 
      select –Property DeviceID,FreeSpace,Size
  } -ArgumentList $minspace

Open in new window

0
 
LVL 39

Assisted Solution

by:footech
footech earned 200 total points
ID: 39667446
Also, unless you have PowerShell Remoting configured on the target computer, the Invoke-Command will fail.  PS Remoting is the better way to do it, but you can do it without with the following.
gwmi -class win32_logicaldisk -filter "DriveType=3" -ComputerName $ComputerName |
      Where-Object  { $_.FreeSpace / $_.Size -lt $minspace} | 
      select –Property DeviceID,FreeSpace,Size

Open in new window

0
 
LVL 1

Author Closing Comment

by:crcsupport
ID: 39669077
Very well explained by you two.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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 …
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

746 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

10 Experts available now in Live!

Get 1:1 Help Now