VMware Cloud Community
DevKhurana
Enthusiast
Enthusiast
Jump to solution

How to get all drives details like capacity and free Space of VMs using Invoke-VMScript

Hello,

Below highlighted command is running in powershell but I am not able to execute it with Invoke-VMScript, can any one help me please.

Get-VM | Select Name,
@{N='Drives-Detail';E={Invoke-VMScript -VM $_ -GuestUser domain\testuser -GuestPassword ********** -ScriptType PowerShell -ScriptText {Get-WMIObject win32_volume -Filter ‘drivetype = 3’ | select driveletter, label, @{LABEL=’Capacity’;EXPRESSION={$_.Capacity/1GB}}, @{LABEL=’GBfreespace’;EXPRESSION={$_.freespace/1GB}}} | select -ExpandProperty ScriptOutput}}

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$MyScript = @'
Get-WMIObject win32_volume -Filter 'drivetype = 3' |
select driveletter, label, @{N='Capacity';E={$_.Capacity/1GB}}, @{N='GBfreespace';E={$_.freespace/1GB}} |
Convertto-CSV
'@

$Sinvoke = @{
  VM = ''
  GuestUser = 'domain\testuser'
  GuestPassword = 'password'
  ScriptType = 'PowerShell'
  ScriptText = $MyScript
}

Get-VM -PipelineVariable vm |
Where-Object { $_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows' } |
ForEach-Object -Process {
  $sInvoke.VM = $vm
  try {
    $result = Invoke-VMScript @sInvoke
    $result.ScriptOutput | ConvertFrom-Csv |
    Select-Object @{N = 'VM'; E = { $vm.Name } }, DriveLetter, Label, Capacity
  } catch {
    Write-Error "Failed for $($vm.Name)"
  }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

0 Kudos
7 Replies
LucD
Leadership
Leadership
Jump to solution

The ScriptText parameter expects a string, not a code block.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DevKhurana
Enthusiast
Enthusiast
Jump to solution

Thanks LucD, I would be thankful if you correct this code as you said because I am a bit unfamiliar with coding. 

0 Kudos
DevKhurana
Enthusiast
Enthusiast
Jump to solution

I wrote below script also but it is giving me same undesirable output.  

$MyScript = @"

Get-WMIObject win32_volume -Filter ‘drivetype = 3’ | select driveletter, label, @{LABEL=’Capacity’;EXPRESSION={$_.Capacity/1GB}}, @{LABEL=’GBfreespace’;EXPRESSION={$_.freespace/1GB}}

"@

Get-VM myvm | 
Select Name,
@{N='Drives-Detail';E={Invoke-VMScript -VM $_ -GuestUser domain\testuser -GuestPassword *********** -ScriptType PowerShell -ScriptText $MyScript |

select -ExpandProperty ScriptOutput}}

 

And my output is coming :-

 

DevKhurana_0-1626000643345.png

 

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Looks like you copied that code from a webpage, which would explain the funny single quotes you have in there.
Also, you better place inline strings between single quotes, that way nothing gets substituted.
Another errror is that an Expression of a calculated property is expected to return a single vale, not a complex object (like ScriptOutput).

You could do something like this instead (note that I used splatting to  make the code more legible)

$MyScript = @'
Get-WMIObject win32_volume -Filter 'drivetype = 3' |
select driveletter, label, @{N='Capacity';E={$_.Capacity/1GB}}, @{N='GBfreespace';E={$_.freespace/1GB}} |
Convertto-CSV
'@

$vm = Get-VM myvm
$Sinvoke = @{
    VM = 'veng'
    GuestUser = 'domain\testuser'
    GuestPassword = 'password'
    ScriptType = 'PowerShell'
    ScriptText = $MyScript
}
$result = Invoke-VMScript @sInvoke
$result.ScriptOutput | ConvertFrom-Csv |
select @{N='VM';E={$vm.Name}},DriveLetter,Label,Capacity

 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DevKhurana
Enthusiast
Enthusiast
Jump to solution

Actually Yes, I copied that code from a webpage.

Your code worked for me for now, Thanks.

Could you please guide me if I want to run that code for all my datacenters VMs instead a single VM and extract details in a CSV file then how this code would be?  

0 Kudos
LucD
Leadership
Leadership
Jump to solution

You could do something like this

$MyScript = @'
Get-WMIObject win32_volume -Filter 'drivetype = 3' |
select driveletter, label, @{N='Capacity';E={$_.Capacity/1GB}}, @{N='GBfreespace';E={$_.freespace/1GB}} |
Convertto-CSV
'@

$Sinvoke = @{
  VM = ''
  GuestUser = 'domain\testuser'
  GuestPassword = 'password'
  ScriptType = 'PowerShell'
  ScriptText = $MyScript
}

Get-VM -PipelineVariable vm |
Where-Object { $_.PowerState -eq 'PoweredOn' -and $_.Guest.OSFullName -match 'Windows' } |
ForEach-Object -Process {
  $sInvoke.VM = $vm
  try {
    $result = Invoke-VMScript @sInvoke
    $result.ScriptOutput | ConvertFrom-Csv |
    Select-Object @{N = 'VM'; E = { $vm.Name } }, DriveLetter, Label, Capacity
  } catch {
    Write-Error "Failed for $($vm.Name)"
  }
} | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
DevKhurana
Enthusiast
Enthusiast
Jump to solution

Thanks a lot LucD, I appreciate your help. 

0 Kudos