VMware Cloud Community
bugeyemonster
Enthusiast
Enthusiast

script decisions based on string values ?

I am using this line to find the datastore name for each guest:

$row.DatastoreName = get-datastore -vm $_ | % {$_.name}

the output in the CSV file looks like vmax2345-sata-81 or instead of sata the output might contain fc.

I would like to change this value based on whether or not the variable contains sata or fc, to Tier3 or Tier2.

I am reading about this now.

advice would be greatly appreciated.

thanks

0 Kudos
2 Replies
ProPenguin
Hot Shot
Hot Shot

See if this helps. Watch out though the fc match may be a bit of refining so it is not to general.  It could grab more than you want if other names have fc some where else in them.  Hope it helps though.

Get-Datastore | foreach{ if($_.Name -match "sata"){echo "SATA";}elseif($_.Name -match "fc"){echo "fc";}};

bugeyemonster
Enthusiast
Enthusiast

this is what i ended up with. customer is custom field with billing information. Thanks for the input ProPenguin.

Get-VM -Location DataCenterName | %{
                $vmGuest = $_ | Get-VMGuest
               $vm = $_ | Get-View
                $row = "" | Select Customer, VMName, IPAddress, Host, OS, State, MemoryInGB, ProvisionedSpaceGB, vCPUcount, DatastoreName
                $row.Customer = $_.CustomFields["Line Of Business"]
                $row.VMname = $_.Name
                $row.IPAddress = $vmGuest.IPAddress[0]
                $row.Host = $_.Host.Name
                $row.OS = $_.Guest.OSFullName
                $row.State = $_.PowerState
                $row.MemoryInGB = $_.MemoryMB/1024
                $row.ProvisionedSpaceGB = "{0:N1}" -f (($_.HardDisks | %{$_.CapacityKB} | Measure-Object -Sum).Sum/1MB)
               $row.vCPUcount = $_.NumCpu
               $row.DatastoreName = get-datastore -vm $_ | foreach{ if($_.Name -match "sata"){echo "Tier3";}elseif($_.Name -match "fc"){echo "Tier2";}}
                $report += $row
}
$Report | Export-Csv -NoTypeInformation $filelocation
0 Kudos