function Check-Description { param([string]$ServerName, [switch]$SkipIdentical = $False) #Checking syntax If ($ServerName -eq "") { Write-Warning 'Usage: Check-Description -ServerName "ServerName" [-SkipIdentical:$True]' Throw "Syntax Error" } #Loading functions for setting descriptions function Set-ADDescription { param([string]$ServerName, [string]$Description) Write-Host "Updating AD Description" Set-QADObject $ServerName -Description $Description | Out-Null } function Set-BrowseComment { param([String]$Value, [string]$Description) Write-Host "Updating Browse Comment" $basekey.SetValue($Value,$Description) } function Set-VMDescription { param($VM, [string]$Description) Write-Host "Updating VMware Notes" $VMadv = $VM | Get-View $VMConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec $VMConfigSpec.Annotation = $Description $VMadv.ReconfigVM($VMConfigSpec) } #Variables $HKLM = [Microsoft.Win32.RegistryHive]::LocalMachine $KeyName = "SYSTEM\CurrentControlSet\Services\lanmanserver\parameters" $Writable = $True $ValueName = "srvcomment" #Get reference objects (don't display errors) $ErrorActionPreference = "SilentlyContinue" $VM = Get-VM | Where {$_.Guest.HostName -like "$ServerName.*"} $Server = Get-QADComputer -Name $ServerName -DontUseDefaultIncludedProperties -IncludedProperties Name, Description | Where {$_.Name -eq $ServerName} $Registry = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($HKLM, $ServerName) If ($Registry){$Basekey = $Registry.OpenSubKey($KeyName,$Writable)} $ErrorActionPreference = "Continue" #Build output object $myObj = "" | Select-Object Name, VMDescription, ADDescription, BrowseComment $myObj.Name = $ServerName If ($VM){$myObj.VMDescription = $VM.Description} Else {$myObj.VMDescription = "Not Available"} If ($Server){$myObj.ADDescription = $Server.Description} Else {$myObj.ADDescription = "Not Available"} If ($BaseKey){$myObj.BrowseComment = $BaseKey.GetValue($ValueName)} Else {$myObj.BrowseComment = "Not Available"} #Display output If ($SkipIdentical -and ($myObj.VMDescription -eq "Not Available" -or $myObj.ADDescription -eq "Not Available" -or $myObj.VMDescription -eq $myObj.ADDescription) -and ($myObj.VMDescription -eq "Not Available" -or $myObj.BrowseComment -eq "Not Available" -or $myObj.VMDescription -eq $myObj.BrowseComment) -and ($myObj.BrowseComment -eq "Not Available" -or $myObj.ADDescription -eq "Not Available" -or $myObj.BrowseComment -eq $myObj.ADDescription)) { If ($myObj.VMDescription -ne "Not Available"){$currentdescription = $myObj.VMDescription} ElseIf ($myObj.ADDescription -ne "Not Available"){$currentdescription = $myObj.ADDescription} Else {$currentdescription = $myObj.BrowseComment} If ($servername.length -lt 8){Write-Host "Descriptions are identical for $ServerName `t`t : $currentdescription"} Else {Write-Host "Descriptions are identical for $ServerName `t : $currentdescription"} Clear-Variable currentdescription -ErrorAction SilentlyContinue } Else { #Display Descriptions $myObj | Format-List * #Display Menu $title = "SYNCHRONIZE DESCRIPTIONS" $message = "Choose which description is correct:" $choiceVM = New-Object System.Management.Automation.Host.ChoiceDescription "&VMware", ` "The VMware Notes description is correct." $choiceAD = New-Object System.Management.Automation.Host.ChoiceDescription "&Active Directory", ` "The Active Directory description is correct." $choiceBrowse = New-Object System.Management.Automation.Host.ChoiceDescription "&Browse Comment", ` "The Browse Comment description is correct." $choiceNew = New-Object System.Management.Automation.Host.ChoiceDescription "&New", ` "Type a new description." $choiceSkip = New-Object System.Management.Automation.Host.ChoiceDescription "&Skip", ` "Skips this server." $options = [System.Management.Automation.Host.ChoiceDescription[]]($choiceVM, $choiceAD, $choiceBrowse, $choiceNew, $choiceSkip) $result = $host.ui.PromptForChoice($title, $message, $options, 4) switch ($result) { 0 { Write-Host "Selected VMware Notes description." $correctDescription = $myObj.VMDescription If ($correctDescription -ne "Not Available") { If ($myObj.ADDescription -ne "Not Available"){Set-ADDescription -ServerName $ServerName -Description $correctDescription} If ($myObj.BrowseComment -ne "Not Available"){Set-BrowseComment -Value $ValueName -Description $CorrectDescription} } Else {Write-Warning "Invalid choice. Skipping."} } 1 { Write-Host "Selected Active Directory description." $correctDescription = $myObj.ADDescription If ($correctDescription -ne "Not Available") { If ($myObj.VMDescription -ne "Not Available"){Set-VMDescription -VM $VM -Description $correctDescription} If ($myObj.BrowseComment -ne "Not Available"){Set-BrowseComment -Value $ValueName -Description $CorrectDescription} } Else {Write-Warning "Invalid choice. Skipping."} } 2 { Write-Host "Selected Browse Comment description." $correctDescription = $myObj.BrowseComment If ($correctDescription -ne "Not Available") { If ($myObj.ADDescription -ne "Not Available"){Set-ADDescription -ServerName $ServerName -Description $correctDescription} If ($myObj.VMDescription -ne "Not Available"){Set-VMDescription -VM $VM -Description $correctDescription} } Else {Write-Warning "Invalid choice. Skipping."} } 3 { $correctDescription = Read-Host "New Description" If ((Read-Host "Are you sure? (y/n)") -eq "y") { If ($myObj.VMDescription -ne "Not Available"){Set-VMDescription -VM $VM -Description $correctDescription} If ($myObj.ADDescription -ne "Not Available"){Set-ADDescription -ServerName $ServerName -Description $correctDescription} If ($myObj.BrowseComment -ne "Not Available"){Set-BrowseComment -Value $ValueName -Description $CorrectDescription} } Else {Write-Host "Operation cancelled by user."} } 4 { Write-Host "Skipping the current server." } default { Write-Host "Something went wrong. Skipping this server." } } Write-Host "--------------------" } }