'Field Returns a False or True -- not details
After connecting to vSphere 6.7 from a win10 machine I run the following PowerShell command: Expecting the CSV output to include the details of Name, OperatingSystems (windows or linux), VMhost/IP, PowerState and the datastore. However, in the field "Operating Systems" I get the value FALSE -or- TRUE. Still relatively new to PowerShell so I don't see my request asking for a false/true. How do I correct this? So I can change the commands from: OSWindows -to- OSLinux? Thanks
Get-VM | Select-Object Name,
@{Name = 'Operatingsystem'; Expression = {$_.Guest -match 'OsWindows'}},
VMHost, PowerState,
@{Name = 'Datastore'; Expression = {$_ | Get-Datastore}} |
Export-CSV -Path 'C:\users\username\documents\VMReport.csv' -NoTypeInformation
Solution 1:[1]
$_.Guest -match 'OsWindows'
will return a boolean ($true
or $false
).
If you want $false
to result in the string Linux
and $true
to result in the string Windows
, you can use the following trick:
@{...; Expression = {@('Linux','Windows')[($_.Guest -match 'OsWindows')]}}
PowerShell will evaluate $_.Guest -match 'OsWindows'
- passing the resulting value to an array indexer @()[...]
then forces PowerShell to convert it to a numerical value - and since it converts $false
to 0
and $true
to 1
, they end up resolving one of the two array items.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Mathias R. Jessen |