-1

I want to get all the information from ESXi. I extracted the information in different CSV files, but once I want to merge them, it does not show all. But I would rather to create foreach to gather same information.

Add-PsSnapin VMware.VimAutomation.Core -ErrorAction  "SilentlyContinue"
Import-Module ‚C:\Program Files\Microsoft Virtual Machine Converter\MvmcCmdlet.psd1‘

$datetime = Get-Date -Format "ddMMyyyy";

# Configuration Block
$User =  
$Password =  
$ESXiServer = "172.17.1.171"

# Connect to ESXi
$PWD = ConvertTo-SecureString -AsPlainText -Force -String $Password;
$SourceCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User,$PWD; 
$sourceConnection = New-MvmcSourceConnection -Server $ESXiServer -SourceCredential $sourceCredential

$SourceVMName = (Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection).Name
$Datacenter = Get-Datacenter
$Datastore = Get-Datastore
$DataStoreLocation = $Datastore.ExtensionData.info.url  
$Datastore = Get-Datastore

# Get-VMHostNetworkAdapter | fl *
Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, DHCPEnabled, DeviceName  | Export-Csv C:\VMHostNetworkDetails_$datetime.csv -Delimiter ";"
Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection | select MemorySizeBytes, OperatingSystem, UsedSpacebytes | Export-Csv C:\VMRAmDetails_$datetime.csv -Delimiter ";"
$Global:DefaultVIServers | Select ProductLine,Version,Build, Port | Export-Csv C:\GlobalDetails_$datetime.csv -Delimiter ";"
@(Import-Csv C:\VMHostNetworkDetails_$datetime.csv) + @(Import-Csv C:\VMRAmDetails_$datetime.csv) + @(Import-Csv C:\GlobalDetails_$datetime.csv) | Export-Csv C:\ESxiDetails_$datetime.csv -Delimiter ";"

Note: get-vm does not work for me.

EDIT:
I tried to get the info by using foreach loop, but cannot get IP, SubnetMask, Mac, DHCPEnabled.$VMSysInfo.IPAddressdoes not give me any IP, but Get-VMHostNetworkAdapter | select VMhost, Name, IP, SubnetMask, Mac, DHCPEnabled, DeviceName gives me IP.

$VmInfo = vmware.vimautomation.core\Get-VM
$VMS = ($VmInfo).Name
$VCenter = @()
foreach ($VM in $VMS)
{
    $HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
    $VMSysInfo = Get-VMGuest -VM $VM
    $MyObject = New-Object PSObject -Property @{
    VMName = $VM
    #VMHostName = $VMSysInfo.HostName
    VMIP = $VMSysInfo.IPAddress
    VMInstalledOS = $VMSysInfo.OSFullName
    PowerState = ($VmInfo | ? {$_.Name -eq $VM}).PowerState
    NumberOfCPU = ($VmInfo | ? {$_.Name -eq $VM}).NumCpu
    MemoryGB = (($VmInfo | ? {$_.Name -eq $VM}).MemoryMB/1024)
    VMDataS = (Get-Datastore -VM $VM).Name
    #HostServer = (($VmInfo | ? {$_.Name -eq $VM}).Host).Name
    #HostCluster = (Get-Cluster -VMHost $HostServer).Name
    Datacenter = (Get-Datacenter -VM $vm).Name
    #Notes =   $vm | Select -ExpandProperty Description
    Portgroup = (Get-VirtualPortGroup -VM $vm).Name
    }
     $VCenter += $MyObject
        }
    $VCenter | Select VMName,
    @{N='VMIPAddress';E={$_.VMIP -join '; '}},
    VMInstalledOS, PowerState, NumberOfCPU, MemoryGB,
    @{N='VMDataStore';E={$_.VMDataS -join '; '}},
    HostServer, HostCluster,Datacenter, Notes, Portgroup |
    Export-Csv C:\test.csv -NoTypeInformation -Delimiter ";"
