shaju
 New Member Posts:12

 |
| 01 Dec 2008 10:38 AM |
|
Hallo experts, i am a newbie to powershell scripting, Here's what i am trying to do. Query all VM's and get the Freespace and if the Freespace less than or equal mark it as red in output html file.
Get-VM | Where { $_.PowerState -eq "PoweredOn" } | Get-VMGuest | Select VmName -ExpandProperty Disks | Select VmName, Path, $a = ([math]::Round( $_.FreeSpace / 1024 / 1024 ), if ($a -lt –le "500") {$_ -replace "", ""} else {$_ -replace "", ""} > .\get-service.html I am getting the following error - Select Object: A positional parameter cannot be found that accepts argument '=''.At line 1 char 110
Can anybody help me sorting this error. Thanks a lot in anticipation.
|
|
|
|
|
shaju
 New Member Posts:12

 |
| 02 Dec 2008 09:16 AM |
|
can anybody help me please |
|
|
|
|
halr9000 PowerShell MVP, Site Admin
 Basic Member Posts:334

 |
| 02 Dec 2008 12:38 PM |
|
Please don't post a thread with the text as an image. Doing so makes it almost impossible for others to find this data when searching. Not to mention that since you used a lossy graphics format (JPG), the text is blurry, and to be honest, a bit too small for me to read comfortably. I hate to rip on you but...this just isn't done! Please re-post your question using text, and then I will delete this thread. |
|
Community Director, PowerShellCommunity.org Co-host, PowerScripting Podcast Author, TechProsaic |
|
|
glnsize
 Basic Member Posts:101
 |
| 02 Dec 2008 01:42 PM |
|
try something like...
get-vm| ? {$_.powerstate -eq "PoweredOn"}|Get-VMGuest|select Vmname -ExpandProperty disks | ? {($_.FreeSpace)/1gb -le "500"} | ft VMName, Path
|
|
|
|
|
shaju
 New Member Posts:12

 |
| 02 Dec 2008 08:40 PM |
|
Have editted it to read as text |
|
|
|
|
shaju
 New Member Posts:12

 |
| 02 Dec 2008 08:56 PM |
|
Thanks for your reply. i tried your code changing the 3000, i get all as true which seems to be not correct for hto01 and rto01 d:\ , i expected it to be false
get-vm| where {$_.powerstate -eq "PoweredOn"}|Get-VMGuest|select Vmname -ExpandProperty disks | select vmname, p ath, {($_.FreeSpace)/1gb -le "500"}, @{ N="Freespace"; E={ [math]::Round( $_.FreeSpace / 1024 / 1024 ) } }
VmName Path ($_.FreeSpace)/1024 -le "3000" Freespace ------ ---- ----------------------------- --------- rto01 C:\ True 2576 rto01 D:\ True 3278 hto01 C:\ True 4973
Thaks for your replies and advices in advance |
|
|
|
|
glnsize
 Basic Member Posts:101
 |
| 03 Dec 2008 01:02 AM |
|
I modified your original post to use format-table... Below is a direct port. 1gb is a built-in constant. At run time it is evaluated as 1073741824. Likewise 1mb will equate to 1048576, and 1kb = 1024. Personally I prefer their use where possible as it leads to more readable code. I think this is where the false positive before came from. A simple and easy to make mistake.
try something like this;
get-vm| where-object {$_.powerstate -eq "PoweredOn"}|Get-VMGuest|select-object Vmname -ExpandProperty disks | where-object {($_.FreeSpace)/1gb -le "30"} | select-object VmName, Path, @{N="FreeSpaceGB"; E={ [math]::Round( $_.FreeSpace / 1024 / 1024 ) }
This will filter any drive with less then 30gb of freespace, and return VmName, Path, and FreeSpace in GB.
Update: AHHH stupid WYSIWYG eating code again! Had to remove the < CODE > tags to make it readable [ ] are getting eaten.
|
|
|
|
|
shaju
 New Member Posts:12

 |
| 03 Dec 2008 08:48 AM |
|
Sorry i dont get the desired result
i get something like this >> >> >> >> and no output on the screen, also no errors. Hope i have typed it good.
< code >
get-vm | where-object {$_.powerstate -eq "PoweredOn"} | Get-VMGuest | select-object Vmname -ExpandProperty disks | where-object {($_.FreeSpace)/1gb -le "30"} | select-object VmName, Path, @{N="FreeSpaceGB"; E={ [math]::Round( $_.FreeSpace / 1024 / 1024 ) }
< / code > |
|
|
|
|
glnsize
 Basic Member Posts:101
 |
| 05 Dec 2008 12:02 AM |
|
Shaju,
When you get >> that means your currently nested, and missing a } somewhere. My bad... I missed one in the code i posted before. try this;
get-vm | where-object {$_.powerstate -eq "PoweredOn"} | Get-VMGuest | select-object Vmname -ExpandProperty disks | where-object {($_.FreeSpace)/1gb -le "30"} | select-object VmName, Path, @{N="FreeSpaceGB"; E={ [math]::Round( $_.FreeSpace / 1024 / 1024 ) } }
that's the problem with one-liners there hard to read, and lead to mistakes. Hope that works for you!
~Glenn |
|
|
|
|
shaju
 New Member Posts:12

 |
| 05 Dec 2008 10:54 AM |
|
you were right about the missing }.
i see indeed the vmname path and FreeSpaceGB but the true and false is not be seen. I belive where-object {($_.FreeSpace)/1gb -le "30"} part is not working.
Thanks in anticipation for your reply. |
|
|
|
|
glnsize
 Basic Member Posts:101
 |
| 05 Dec 2008 10:08 PM |
|
sorry about that... Where-object will only pass an object if the condition is true. So in the above example only Vm's with less then 30gb of free space will evaluate to True, and be passed to the next cmdlet in the pipeline. Any Vm with more then 30 gb are filtered out and wont be outputted. Further more since the filtering is done after we expand the disks property. Your only getting the individual disks that need addressing.
Short version powershell is doing all the work for you. If a vm has a disk on the output you need to worry about it. If it doesn't then all is well and powershell isn't bothering you with that information.
Hope that helps
~Glenn |
|
|
|
|
shaju
 New Member Posts:12

 |
| 06 Dec 2008 08:21 AM |
|
Thanks it works. To test i changed the 1gb to read as 1mb and 30 to read as 500. Then it gave me only vm's where the disk space is lower than 500mb. This thread is solved with your help Glenn |
|
|
|
|