Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Need help with a simple powershell script

Posted on 2013-11-21
4
Medium Priority
?
322 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 71

Accepted Solution

by:
Qlemo earned 1200 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 41

Assisted Solution

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

Ready for your healthcare security check-up?

In the past few years, healthcare organizations have become a prime target for advanced attacks. Does your organization have what it needs to defend itself? Schedule your healthcare security check-up today and download our free Healthcare Security Resource Kit today!

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
Measuring Server's processing rate with a simple powershell command. The differences in processing rate also was recorded in different use-cases, when a server in free and busy states.
Loops Section Overview
Screencast - Getting to Know the Pipeline

885 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