Solved

Need help with a simple powershell script

Posted on 2013-11-21
4
316 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 69

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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Synchronize a new Active Directory domain with an existing Office 365 tenant
The following article is intended as a guide to using PowerShell as a more versatile and reliable form of application detection in SCCM.
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…

813 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

16 Experts available now in Live!

Get 1:1 Help Now