PowerCLI VM Power state with wildcard suffix

PowerCLI

You want to find the power state for a list of VMs, but do not want to add a * at the end of each VM name to find VM names that have suffixes in vCenter vs DNS.

Since “V” was provided, you can see ALL of the VMs that start with V.    “fake” was not found (correct).  The win2012-disk-test was found and reported powered off.

<#
https://vmscribble.com PowerCLI 6.5.4
Provide a .csv of VM names to find the power state or not found.   An * is applied to the suffix in case the VM name provides has a suffix in vCenter.
#>

$folder = Get-Location

Write-Host "
1)  The PowerCLI script expects vms.csv to be in in $folder
2) The 1st of line of the csv must have: Name
CSV file example:
Name
VM1
VM2
" -ForegroundColor White

# Ask for the vCenter 
$vCenter = Read-Host "Enter the FQDN of the vCenter"

# Connect to your vCenter
Connect-VIServer $vCenter | Out-Null

$searchvms = Import-Csv ".vms.csv"

$vms =@()
foreach ($vm in $searchvms){
$found = Get-VM | Where-Object {$_.name -match $vm.Name -eq "true"} 
if($found -ne $null){
foreach($find in $found){

$details = @{
Name = $find.Name 
Power_State = $find.PowerState 
}
$vms += New-Object PSObject -Property $details

}
}
else {
$details = @{
Name = $vm.Name 
Power_State = "Not Found"
}
$vms += New-Object PSObject -Property $details
}
}

# Create CSV
$vms | export-csv -Path .Results.csv -NoTypeInformation 
Write-Host "Results.csv created in $folder" -ForegroundColor Green

# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false
The Ask

You want to find the power state for a list of VMs, but do not want to add a * at the end of each VM name to find VM names that have suffixes in vCenter vs DNS.

Since “V” was provided, you can see ALL of the VMs that start with V.    “fake” was not found (correct).  The win2012-disk-test was found and reported powered off.

PowerCLI Script
<#
https://vmscribble.com PowerCLI 6.5.4
Provide a .csv of VM names to find the power state or not found.   An * is applied to the suffix in case the VM name provides has a suffix in vCenter.
#>

$folder = Get-Location

Write-Host "
1)  The PowerCLI script expects vms.csv to be in in $folder
2) The 1st of line of the csv must have: Name
CSV file example:
Name
VM1
VM2
" -ForegroundColor White

# Ask for the vCenter 
$vCenter = Read-Host "Enter the FQDN of the vCenter"

# Connect to your vCenter
Connect-VIServer $vCenter | Out-Null

$searchvms = Import-Csv ".vms.csv"

$vms =@()
foreach ($vm in $searchvms){
$found = Get-VM | Where-Object {$_.name -match $vm.Name -eq "true"} 
if($found -ne $null){
foreach($find in $found){

$details = @{
Name = $find.Name 
Power_State = $find.PowerState 
}
$vms += New-Object PSObject -Property $details

}
}
else {
$details = @{
Name = $vm.Name 
Power_State = "Not Found"
}
$vms += New-Object PSObject -Property $details
}
}

# Create CSV
$vms | export-csv -Path .Results.csv -NoTypeInformation 
Write-Host "Results.csv created in $folder" -ForegroundColor Green

# Disconnect from the vCenter
Disconnect-VIServer $vCenter -Confirm:$false
Menu