Good afternoon everyone!
I have a script that reads a csv with several hostnames, connects to vCenter and should get the hostname information, ip and PortGroup to generate a CSV external file.
It happens that many cases vm does not exist in vCenter and returns me the error below:
get-vm : 07/04/2016 14:41:58 | Get-VM | VM with name 'pxl1sso00008' was not found using the specified filter(s). |
No C:\Users\f3135606\Desktop\vmTeste1.ps1:25 caractere:31
+ foreach ($vmName in $vmList) {get-vm $vmName| Select Name, @{N="Network"; e={ $_ ...
+ | ~~~~~~~~~~~~~~ | |
+ CategoryInfo | : ObjectNotFound: (:) [Get-VM], VimException | |
+ FullyQualifiedErrorId : Core_OutputHelper_WriteNotFoundError,VMware.VimAutomation.ViCore.Cmdlets.Commands.GetVM |
How would I do to get the information from an input file, see the vCenter and if this positive results write the file ignoring errors. Ideally, if not find the machine, just create a line in the output file with only the hostname with the rest blank.
Follow .ps1 file:
$vmlist = Get-Content C:\vmnames.csv
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue))
{
Add-PSSnapin VMware*
Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -DefaultVIServerMode multiple -InvalidCertificateAction Ignore -Scope Session -ProxyPolicy NoProxy -Confirm:$false | Out-Null
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) { Add-PSSnapin VMware.VimAutomation.Core }
}
$VCconn = Connect-VIServer $vCenter -User $vUsuario -Password $vPass > $null
foreach ($vmName in $vmList) {get-vm $vmName| Select Name, @{N="Network"; e={ $_ | get-networkadapter|Select-Object @{N="Network";E={$_.NetworkName}}} }, @{N="IP Address";E={@($_.guest.IPAddress[0])}}|Export-Csv –path c:\scripts\vlans.csv –NoTypeInformation}
ePoy,
See below...should get what you want...If the VM is not found it just provides the VM name in the output.
$vmlist = Get-Content C:\vmnames.csv
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware*
Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -DefaultVIServerMode multiple -InvalidCertificateAction Ignore -Scope Session -ProxyPolicy NoProxy -Confirm:$false | Out-Null
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) {
Add-PSSnapin VMware.VimAutomation.Core
}
}
$VCconn = Connect-VIServer $vCenter -User $vUsuario -Password $vPass > $null
$arrVMInfo = @()
foreach ($vmName in $vmList) {
$vm = get-vm $vmName -ErrorAction SilentlyContinue -ErrorVariable VMError | Select Name, @{N="Network";E={ $_ | get-networkadapter | Select-Object @{N="Network";E={$_.NetworkName}}} }, @{N="IPAddress";E={@($_.guest.IPAddress[0])}}
if ($vm -eq $null) {
$arrVMInfo += New-Object PSObject -Property @{ `
Name=$vmName `
}
}
else {
$arrVMInfo += New-Object PSObject -Property @{ `
"Name"=$vm.name; `
"Network"=$vm.Network.Network; `
"IP Address"=$vm.IPAddress `
}
}
}
$arrVMInfo | Select Name, Network, "IP Address" | Export-Csv "c:\scripts\vlans.csv" -NoTypeInformation
ePoy,
See below...should get what you want...If the VM is not found it just provides the VM name in the output.
$vmlist = Get-Content C:\vmnames.csv
if (!(Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) {
Add-PSSnapin VMware*
Set-PowerCLIConfiguration -DisplayDeprecationWarnings $false -DefaultVIServerMode multiple -InvalidCertificateAction Ignore -Scope Session -ProxyPolicy NoProxy -Confirm:$false | Out-Null
[void](Get-PSSnapin VMWare.VimAutomation.Core -ErrorVariable getVmwareSnapinErr 2> $null)
if ($getVmwareSnapinErr.Count -gt 0) {
Add-PSSnapin VMware.VimAutomation.Core
}
}
$VCconn = Connect-VIServer $vCenter -User $vUsuario -Password $vPass > $null
$arrVMInfo = @()
foreach ($vmName in $vmList) {
$vm = get-vm $vmName -ErrorAction SilentlyContinue -ErrorVariable VMError | Select Name, @{N="Network";E={ $_ | get-networkadapter | Select-Object @{N="Network";E={$_.NetworkName}}} }, @{N="IPAddress";E={@($_.guest.IPAddress[0])}}
if ($vm -eq $null) {
$arrVMInfo += New-Object PSObject -Property @{ `
Name=$vmName `
}
}
else {
$arrVMInfo += New-Object PSObject -Property @{ `
"Name"=$vm.name; `
"Network"=$vm.Network.Network; `
"IP Address"=$vm.IPAddress `
}
}
}
$arrVMInfo | Select Name, Network, "IP Address" | Export-Csv "c:\scripts\vlans.csv" -NoTypeInformation
It works exactly the way I needed !!
Tanks!