Hi all,
I have just purchased "Managing VMware Infrastructure with Windows PowerShell" and am really impressed with what you can do - first go with PowerShell
Having gone through some of the book I came across the following script which is meant to pull out the VM name, CapacityMB, FreeSpaceMB and USage % but I can't get it to work by using the command "Get-VM | scripts:Get-VMDiskUsage.ps1"
########################################################################################
VM Tools must be in "green" state and VM must be powered on
Begin {
This hides the errors which appear if the VM Tools are not running or if VM is off
$ErrorActionPreference = "SilentlyContinue"
$VMNameExp = @{
Name = "Name"
Expr = { $VMName }
}
$CapacityExp = @{
Name = "CapacityMB"
Expr = { "{0:n2}" -f ( $_.Capacity / 1MB ) }
}
$FreeSpaceExp = @{
Name = "FreeSpaceMB"
Expr = { "{0:n2}" -f ( $_.FreeSpace / 1MB ) }
}
$UsageExp = @{
Name = "Usage %"
Expr = { "{0:p2}" -f ( $_.FreeSpace / $_.Capacity ) }
}
Process {
if ( $_.GetType().Name -ne 'VirtualMachineImpl' ) {
Throw "This script expects a VirtualMachineImpl object as imput."
}
$VMName = $_.Name
$_ | Get-VMGuest | Select-Object -ExpandProperty Disks |
Select-Object $VMNameExp, $CapacityExp, $FreeSpaceExp, $UsageExp
}
########################################################################################
Any help would be appreciated as would eventually like to get this Emailed as a small, simple daily report.
Thanks.
Pete.
You are connected to the vCenter ?
Did you run
Connect-ViServer -Server <your-VC-server>
before you start the script ?
I attached my copy, perhaps try that one.
This is what you should get
You could comment out the $ErrorActionPreference line, that way you will see the error messages.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
The problem is caused by the fact that you are trying to use a provider (scripts:) which is not on your client.
Hal seems to have defined a provider for his scripts and apparently that provider made it to the printed copy.
If you're in the same directory (do a pwd at the prompt to check) as where you saved the script, you can do
Get-VM | .\Get-VMDiskUsage.ps1
else you can also give the full path to the script.
For example
Get-VM | C:\Scripts\Get-VMDiskUsage.ps1
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi,
Yeah tried "Get-VM | .\Get-VMDiskUsage.ps1" and that seems to just run the script and then return me to the prompt - no error but no output.
Any other suggestions?
Thanks.
Pete.
You are connected to the vCenter ?
Did you run
Connect-ViServer -Server <your-VC-server>
before you start the script ?
I attached my copy, perhaps try that one.
This is what you should get
You could comment out the $ErrorActionPreference line, that way you will see the error messages.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks for the help LucD! It was a typo on my part - I had missed of a '}' before the 'Process'
The problem is caused by the fact that you are trying to use a provider (scripts:) which is not on your client.
Hal seems to have defined a provider for his scripts and apparently that provider made it to the printed copy.
Oops! In my defense, Luc was basically the senior technical editor and he didn't catch the bug, so it's his fault. :smileygrin:
[vExpert|http://www.vmware.com/communities/vexpert/], PowerShell MVP, VI Toolkit forum moderator
Author of the book: Managing VMware Infrastructure with PowerShell
Co-Host, PowerScripting Podcast (http://powerscripting.net)
Need general, non-VMware-related PowerShell Help? Try the forums at PowerShellCommunity.org
I humbly bow my head in shame
I'll try to do a better job when Hal finishes the 2nd edition.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
I was wondering how I could write this file out to a csv file and make it look right?
This is what I have, but it does not look like what it does when it writes out to the screen
$_ | Get-VMGuest | Select-Object -ExpandProperty Disks | ConvertTo-Csv | Set-Content c:\vms.csv
Thanks
You could use the Export-Csv cmdlet.
Something like this
$_ | Get-VMGuest | Select-Object -ExpandProperty Disks | Export-Csv "C:\Report.csv" -NoTypeInformation -UseCulture
Note that the -Userculture parameter is a PowerShell v2 RTM feature.
____________
Blog: LucD notes
Twitter: lucd22
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference