I have an input file with a set of operating system hostnames. I need to take this OS hostname from the input file and do a get-vm against vCenter
(Assume VM tools are installed on each VM and it is powered on so that the OS hostname is available)
If I have these 3 hostnames in my input file:
windows1
Windows2
windows3
How would I pull these and find the VM names for each as seen in vCenter?
Thanks!
I'm afraid there are a couple of flaws in that script.
$ServerList = Get-Content C:\scripts\servers.txt
$Report = @()
$vms = Get-VM
foreach ($Server in $ServerList){
$vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{
$report += New-Object PSObject -Property @{
VM_Name = $_.Name
DNS_Name = $_.ExtensionData.Guest.Hostname
}
}
}
$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
This is basic but should do the job, if "VM _Name" is blank most likely tools isn't running
$ServerList = Get-Content C:\scripts\servers.txt
$Report = @()
foreach ($Server in $ServerList){
$VM = Get-VM | Where-Object {$_.ExtensionData.Guest.Hostname -like $server}
$Report += New-Object PSObject -Property @{
VM_Name = $VM
DNS_Name = $server}
}
$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
I'm afraid there are a couple of flaws in that script.
$ServerList = Get-Content C:\scripts\servers.txt
$Report = @()
$vms = Get-VM
foreach ($Server in $ServerList){
$vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{
$report += New-Object PSObject -Property @{
VM_Name = $_.Name
DNS_Name = $_.ExtensionData.Guest.Hostname
}
}
}
$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, that makes perfect sense.
This is already solved here by nicholas1982 and LucD, but I wanted to mention another option: the vNugglets.Utility PowerShell module (of which I am an author) has a cmdlet for getting VMs by VM guest hostname: Get-VNVMByAddress with the -GuestHostname parameter.
Example for a single guest OS hostname:
Get-VNVMByAddress -GuestHostname myguesthostname0
Name GuestHostname MoRef Client
---- ------------- ----- ------
myVMName0 myguesthostname0 VirtualMachine-vm-18927 VMware.Vim.VimClientImpl
Or, for a list of guest OS hostnames:
## iterate through each server name in the file, returning corresponding VMs with given guest OS hostnames
Get-Content C:\scripts\servers.txt | Foreach-Object {Get-VNVMByAddress -GuestHostname $_}
Part of the benefit: that Get-VNVMByAddress cmdlet is using the vCenter SearchIndex object, and can be quicker/more efficient, especially in a larger environment.
And, of course, that module is available in the PowerShellGallery, so getting it is easy with Install-Module or Save-Module.
thanks again all - much appreciated
@LucD
If I just need to pull FQDN of VM's in vCenter, could I just do this?
$Report = @()
$vms = Get-VM
{$vms | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | %{
$report += New-Object PSObject -Property @{
VM_Name = $_.Name
DNS_Name = $_.ExtensionData.Guest.Hostname
}
}
}
$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
Thanks in advance for any help, kind sir 🙂
- DM
I would do it like this.
Don't forget the put a value in the $server variable.
$Report = @()
Get-VM | Where-Object {$_.ExtensionData.Guest.Hostname -like "*$($server)*"} | % {
$report += New-Object PSObject -Property @{
VM_Name = $_.Name
DNS_Name = $_.ExtensionData.Guest.Hostname
}
}
$Report | select VM_Name,DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
hmmm...so for $server, could I do like a gc -path c:\vmlist.txt, with a list of all the VM's in vCenter? I'm sorry man, I'm new to PowerCLI scripting...
- DM
The current setup is that the VMs are filtered by their FQDN.
If the FQDN of a VM matches (in fact if the FQDN contains the string you sopecify in $server), it passes.
But if you want to have all VMs in your vCenter in the report, you can just leave out the Where-clause.
And If you only want to have the VMs in your text file in your report, you could do
$vmNames = Get-Content -Path .\vmlist.txt
Get-VM -Name $vmNames | % {
$report += New-Object PSObject -Property @{
VM_Name = $_.Name
DNS_Name = $_.ExtensionData.Guest.Hostname
}
}
$Report | select VM_Name, DNS_Name | Export-Csv C:\scripts\listvm.csv -NoTypeInformation
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
sweet, thanks man!
- DM
I made one small adjustment and it ran perfect...thanks LucD! I guess it wouldn't be a bad idea to pick up your PowerCLI book for VMware 😉
- DM
I made one small adjustment and it ran perfect...thanks LucD! I guess it wouldn't be a bad idea to pick up your PowerCLI book for VMware 😉
- DM