Solved

Need help with a simple powershell script

Posted on 2013-11-21
4
318 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

"Migrate" an SMTP relay receive connector to a new server using info from an old server.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

726 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