Solved

Need help with a simple powershell script

Posted on 2013-11-21
4
317 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 40

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
"Migrate" an SMTP relay receive connector to a new server using info from an old server.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

808 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