четверг, 31 декабря 2020 г.

Search VMWare VMs and put to CSV - PowerShell

cls

## This script is prepared by Dr.Vlad

# The script searches for VMs by 3 parameters: VM name, Hostname, IP address

# The script outputs the found data in CSV file format, it is usable by itself or as basis for another script which uses it for fast searchin and making actions on found VM

# The script makes use of saved credentials ##


$vCs = 'vcsa-65', 'vcenter-5-5','vcenter-6-014'

$FileName = "V:\SCRIPTS\Scheduled\VM_REPORT.csv"

$FileNameDate = "V:\SCRIPTS\Scheduled\OldDates\VM_REPORT_$(get-date -f yyyy-MM-dd).csv"

$Title = "VMs_REPORT"

Disconnect-VIServer * -Confirm:$false


$PATH = ""

$VC = ""

$VC1 = ""

$VC2 = ""

$VC3 = ""

$User = ""

$Pass = ""

$Report = ""

$VMachines = ""

$Excel_Array =  ""

$VMachines = ""

$VMName = ""

$DNSHostname = ""

$OS = ""

$Nics = ""

$IPAddress = ""

$IP = ""

$VMHost = ""

$CPU = ""

$Memory = ""

$Count = ""

$VM = ""

$VMPowerstate = ""

$VC_IP = ""


$Credentials = Import-Clixml "C:\cred\Credentials_Metropolinet.xml"

$User = $Credentials.UserName

$Pass = $Credentials.GetNetworkCredential().Password


$Excel_Array = New-Object System.Collections.ArrayList


FOREACH ($vC IN $vCs)

{

 

 Connect-VIServer -User $User -Password $Pass $VC


 $Count = (GET-VM | measure).count


 IF($vC -eq "vcsa-65"){$VC_IP = "172.30.1.1"}

 IF($vC -eq "vcenter-5-5"){$VC_IP = "172.30.10.151"}

 IF($vC -eq "vcenter-6-014"){$VC_IP = "172.30.100.146"}


 Write-Host $vC $VC_IP contain $Count VMs! -ForegroundColor GREEN


 $VMachines = Get-View -ViewType Virtualmachine

 


 FOREACH ($VMs IN $VMachines)

 {

    $VMName = $VMs.Name

    $DNSHostname = $VMs.guest.hostname

    $OS = $VMs.Config.GuestFullName

    #$vCenter = ([uri]$VMs.Client.ServiceUrl).Host

    $VMPowerstate = $VMs.RunTime.PowerState

    $Nics = $VMs.summary.config.numEthernetCards

    $IP = $VMs.Guest.Net.ipconfig.ipaddress.ipaddress -join '  '

    $VMHost = (Get-View -Id $VMs.Runtime.Host -Property Name).Name

    $CPU = $VMs.Config.Hardware.numCPU

    $Memory = $VMs.Config.Hardware.memoryMB


    $VMInfo = New-Object -Type PSObject

            $VMInfo | Add-Member -MemberType NoteProperty -Name "VMName" -Value "$VMName" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "DNSHostName" -Value "$DNSHostName" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "VM_State" -Value "$VMPowerstate" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "VM_OS" -Value "$OS" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "vC_NAME" -Value "$VC" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "vC_IP" -Value "$VC_IP" -Force

            $VMInfo | Add-Member -MemberType NoteProperty -Name "VM_IP" -Value "$IP" -Force

                                    

            # $VMInfo | Add-Member -MemberType NoteProperty -Name "VMHost" -Value "$VMHost" -Force

            # $VMinfo | Add-Member -MemberType NoteProperty -Name "Nics" -Value "$Nics" -Force

            # $VMInfo | Add-Member -MemberType NoteProperty -Name "CPU" -Value "$CPU" -Force

            # $VMInfo | Add-Member -MemberType NoteProperty -Name "Memory" -Value "$Memory" -Force

            

            $Excel_Array.Add($VMinfo) | Out-Null


    $VMName = ""

    $DNSHostname = ""

    $OS = ""

    $Nics = ""

    $IPAddress = ""

    $IP = ""

    $VMHost = ""

    $CPU = ""

    $Memory = ""

    $VM = ""

    $VMPowerstate = ""

 }

    Disconnect-VIServer * -Confirm:$false

    $VC_IP = ""

    $VMachines = ""

}


# CD "$CurrentDir"

# $Excel_Array | Out-GridView -Title $Title

$Excel_Array | Export-CSV -path $FileName -NoTypeInformation

$Excel_Array | Export-CSV -path $FileNameDate -NoTypeInformation

$FileNameDate

# ii $FileName

 

Комментариев нет:

Отправить комментарий