Get VMware Guest OS List With PowerCLI

Get VMware Guest OS List With PowerCLI

Get VMware Guest OS List With PowerCLI

What it does...

This function I wrote will return an object which contains the unique name of each OS for guests on a vCenter server. It will also show you the total number of VMs with that OS.

What it needs to run...

This function takes the parameter $vCenter. This should be the name of a vCenter server in your environment. You can also call the function as part of a foreach loop if you have multiple vCenter servers, running it once for each server and then returning that into another variable.

The Code:

function Get-VMOSList {
    [cmdletbinding()]
    param($vCenter)
    
    Connect-VIServer $vCenter  | Out-Null
    
    [array]$osNameObject       = $null
    $vmHosts                   = Get-VMHost
    $i = 0
    
    foreach ($h in $vmHosts) {
        
        Write-Progress -Activity "Going through each host in $vCenter..." -Status "Current Host: $h" -PercentComplete ($i/$vmHosts.Count*100)
        $osName = ($h | Get-VM | Get-View).Summary.Config.GuestFullName
        [array]$guestOSList += $osName
        Write-Verbose "Found OS: $osName"
        
        $i++    
 
    
    }
    
    $names = $guestOSList | Select-Object -Unique
    
    $i = 0
    
    foreach ($n in $names) { 
    
        Write-Progress -Activity "Going through VM OS Types in $vCenter..." -Status "Current Name: $n" -PercentComplete ($i/$names.Count*100)
        $vmTotal = ($guestOSList | ?{$_ -eq $n}).Count
        
        $osNameProperty  = @{'Name'=$n} 
        $osNameProperty += @{'Total VMs'=$vmTotal}
        $osNameProperty += @{'vCenter'=$vcenter}
        
        $osnO             = New-Object PSObject -Property $osNameProperty
        $osNameObject     += $osnO
        
        $i++
    
    }    
    Disconnect-VIserver -force -confirm:$false
        
    Return $osNameObject
}

Here is some example output (I tested it within another script I wrote). 
I blacked out the vCenter server name for security reasons.

 

Here is an example of the code I use to wrap it:

In this example the variables that already exist are:

$vCenterServers (this is an array of my vCenter servers)
$vCenter              (this is a parameter of my main script, the value of "all" is what the example below shows)
$getGuestOSList (this is a boolean parameter my script uses. What you see below is what happens if it is $true)

The below code will go through each of the individual vCenter servers and use the function above to build the variable $guestOSObjects with the output.

if (Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager) {Disconnect-VIServer * -confirm:$false -force;Write-Host "Disconnecting VI Server Sessions" -foregroundColor $foregroundColor}

