Advertisement
Guest User

Untitled

a guest
Nov 15th, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # -----------------------------------------------------------
  2. # FUNCTIONS
  3. # -----------------------------------------------------------
  4.  
  5. # Pull VM info for provided vCenter and output to file
  6. function GetInfo {
  7.     param( [string]$ServerName, [System.Management.Automation.PSCredential]$Creds )
  8.  
  9.     # vSphere Server Connection
  10.     $vSphere = Connect-VIServer -Server $ServerName -Credential $Creds
  11.  
  12.     # Get VM Information
  13.     Get-VM -Server $vSphere | %{
  14.         # Output current VM name to console
  15.         Write-Host $_.Name
  16.    
  17.         # Build out vSphere folder path
  18.         $loop = $true
  19.         $Path = $_.Folder.Name
  20.         $ParentID = $_.Folder.ParentId
  21.         $Parent = ""
  22.         if ($ParentID -like "Datacenter*" -or $ParentID -eq $null){
  23.             $loop = $false
  24.         } else {
  25.             $Parent = Get-Folder -ID $ParentID
  26.             if ($Parent -eq $null -or $Parent.Name -eq "vm" -or (Get-Folder -ID $ParentID).Name -eq "vm" -or $Parent.Name.Count -gt 1){
  27.                 $loop = $false
  28.             }
  29.         }
  30.         while ($loop -eq $true){
  31.             $Fldr = $null
  32.             $Fldr = Get-Folder -ID $ParentID
  33.             if ($Fldr -eq $null){
  34.                 $loop = $false
  35.             } else {
  36.                 $Path = "$($Fldr.Name)\$Path"
  37.                 if ($Fldr.Parent -eq $null -or $Fldr.Parent.Name -eq "vm" -or (Get-Folder -ID $ParentID).Name -eq "vm" -or $Fldr.Parent.Name.Count -gt 1){
  38.                     $loop = $false
  39.                 } else {
  40.                     $ParentID = $Fldr.ParentId
  41.                     if ($ParentID -like "Datacenter*" -or $ParentID -eq $null){
  42.                         $loop = $false
  43.                     }
  44.                 }
  45.             }
  46.         }
  47.         if($Path -eq "vm"){
  48.             $Path = ""
  49.         }
  50.        
  51.        
  52.         # Get VM tags
  53.         $Tags = $null
  54.         $Tags = Get-TagAssignment -Entity $_ -ErrorAction SilentlyContinue
  55.         if ($Tags -ne $null){
  56.             $Tags = $Tags.Tag -join ','
  57.         }
  58.        
  59.         # Get VM snapshots
  60.         $Snaps = $null
  61.         $Snaps = Get-Snapshot -VM $_ -ErrorAction SilentlyContinue
  62.         if ($Snaps -ne $null){
  63.             $Snaps = $Snaps.Name -join ','
  64.         }
  65.    
  66.         $VmInfo = [pscustomobject] @{
  67.                         Name                    = $_.Name
  68.                         vCenter                 = $ServerName
  69.                         Cluster                 = (Get-Cluster -VM $_).Name
  70.                         Host                    = (Get-VMHost -VM $_).Name
  71.                         FQDN                    = $_.Guest.HostName
  72.                         State                   = $_.Guest.State
  73.                         OperatingSystem         = $_.Guest.OSFullName
  74.                         Datastore               = (Get-Datastore -VM $_).Name -join ','
  75.                         CPUs                    = $_.NumCpu
  76.                         MemoryMB                = $_.MemoryMB
  77.                         UsedSpaceGB             = $_.UsedSpaceGB
  78.                         ProvisionedSpaceGB      = $_.ProvisionedSpaceGB
  79.                         IpAddress               = $_.Guest.IPAddress -join ','
  80.                         NetworkLabel            = (Get-NetworkAdapter -VM $_).NetworkName -join ','
  81.                         MacAddress              = (Get-NetworkAdapter -VM $_).MacAddress -join ','
  82.                         Snapshots               = $Snaps
  83.                         Tags                    = $Tags
  84.                         Folder                  = $Path
  85.                         Notes                   = $_.Notes
  86.                         #LastLogDate                = $DateInLastLine
  87.         }
  88.                    
  89.        
  90.         Export-Csv -Path $outputFile -InputObject $VmInfo -Append -Encoding UTF8 -Force -NoTypeInformation
  91.     }
  92. }
  93.  
  94. # -----------------------------------------------------------
  95. # MAIN
  96. # -----------------------------------------------------------
  97.  
  98. # Get start time
  99. $startTime = Get-Date
  100.  
  101. # Add snap-ins for VMware
  102. #asnp VMware.VimAutomation.Core
  103. Import-Module VMware.VimAutomation.Core
  104.  
  105. # Prepare output file
  106. $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
  107. $outputFile = "$ScriptPath\$(([io.fileinfo]$MyInvocation.MyCommand.Definition).BaseName)_$(Get-Date -format yyyy-MM-dd).csv"
  108. New-Item $outputFile -type file -force | out-null
  109.  
  110. # Ask for credentials, query specified vCenters
  111. $C = Get-Credential -Credential $null
  112. $vCenters = "vCenterA","vCenterB","vCenterC"
  113. foreach ($vc in $vCenters) {
  114.     GetInfo -ServerName $vc -Creds $C
  115. }
  116.  
  117. $endTime = Get-Date
  118. Write-Host "EXECUTION TIME IN SECONDS: $(($endTime - $startTime).totalSeconds)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement