Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # -----------------------------------------------------------
- # FUNCTIONS
- # -----------------------------------------------------------
- # Pull VM info for provided vCenter and output to file
- function GetInfo {
- param( [string]$ServerName, [System.Management.Automation.PSCredential]$Creds )
- # vSphere Server Connection
- $vSphere = Connect-VIServer -Server $ServerName -Credential $Creds
- # Get VM Information
- Get-VM -Server $vSphere | %{
- # Output current VM name to console
- Write-Host $_.Name
- # Build out vSphere folder path
- $loop = $true
- $Path = $_.Folder.Name
- $ParentID = $_.Folder.ParentId
- $Parent = ""
- if ($ParentID -like "Datacenter*" -or $ParentID -eq $null){
- $loop = $false
- } else {
- $Parent = Get-Folder -ID $ParentID
- if ($Parent -eq $null -or $Parent.Name -eq "vm" -or (Get-Folder -ID $ParentID).Name -eq "vm" -or $Parent.Name.Count -gt 1){
- $loop = $false
- }
- }
- while ($loop -eq $true){
- $Fldr = $null
- $Fldr = Get-Folder -ID $ParentID
- if ($Fldr -eq $null){
- $loop = $false
- } else {
- $Path = "$($Fldr.Name)\$Path"
- 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){
- $loop = $false
- } else {
- $ParentID = $Fldr.ParentId
- if ($ParentID -like "Datacenter*" -or $ParentID -eq $null){
- $loop = $false
- }
- }
- }
- }
- if($Path -eq "vm"){
- $Path = ""
- }
- # Get VM tags
- $Tags = $null
- $Tags = Get-TagAssignment -Entity $_ -ErrorAction SilentlyContinue
- if ($Tags -ne $null){
- $Tags = $Tags.Tag -join ','
- }
- # Get VM snapshots
- $Snaps = $null
- $Snaps = Get-Snapshot -VM $_ -ErrorAction SilentlyContinue
- if ($Snaps -ne $null){
- $Snaps = $Snaps.Name -join ','
- }
- $VmInfo = [pscustomobject] @{
- Name = $_.Name
- vCenter = $ServerName
- Cluster = (Get-Cluster -VM $_).Name
- Host = (Get-VMHost -VM $_).Name
- FQDN = $_.Guest.HostName
- State = $_.Guest.State
- OperatingSystem = $_.Guest.OSFullName
- Datastore = (Get-Datastore -VM $_).Name -join ','
- CPUs = $_.NumCpu
- MemoryMB = $_.MemoryMB
- UsedSpaceGB = $_.UsedSpaceGB
- ProvisionedSpaceGB = $_.ProvisionedSpaceGB
- IpAddress = $_.Guest.IPAddress -join ','
- NetworkLabel = (Get-NetworkAdapter -VM $_).NetworkName -join ','
- MacAddress = (Get-NetworkAdapter -VM $_).MacAddress -join ','
- Snapshots = $Snaps
- Tags = $Tags
- Folder = $Path
- Notes = $_.Notes
- #LastLogDate = $DateInLastLine
- }
- Export-Csv -Path $outputFile -InputObject $VmInfo -Append -Encoding UTF8 -Force -NoTypeInformation
- }
- }
- # -----------------------------------------------------------
- # MAIN
- # -----------------------------------------------------------
- # Get start time
- $startTime = Get-Date
- # Add snap-ins for VMware
- #asnp VMware.VimAutomation.Core
- Import-Module VMware.VimAutomation.Core
- # Prepare output file
- $ScriptPath = Split-Path -Parent -Path $MyInvocation.MyCommand.Definition
- $outputFile = "$ScriptPath\$(([io.fileinfo]$MyInvocation.MyCommand.Definition).BaseName)_$(Get-Date -format yyyy-MM-dd).csv"
- New-Item $outputFile -type file -force | out-null
- # Ask for credentials, query specified vCenters
- $C = Get-Credential -Credential $null
- $vCenters = "vCenterA","vCenterB","vCenterC"
- foreach ($vc in $vCenters) {
- GetInfo -ServerName $vc -Creds $C
- }
- $endTime = Get-Date
- Write-Host "EXECUTION TIME IN SECONDS: $(($endTime - $startTime).totalSeconds)"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement