Hi,
I am trying to get full vm name by passing partial name and its powerstate from specific folder in no. of vCenters. I am getting some errors in below script.
I tried fix it but somewhere I am missing, elseif loop is not working properly. Can some one help me by correcting the bug.
$report = @()
$servers = get-content "D:\Servers.txt"
$date = Get-Date -Format 'yyyyMMdd'
$vc1 = "vCenter01"
$vc2 = "vCenter02"
$vc3 = "vCenter03"
$vc4 = "vCenter04"
$vc5 = "vCenter05"
$vc6 = "vCenter06"
$vc7 = "vCenter07"
$vc8 = "vCenter08"
$vc9 = "vCenter09"
$vc10 = "vCenter10"
write-progress -Activity "Fetching VM details with full name....."
foreach ($server in $servers)
{
$vcs1 = Connect-VIServer -Server $vc1
$info = "" | Select ActualName, NameWithTimeStamp, PowerState
$vm = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
if($vm)
{ $info.ActualName = $server
$info.NameWithTimeStamp = $vm.Name
$info.PowerState = $vm.PowerState
}
elseif ($vm -eq "")
{
$vcs2 = Connect-VIServer -Server $vc2
$vm2 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm2.Name
$info.PowerState = $vm2.PowerState
}
elseif(($vm -and $vm2) -eq "")
{
$vcs3 = Connect-VIServer -Server $vc3
$vm3 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm3.Name
$info.PowerState = $vm3.PowerState
}
elseif(($vm -and $vm2 -and $vm3) -eq "")
{
$vcs4 = Connect-VIServer -Server $vc4
$vm4 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm4.Name
$info.PowerState = $vm4.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4) -eq "")
{
$vcs5 = Connect-VIServer -Server $vc5
$vm5 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm5.Name
$info.PowerState = $vm5.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5) -eq "")
{
$vcs6 = Connect-VIServer -Server $vc6
$vm6 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm6.Name
$info.PowerState = $vm6.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5 -and $vm6) -eq "")
{
$vcs7 = Connect-VIServer -Server $vc7
$vm7 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm7.Name
$info.PowerState = $vm7.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5 -and $vm6 -and $vm7) -eq "")
{
$vcs8 = Connect-VIServer -Server $vc8
$vm8 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm8.Name
$info.PowerState = $vm8.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5 -and $vm6 -and $vm7 -and $vm8) -eq "")
{
$vcs9 = Connect-VIServer -Server $vc9
$vm9 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $vm9.Name
$info.PowerState = $vm9.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5 -and $vm6 -and $vm7 -and $vm8 -and $vm9) -eq "")
{
$vcs10 = Connect-VIServer -Server $vc10
$vm10 = Get-Folder -Name "App01" | Get-VM | Where {$_.Name -like "$server*"}
$info.NameWithTimeStamp = $_.Name
$info.PowerState = $_.PowerState
}
elseif(($vm -and $vm2 -and $vm3 -and $vm4 -and $vm5 -and $vm6 -and $vm7 -and $vm8 -and $vm9 -and $vm10) -eq "")
{
$info.ActualName = $server
$info.NameWithTimeStamp = "Not Found"
$info.PowerState = "Not Found"
}
else
{
}
}
$report += $info
write-host -f green "Data Collection completed."
$report | Export-CSV -Path "D:\VMs_$date.csv" -NoTypeInformation -UseCulture
Disconnect-VIServer -Confirm:$false
Thanks in advance...
Hi
Can any one please help me on this.
Thanks in advance.
I've read this a few times, I do not understand what you are trying to do.
Hi,
Happy to see your response.
I have VM's like VMname with numbers across 10 vCenters like below.
VMname01_1500001
VMname02_1500002
VMname03_1500003
VMname04_1500004
VMname05_1500005
With the list off only names in txt file like, VMname01, VMname02, VMname03, VMname04, VMname05.
Script has to take this txt file as input, it has to fetch full name of these VM's and its powerstate from across all vCenters.
1) If a vm is not available in one vCenter, it has to connect and check in next vCenter.
2) if a vm not available in all 10 vCenters, it has to return not found.
Need a report like below.
ActuatVMName | FullVMName | PowerState |
VMname01 | VMname01_1500001 | PowerOFF |
VMname02 | VMname02_1500002 | PowerOFF |
VMname03 | VMname03_1500003 | PowerOFF |
VMname04 | VMname04_1500004 | PowerOFF |
VMname05 | Not found | Not found |
Thanks in advance,
I switched up the logic a bit, Iterate through the vCenters. This will create a hash table with the Partial name, and a second value of text ("null") as it finds servers it will update that with the name in vCenter. if it is never found, it will say null when the looping is over. You may need to make some minor edits, as my test environment is nothing like what you are describing.
# Create a hashtable
$myHash = @{}
foreach ($PartialServer in $ServerlistRaw){
$myHash["$PartialServer"] = "null"
}
$vCenterList = @("vC1","VC2")
foreach ($vCenter in $vCenterList){
#Connect to vCenter
Connect-VIServer $vCenter
# Loop through each Server
Foreach($PartialServer in $ServerlistRaw){
$vm = $null
$vm = Get-Folder -Name "App01" | Get-VM | Where-Object {$_.Name -like "*$PartialServer*"}
if ($vm){
$vmName = $vm.Name
$myHash[$PartialServer] = "$vmName"
}
}
# Disconnect from Server
Disconnect-VIServer -Force -Confirm:$False
}
# Do the report thing with the hash table
$myHash