Files
Backup/_NDGOV_WindowsTeam/ITD.Infra-VMware.VirtualMachine/Public/Sync-ITDVMwareVMMetadataToSharePoint.ps1
T
Zack Meier 1d304511b8 update
2026-04-15 15:45:50 -05:00

239 lines
15 KiB
PowerShell

function Sync-ITDVMwareVMMetadataToSharePoint {
[CmdletBinding()]
param (
[string[]]
$VMName,
[switch]
$SRMImplemented,
[switch]
$WhatIf
)
begin {
}
process {
# validate vcenter connection
# search for sharepoint record
# if multiple, error
# get VM (non-placeholder) information
# name, cpu, memoryGB, disks, cluster, datacenter
# compare, discover fields that are mismatched
# if SRMImplement -eq $true, set SP record to match that
# set sharepoint record to values pulled from vcenter
If ($global:DefaultVIServers) {
}
Else {
Write-Error -Message "Not connected to vCenter" -ErrorAction Stop
}
Write-Verbose -Message "Get SharePoint Cluster List"
$SharePointClusterList = Get-ITDSharePointVMClusterList
Write-Verbose -Message "Get SharePoint Guest List"
$SharePointVMList = Get-ITDSharePointVMGuestList
ForEach ($ComputerName in $VMName) {
Write-Verbose -Message "Start $ComputerName"
$RecordToUpdate = $SharePointVMList | Where-Object Title -EQ $ComputerName
$VM = Get-VM -Name $ComputerName | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
switch (@($VM).count) {
{ $_ -le 0 } {
Write-Error -Message "No virtual machine found with name $ComputerName" -ErrorAction Stop
}
{ $_ -gt 1 } {
Write-Error -Message "More than one virtual machine found with name $ComputerName" -ErrorAction Stop
}
{ 1 } {
switch (@($RecordToUpdate).count) {
{ $_ -le 0 } {
Write-Error "SharePoint record with Title $ComputerName not found. Create the new record first." -ErrorAction Stop
}
{ $_ -gt 1 } {
Write-Error "More than one SharePoint record found with Title equal to $ComputerName" -ErrorAction Stop
}
{ 1 } {
# get virtual hard disks and load variables
$VMDisks = $VM | Get-HardDisk
$VMDisk1 = ($VMDisks | Where-Object Name -EQ "Hard Disk 1").CapacityGB
$VMDisk2 = ($VMDisks | Where-Object Name -EQ "Hard Disk 2").CapacityGB
$VMDisk3 = ($VMDisks | Where-Object Name -EQ "Hard Disk 3").CapacityGB
$VMDisk4 = ($VMDisks | Where-Object Name -EQ "Hard Disk 4").CapacityGB
$VMDisk5 = ($VMDisks | Where-Object Name -EQ "Hard Disk 5").CapacityGB
$VMDisk6 = ($VMDisks | Where-Object Name -EQ "Hard Disk 6").CapacityGB
$VMDisk7 = ($VMDisks | Where-Object Name -EQ "Hard Disk 7").CapacityGB
$VMDisk8 = ($VMDisks | Where-Object Name -EQ "Hard Disk 8").CapacityGB
$VMDisk9 = ($VMDisks | Where-Object Name -EQ "Hard Disk 9").CapacityGB
$VMDisk10 = ($VMDisks | Where-Object Name -EQ "Hard Disk 10").CapacityGB
$VMDisk11 = ($VMDisks | Where-Object Name -EQ "Hard Disk 11").CapacityGB
$VMDisk12 = ($VMDisks | Where-Object Name -EQ "Hard Disk 12").CapacityGB
$VMDisk13 = ($VMDisks | Where-Object Name -EQ "Hard Disk 13").CapacityGB
$VMDisk14 = ($VMDisks | Where-Object Name -EQ "Hard Disk 14").CapacityGB
$VMDisk15 = ($VMDisks | Where-Object Name -EQ "Hard Disk 15").CapacityGB
$VMDisk16 = ($VMDisks | Where-Object { $_.Name.split(' ')[2] -notmatch '\b([1-9]|1[0-5])\b' } | Measure-Object -Sum CapacityGB).Sum
If ($null -eq $VMDisk1 ) { $VMDisk1 = 0 }
If ($null -eq $VMDisk2 ) { $VMDisk2 = 0 }
If ($null -eq $VMDisk3 ) { $VMDisk3 = 0 }
If ($null -eq $VMDisk4 ) { $VMDisk4 = 0 }
If ($null -eq $VMDisk5 ) { $VMDisk5 = 0 }
If ($null -eq $VMDisk6 ) { $VMDisk6 = 0 }
If ($null -eq $VMDisk7 ) { $VMDisk7 = 0 }
If ($null -eq $VMDisk8 ) { $VMDisk8 = 0 }
If ($null -eq $VMDisk9 ) { $VMDisk9 = 0 }
If ($null -eq $VMDisk10) { $VMDisk10 = 0 }
If ($null -eq $VMDisk11) { $VMDisk11 = 0 }
If ($null -eq $VMDisk12) { $VMDisk12 = 0 }
If ($null -eq $VMDisk13) { $VMDisk13 = 0 }
If ($null -eq $VMDisk14) { $VMDisk14 = 0 }
If ($null -eq $VMDisk15) { $VMDisk15 = 0 }
If ($null -eq $VMDisk16) { $VMDisk16 = 0 }
# Get VMware Cluster
# what vcenter says, and what it should be
$vCenterCluster = $VM | Get-Cluster
$vCenterSharePointClusterItem = $SharePointClusterList | Where-Object Name -EQ $vCenterCluster.Name
# what sharepoint currently is
$SharePointClusterFieldId = $RecordToUpdate.ClusterId
$SharePointClusterFieldName = $SharePointClusterList | Where-Object Id -EQ $SharePointClusterFieldId
# Get VMware Datacenter
$Datacenter = $VM | Get-Datacenter
switch ($Datacenter.Name) {
'DCN Datacenter' { $DatacenterSharePointName = "DCN" }
'Fargo Datacenter' { $DatacenterSharePointName = "Fargo" }
'Grand Forks Vantis' { $DatacenterSharePointName = "Grand Forks" }
'Primary Datacenter' { $DatacenterSharePointName = "Bismarck" }
'Secondary Datacenter' { $DatacenterSharePointName = "Mandan" }
'VDI Datacenter' { $DatacenterSharePointName = "VDI" }
}
# one sharepoint record and one virtual machine, compare and set params
$SetITDVMwareSharePointVMRecordParams = @{
Title = $ComputerName;
}
[string]$SpecialInstructions = ([string](Get-Date) + " - synchronization from vCenter, the following was changed: ")
If ($VM.NumCPU -ne $RecordToUpdate.Processors) {
$SetITDVMwareSharePointVMRecordParams += @{CPU = $VM.NumCpu }
$SpecialInstructions += ("CPU adjusted from " + $RecordToUpdate.Processors + " to " + $VM.NumCpu + ". ")
}
If ($VM.MemoryGB -ne $RecordToUpdate.RAM) {
$SetITDVMwareSharePointVMRecordParams += @{MemoryGB = $VM.MemoryGB }
$SpecialInstructions += ("MemoryGB adjusted from " + $RecordToUpdate.RAM + " to " + $VM.MemoryGB + ". ")
}
If ($VMDisk1 -ne $RecordToUpdate.Disk_x0020_C_x003a_) {
$SetITDVMwareSharePointVMRecordParams += @{Disk1OS = $VMDisk1 }
$SpecialInstructions += ("Disk1GB adjusted from " + $RecordToUpdate.Disk_x0020_C_x003a_ + " to " + $VMDisk1 + ". ")
}
If ($VMDisk2 -ne $RecordToUpdate.Disk2_x002d_SwapDisk) {
$SetITDVMwareSharePointVMRecordParams += @{Disk2Swap = $VMDisk2 }
$SpecialInstructions += ("Disk2GB adjusted from " + $RecordToUpdate.Disk2_x002d_SwapDisk + " to " + $VMDisk2 + ". ")
}
If ($VMDisk3 -ne $RecordToUpdate.Disk_x0020_D_x003a_) {
$SetITDVMwareSharePointVMRecordParams += @{Disk3 = $VMDisk3 }
$SpecialInstructions += ("Disk3GB adjusted from " + $RecordToUpdate.Disk_x0020_D_x003a_ + " to " + $VMDisk3 + ". ")
}
If ($VMDisk4 -ne $RecordToUpdate.Disk_x0020__x002d__x0020_Other) {
$SetITDVMwareSharePointVMRecordParams += @{Disk4 = $VMDisk4 }
$SpecialInstructions += ("Disk4GB adjusted from " + $RecordToUpdate.Disk_x0020__x002d__x0020_Other + " to " + $VMDisk4 + ". ")
}
If ($VMDisk5 -ne $RecordToUpdate.Disk5) {
$SetITDVMwareSharePointVMRecordParams += @{Disk5 = $VMDisk5 }
$SpecialInstructions += ("Disk5GB adjusted from " + $RecordToUpdate.Disk5 + " to " + $VMDisk5 + ". ")
}
If ($VMDisk6 -ne $RecordToUpdate.Disk6) {
$SetITDVMwareSharePointVMRecordParams += @{Disk6 = $VMDisk6 }
$SpecialInstructions += ("Disk6GB adjusted from " + $RecordToUpdate.Disk6 + " to " + $VMDisk6 + ". ")
}
If ($VMDisk7 -ne $RecordToUpdate.Disk7) {
$SetITDVMwareSharePointVMRecordParams += @{Disk7 = $VMDisk7 }
$SpecialInstructions += ("Disk7GB adjusted from " + $RecordToUpdate.Disk7 + " to " + $VMDisk7 + ". ")
}
If ($VMDisk8 -ne $RecordToUpdate.Disk8) {
$SetITDVMwareSharePointVMRecordParams += @{Disk8 = $VMDisk8 }
$SpecialInstructions += ("Disk8GB adjusted from " + $RecordToUpdate.Disk8 + " to " + $VMDisk8 + ". ")
}
If ($VMDisk9 -ne $RecordToUpdate.Disk9) {
$SetITDVMwareSharePointVMRecordParams += @{Disk9 = $VMDisk9 }
$SpecialInstructions += ("Disk9GB adjusted from " + $RecordToUpdate.Disk9 + " to " + $VMDisk9 + ". ")
}
If ($VMDisk10 -ne $RecordToUpdate.Disk10) {
$SetITDVMwareSharePointVMRecordParams += @{Disk10 = $VMDisk10 }
$SpecialInstructions += ("Disk10GB adjusted from " + $RecordToUpdate.Disk10 + " to " + $VMDisk10 + ". ")
}
If ($VMDisk11 -ne $RecordToUpdate.Disk11) {
$SetITDVMwareSharePointVMRecordParams += @{Disk11 = $VMDisk11 }
$SpecialInstructions += ("Disk11GB adjusted from " + $RecordToUpdate.Disk11 + " to " + $VMDisk11 + ". ")
}
If ($VMDisk12 -ne $RecordToUpdate.Disk12) {
$SetITDVMwareSharePointVMRecordParams += @{Disk12 = $VMDisk12 }
$SpecialInstructions += ("Disk12GB adjusted from " + $RecordToUpdate.Disk12 + " to " + $VMDisk12 + ". ")
}
If ($VMDisk13 -ne $RecordToUpdate.Disk13) {
$SetITDVMwareSharePointVMRecordParams += @{Disk13 = $VMDisk13 }
$SpecialInstructions += ("Disk13GB adjusted from " + $RecordToUpdate.Disk13 + " to " + $VMDisk13 + ". ")
}
If ($VMDisk14 -ne $RecordToUpdate.Disk14) {
$SetITDVMwareSharePointVMRecordParams += @{Disk14 = $VMDisk14 }
$SpecialInstructions += ("Disk14GB adjusted from " + $RecordToUpdate.Disk14 + " to " + $VMDisk14 + ". ")
}
If ($VMDisk15 -ne $RecordToUpdate.Disk15) {
$SetITDVMwareSharePointVMRecordParams += @{Disk15 = $VMDisk15 }
$SpecialInstructions += ("Disk15GB adjusted from " + $RecordToUpdate.Disk15 + " to " + $VMDisk15 + ". ")
}
If ($VMDisk16 -ne $RecordToUpdate.Disk16) {
$SetITDVMwareSharePointVMRecordParams += @{Disk16 = $VMDisk16 }
$SpecialInstructions += ("Disk16GB adjusted from " + $RecordToUpdate.Disk16 + " to " + $VMDisk16 + ". ")
}
If ($vCenterCluster.Name -ne $SharePointClusterFieldName.Name) {
$SetITDVMwareSharePointVMRecordParams += @{ClusterId = $vCenterSharePointClusterItem.Id }
$SpecialInstructions += ("ClusterId adjusted from " + $RecordToUpdate.ClusterId + " to " + $vCenterSharePointClusterItem.Id + ". ")
$SpecialInstructions += ("Cluster adjusted to " + $vCenterSharePointClusterItem.Name + ". ")
}
If ($DatacenterSharePointName -ne $RecordToUpdate.Datacenter) {
$SetITDVMwareSharePointVMRecordParams += @{'Datacenter' = $DatacenterSharePointName }
$SpecialInstructions += ("Datacenter adjusted from " + $RecordToUpdate.Datacenter + " to " + $DatacenterSharePointName + ". ")
}
If($PSBoundParameters.ContainsKey('SRMImplemented')){
$SetITDVMwareSharePointVMRecordParams += @{SRM_Status = 'Implemented'}
$SpecialInstructions += ("SRM_Status set to Implemented. SRM_RecoveryVMtype set to Reserved. SRM_ConfiguredDate set to " + (Get-Date))
}
$SetITDVMwareSharePointVMRecordParams += @{SpecialInstructions = $SpecialInstructions }
If ($WhatIf) {
Write-Warning -Message ("The following fields in SharePoint record #" + $RecordToUpdate.ID + " for Title " + $RecordToUpdate.Title + " would be changed:")
$SetITDVMwareSharePointVMRecordParams
}
Else {
$SetITDVMwareSharePointVMRecordParams
Set-ITDVMwareSharePointVMRecord @SetITDVMwareSharePointVMRecordParams #-Verbose
}
}
}
}
}
}
}
end {
$postParams = [PSCustomObject]@{
AutomationName = "Infra-VMware";
Action = 'Change';
Units = 3;
Platform = 'ServiceNow-Overhead-SharePointDualEntry';
}
Invoke-RestMethod -Uri http://itdnettools.nd.gov/services/automation-tracking.py -Method POST -Body ($postParams | ConvertTo-Json) | Out-Null
}
}