if ($getGuestOSList) {
    
    Switch ($vCenter) {
    {$_ -like "all"} {
        
        $i = 0
        
        Foreach ($v in $vCenterServers) {
           
            $vCenter = $vcenterServers[$i]
            Write-Progress -Activity "Looking up VM Guests" -Status "Current vCenter Server: $vCenter" -PercentComplete ($i/$vCenterServers.Count*100)
            [array]$GuestOSObjects += Get-VMOSList $vCenter
      
            $i++
        
        }
        
    Return $guestOSOBjects
    
    }
    
}

PowerShell: Working with the NetApp module (part 1)

PowerShell: Working with the NetApp module (part 1)

I've been tasked with heading up a storage migration where I am currently employed.

We have both 7-Mode and CDOT systems here (NetApp), and I wanted a way to run different functions with varying options based on each OS. 

To do this I created a couple variables up front:

$accountRun and $NetAppControllers.

$accountRun simply gets the account name you're running PowerShell as.

$accountRun               = (Get-ChildItem ENV:username).Value

$netAppControllers is an array of your controllers.

$global:NetAppControllers = @("controller1","controller2-c")

Notice how controller2 has -c after it. This is used in the below code to delineate between 7-Mode and CDOT controllers.

function Connect-NetAppController {

    $i = 0
    Write-Host `n"NetApp Controllers"`n -ForegroundColor $foregroundColor

    foreach ($n in $NetAppControllers) { 
    
        if ($n.Split("-")[1] -like "*c*"){

            Write-Host $i -NoNewline
            Write-Host -> -NoNewLine -ForegroundColor $foregroundcolor 
            Write-Host $n.Split("-")[0]  -NoNewline
            Write-Host "(CDOT)" -ForegroundColor $foregroundColor

        } else {

            Write-Host $i -NoNewline
            Write-Host -> -NoNewLine -ForegroundColor $foregroundcolor 
            Write-Host $n 

        }

        $i++

}
    Write-Host `n

    $selection = Read-Host "Which #?"
    
    if (($netAppControllers[$selection]).Split("-")[1] -like "*c*") { 
    Write-Host ($netAppControllers[$selection]).Split("-")[0]
        $nController = $netAppControllers[$selection].Split("-")[0]
        
        Write-Host `n"Attempting to connect to: " $nController "(CDOT)"`n -ForegroundColor $foregroundColor
        
        if (Test-Connection $nController -Count 1 -Quiet) {
            
        Write-Host `n"We are able to connect to:" $nController "... gathering credentials."`n -foregroundcolor $foregroundColor
        
        $netAppCredential = Get-Credential -UserName $accountRun.ToLower() -Message "Enter NetApp Credentials for $nController"
       
        Connect-NcController $nController -Credential $netAppCredential | Out-Null

        Get-CDOTInfo $netAppCredential $nController
    
        } Else {
            
            Write-Host `n"Unable to ping: " $nController`n -foregroundcolor Red
            
        }

    } else {

        $7Controller = $NetAppControllers[$selection]
        
        Write-Host `n"Attempting to connect to: " $7Controller`n -ForegroundColor $foregroundColor

        if (Test-Connection $NetAppControllers[$selection] -Count 1 -Quiet) {
        
        $7Controller = $NetAppControllers[$selection]
            
        Write-Host `n"We are able to connect to:" $7Controller "... gathering credentials."`n -foregroundcolor $foregroundColor 
            
        $netAppCredential = Get-Credential -UserName $accountRun.ToLower() -Message "Enter NetApp Credentials for $nController"

        Connect-NaController $NetAppControllers[$selection] -Credential $netAppCredential | Out-Null

        Get-7ModeInfo $netAppCredential $NetAppControllers[$selection]
       
         } Else {
            
            Write-Host `n"Unable to ping: " $7Controller`n -foregroundcolor Red
            
        }
         
     }
  }

Let me know what you think, xor you have any ideas!

PowerShell: Get-DCDiag script for getting domain controller diagnostic information

PowerShell: Get-DCDiag script for getting domain controller diagnostic information

I wrote a script that will run DCDiag on domain controllers that you specify or all DCs in your environment. 

I will be working to improve the script as much as I can. This was a way for me to learn how advanced parameters and object building works in PowerShell.

The "all" and "full" options currently return an object that you can manipulate. The other options will do that soon once I update the script and test it more.

<#   
.SYNOPSIS   
   Display DCDiag information on domain controllers.
.DESCRIPTION 
   Display DCDiag information on domain controllers. $adminCredential and $ourDCs should be set externally.
   $ourDCs should be an array of all your domain controllers. This function will attempt to set it if it is not set via QAD tools.
   $adminCredential should contain a credential object that has access to the DCs. This function will prompt for credentials if not set.
   If the all dc option is used along side -Type full, it will return an object you can manipulate.
.PARAMETER DC 
    Specify the DC you'd like to run dcdiag on. Use "all" for all DCs.
.PARAMETER Type 
    Specify the type of information you'd like to see. Default is "error". You can specify "full"           
.NOTES   
    Name: Get-DCDiagInfo
    Author: Ginger Ninja (Mike Roberts)
    DateCreated: 12/08/2015
.LINK  
    https://www.gngrninja.com/script-ninja/2015/12/29/powershell-get-dcdiag-commandlet-for-getting-dc-diagnostic-information      
.EXAMPLE   
    Get-DCDiagInfo -DC idcprddc1 -Type full
    $DCDiagInfo = Get-DCDiagInfo -DC all -type full -Verbose
#>  
    [cmdletbinding()]
    param(
        [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
        [String]
        $DC,
        
        [Parameter()]
        [ValidateScript({$_ -like "Full" -xor $_ -like "Error"})]
        [String]
        $Type,
        
        [Parameter(Mandatory=$false,ValueFromPipeline=$false)]
        [String]
        $Utility
        )
    
    try {
        
    if (!$ourDCs) {
        
        $ourDCs = get-QADComputer -computerRole 'DomainController'
    
    }
    
    if (!$adminCredential) {
        
        $adminCredential = Get-Credential -Message "Please enter Domain Admin credentials"
        
    }
    
    Switch ($dc) {
    
    {$_ -eq $null -or $_ -like "*all*" -or $_ -eq ""} {
    
        Switch ($type) {  
            
        {$_ -like "*error*" -or $_ -like $null} {  
             
            [array]$dcErrors = $null
            $i               = 0
            
            foreach ($d in $ourDCs){
            
                $name = $d.Name    
                
                Write-Verbose "Domain controller: $name"
                
                Write-Progress -Activity "Connecting to DC and running dcdiag..." -Status "Current DC: $name" -PercentComplete ($i/$ourDCs.Count*100)
                
                $session = New-PSSession -ComputerName $d.Name -Credential $adminCredential
                
                Write-Verbose "Established PSSession..."
                
                $dcdiag  = Invoke-Command -Session $session -Command  { dcdiag }
                
                Write-Verbose "dcdiag command ran via Invoke-Command..."
            
                if ($dcdiag | ?{$_ -like "*failed test*"}) {
                    
                    Write-Verbose "Failure detected!"
                    $failed = $dcdiag | ?{$_ -like "*failed test*"}
                    Write-Verbose $failed
                    [array]$dcErrors += $failed.Replace(".","").Trim("")
            
                } else {
                
                    $name = $d.Name    
                
                    Write-Verbose "$name passed!"
                    
                }
                
                
                Remove-PSSession -Session $session
                
                Write-Verbose "PSSession closed to: $name"
                $i++
            }
            
            Return $dcErrors
        } 
            
        {$_ -like "*full*"}    {
            
            [array]$dcFull             = $null
            [array]$dcDiagObject       = $null
            $defaultDisplaySet         = 'Name','Error','Diag'
            $defaultDisplayPropertySet = New-Object System.Management.Automation.PSPropertySet('DefaultDisplayPropertySet',[string[]]$defaultDisplaySet)
            $PSStandardMembers         = [System.Management.Automation.PSMemberInfo[]]@($defaultDisplayPropertySet)
            $i                         = 0
            
            foreach ($d in $ourDCs){
                
                $diagError = $false
                $name      = $d.Name
                
                Write-Verbose "Domain controller: $name"
                
                Write-Progress -Activity "Connecting to DC and running dcdiag..." -Status "Current DC: $name" -PercentComplete ($i/$ourDCs.Count*100)
                
                $session = New-PSSession -ComputerName $d.Name -Credential $adminCredential
                
                Write-Verbose "Established PSSession..."
                
                $dcdiag  = Invoke-Command -Session $session -Command  { dcdiag }
                
                Write-Verbose "dcdiag command ran via Invoke-Command..."
                
                $diagstring = $dcdiag | Out-String
                
                Write-Verbose $diagstring
                if ($diagstring -like "*failed*") {$diagError = $true}
                
                $dcDiagProperty  = @{Name=$name}
                $dcDiagProperty += @{Error=$diagError}
                $dcDiagProperty += @{Diag=$diagstring}
                $dcO             = New-Object PSObject -Property $dcDiagProperty
                $dcDiagObject   += $dcO
                
                Remove-PSSession -Session $session
                
                Write-Verbose "PSSession closed to: $name"
                
                $i++
            }
            
            $dcDiagObject.PSObject.TypeNames.Insert(0,'User.Information')
            $dcDiagObject | Add-Member MemberSet PSStandardMembers $PSStandardMembers
            
            Return $dcDiagObject
        
            }
        
        }
         break         
    }
   
   
    {$_ -notlike "*all*" -or $_ -notlike $null} {
   
        Switch ($type) {
        
        {$_ -like "*error*" -or $_ -like $null} {
        
            if (Get-ADDomainController $dc) { 
    
                Write-Host "Domain controller: " $dc `n -foregroundColor $foregroundColor
            
                $session = New-PSSession -ComputerName $dc -Credential $adminCredential
                $dcdiag  = Invoke-Command -Session $session -Command  { dcdiag }
       
                if ($dcdiag | ?{$_ -like "*failed test*"}) {
                
                    Write-Host "Failure detected!"
                
                    $failed = $dcdiag | ?{$_ -like "*failed test*"}
                
                    Write-Output $failed 
                
                } else { 
                
                    Write-Host $dc " passed!"
                
                }
                    
            Remove-PSSession -Session $session       
            } 
        }
        
        {$_ -like "full"} {
            
            if (Get-ADDomainController $dc) { 
    
                Write-Host "Domain controller: " $dc `n -foregroundColor $foregroundColor
            
                $session = New-PSSession -ComputerName $dc -Credential $adminCredential
                $dcdiag  = Invoke-Command -Session $session -Command  { dcdiag }
                $dcdiag     
                    
                Remove-PSSession -Session $session       
            }     
                
        }
        
    }
    
    }
    
    }
    
    }
    
    Catch  [System.Management.Automation.RuntimeException] {
      
        Write-Warning "Error occured: $_"
 
        
     }
    
    Finally { Write-Verbose "Get-DCDiagInfo function execution completed."}

You can run this as a script or as a function within a script depending on your needs. Let me know if you have any feedback!