frhling1
  • 55
  • 1
  • 6
  • 11
  • "Does not show" "does not work" "I am not able to" - please read [How to ask a good question](http://stackoverflow.com/help/how-to-ask) and edit your question with enough detail that someone might be able to help you. – TessellatingHeckler Jul 18 '16 at 15:54
  • Please read carefully. My question is clear. I have created 3 CSV files, each give me correct info. just want to know how to get the same information by using foreach loop. – frhling1 Jul 18 '16 at 19:59
  • I guess `Get-VM` doesn't work because of the naming clash with the Hyper-V command. If you use the full name including the module/snapin prefix `vmware.vimautomation.core\Get-VM` that might work. Anyway - it's not clear how three different tables can fit in one CSV - what you want it to look like. It's not CSV data if you chain them together. (`Get-Content a.csv,b.csv,c.csv | Set-Content out.csv`). It's not clear what you intend your `foreach` to loop over. It's not clear how/why a foreach loop can gather the same information yet give a different result. – TessellatingHeckler Jul 19 '16 at 05:47
  • first Thanks. `vmware.vimautomation.core\Get-VM` worked. `out.csv` also worked but just the problem ist: 1- the headers are in 3 different lines but what I want, is that all headers in one line. 2- I want to get the same info as in out.csv by using foreach loop. It is like a practice for me. – frhling1 Jul 19 '16 at 12:59

1 Answers1

0

I changed the foreach loop and it works:

$VmInfo = vmware.vimautomation.core\Get-VM
#$VmInfo = (Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection).Name
$VMS = ($VmInfo).Name
$Data = @()
foreach ($VM in $VMS)
{
    $Datacenter = Get-Datacenter
    $Datastore = Get-Datastore
    $SourceIP =  ($global:DefaultVIServer).name
    $DataStoreLocation = $Datastore.ExtensionData.info.url  

    $VMNetwork = Get-VMHostNetworkAdapter
    $PortalGroup = Get-VirtualPortGroup -VM $vm
    $MvmcSourceVirtualMachine = Get-MvmcSourceVirtualMachine -SourceConnection $sourceconnection

    $VMCustom = New-Object System.Object

    $VMCustom | Add-Member -Type NoteProperty -Name DataCenter -Value $Datacenter.Name
    $VMCustom | Add-Member -Type NoteProperty -Name DataStoreName -Value $Datastore.Name
    $VMCustom | Add-Member -Type NoteProperty -Name DataStoreLocation -Value $DataStoreLocation
    $VMCustom | Add-Member -Type NoteProperty -Name NumberOfCPU -Value $VmInfo.NumCpu
    $VMCustom | Add-Member -Type NoteProperty -Name PowerState -Value $VmInfo.PowerState
    $VMCustom | Add-Member -Type NoteProperty -Name MemoryGB -Value $VmInfo.MemoryGB
    $VMCustom | Add-Member -Type NoteProperty -Name VMHost -Value $VMNetwork.VMhost
    $VMCustom | Add-Member -Type NoteProperty -Name DHCP -Value $VMNetwork.DHCPEnabled
    $VMCustom | Add-Member -Type NoteProperty -Name SubnetMask -Value $VMNetwork.SubnetMask
    $VMCustom | Add-Member -Type NoteProperty -Name Client -Value $VMNetwork.Client
    $VMCustom | Add-Member -Type NoteProperty -Name IP -Value $SourceIP
    $VMCustom | Add-Member -Type NoteProperty -Name MacAddress -Value $VMNetwork.Mac
    $VMCustom | Add-Member -Type NoteProperty -Name PortalGroupName -Value $PortalGroup.Name
    $VMCustom | Add-Member -Type NoteProperty -Name OperatingSystem -Value $MvmcSourceVirtualMachine.OperatingSystem

    $Data += $VMCustom
}

$Data   | Export-CSV "C:\ESXiInfo.csv" -Delimiter ";" -NoTypeInformation
frhling1
  • 55
  • 1
  • 6
  • 11