VM Compare AD Group Script
July 19th, 2010
No comments
If you apply GPO’s to servers for ease of administration and security, you may want an easy way if all your servers are in the appropriate group(in this case “Virtual Servers”) and compare this list to a list of all running Windows machines. The following script will do this:
########################################################################
# Title: VM Groupmembership Script
# Filename: vmgroup.ps1
# Created by: Douglas Smith
# Date: 7/14/2010
# Version: 0.1
# Description: *This script requires the Quest AD cmdlets be installed.*
########################################################################
#### Pass through paramaeters ###################################################
param (
[string]$vcserver,
)
#### Variables ##############################################################
$Date = Get-Date
#### Functions ##############################################################
#### Begin Script ############################################################
#### Validate Input Variables ####################################################
If ($vcserver -eq "") {
$wscript = new-object -comobject wscript.shell
$msgbox = $wscript.popup("You MUST enter the name or IP of your vSphere server",0,"Access Denied",1)
exit
}
#### Check if Snapin loaded, if not load #############################################
if ((Get-PSSnapin -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue}
if ((Get-PSSnapin -Name Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue) -eq $null )
{Add-PSsnapin Quest.ActiveRoles.ADManagement -ErrorAction SilentlyContinue}
#### Connect to vSphere
$VIServer = connect-VIServer $vcserver
$adlist = get-qadgroupmember "Virtual Servers" -type computer | Select-Object name |
Sort-Object name | ForEach-Object{$_.name.tolower()}
$vms = Get-Vm | Where-Object {$_.powerstate -eq "PoweredOn"} |
Where-Object {$_.name.length -gt 10} |
Where-Object {$_.guest.osfullname -like quot;*indows*"} | Select-Object name |
Sort-Object name | ForEach-Object{$_.Name.ToLower()}
$vmcount = $vms.count
$AddMachine = Compare-Object $vms $adlist -syncwindow $vmcount |
Where-Object {$_.sideindicator -eq "<="} | Sort-Object InputObject
$RemoveMachine = Compare-Object $vms $adlist -syncwindow $vmcount |
Where-Object {$_.sideindicator -eq "=>"} | Sort-Object InputObject
#### Create Spreadsheet
$excel = New-Object -comobject Excel.Application
#$excel.visible = $True #### Use for troubleshooting purposes
$excel.DisplayAlerts = $False
$excelfile = $excel.Workbooks.Add()
$WorkSheet = $excelfile.worksheets.item(1)
$Worksheet.Name = "Add to Group"
$Worksheet.Cells.Item(1, 1) = "Machines that need to be added to the
Group"
$WorkBook = $Worksheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$Row = 2
$Addmachine | ForEach-Object {
$Worksheet.Cells.Item($Row, 1) = $_.InputObject
$Row ++
}
[void]$WorkBook.EntireColumn.AutoFit()
$WorkSheet = $excelfile.worksheets.item(2)
$Worksheet.Name = "Remove from Group"
$Worksheet.Cells.Item(1, 1) = "Machines that need to be removed from the
Group"
$WorkBook = $Worksheet.UsedRange
$WorkBook.Interior.ColorIndex = 8
$WorkBook.Font.ColorIndex = 11
$WorkBook.Font.Bold = $True
$Row = 2
$RemoveMachine | ForEach-Object {
$Worksheet.Cells.Item($Row, 1) = $_.InputObject
$Row ++
}
#### Adjust Columns
[void]$WorkBook.EntireColumn.AutoFit()
#### Save File
$excelfile.SaveAs("c:\temp\VirtualServerGroup-" + $date.month + "-" +
$date.day + "-" + $date.year + ".xls", 1)
#### Quit Excel
$excel.Quit()
#Disconnect from vCenter Server
$VIServer | Disconnect-VIServer -Confirm:$false