80 lines
3.2 KiB
PowerShell
80 lines
3.2 KiB
PowerShell
<#
|
|
.SYNOPSIS
|
|
Updates ServiceNow SCTask after a Linux VM is deleted
|
|
.DESCRIPTION
|
|
Updates ServiceNow SCTask after a Linux VM is deleted
|
|
.NOTES
|
|
Information or caveats about the function e.g. 'This function is not supported in Linux'
|
|
.EXAMPLE
|
|
Test-MyTestFunction -Verbose
|
|
Explanation of the function or its result. You can include multiple examples with additional .EXAMPLE lines
|
|
#>
|
|
|
|
function Remove-ITDLinuxServerMissingCmdb {
|
|
[CmdletBinding()]
|
|
param (
|
|
[string]
|
|
$SCTaskNum,
|
|
|
|
[string]
|
|
$ComputerName
|
|
)
|
|
|
|
begin {
|
|
|
|
}
|
|
|
|
process {
|
|
# get current user, SCTask, Ritm, custom variables
|
|
$assignTo = Get-ServiceNowRecord -Table 'User' -Filter @('email', '-eq', ($env:username + "@nd.gov"))
|
|
|
|
$SCTask = Get-ServiceNowRecord -Table 'Catalog Task' -ID $SCTaskNum
|
|
$RitmNum = $SCTask.request_item.display_value
|
|
|
|
$Ritm = Get-ServiceNowRecord -Table 'Requested Item' -ID $RitmNum -IncludeCustomVariable
|
|
[string]$SNHostName = ($RITM.CustomVariable | Where-Object Name -EQ server_name).Value
|
|
|
|
Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = "VMware VM Removal for $SNHostName"; assigned_to = $assignTo.name }
|
|
|
|
If ($ComputerName -ne $SNHostName) {
|
|
# false is good
|
|
Write-Error -Message ("ComputerName entered in parameters does not match Host Name field in " + $SCTaskNum) -ErrorAction Stop
|
|
}
|
|
|
|
$VMs = Get-VM -Name $ComputerName -ErrorAction SilentlyContinue | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" }
|
|
switch (@($VMs).count) {
|
|
{ 0 } {
|
|
Write-Warning "$ComputerName not found in vCenter... is it Azure?"
|
|
}
|
|
{ $_ -gt 1 } {
|
|
Write-Warning '-gt 1'
|
|
Write-Error -Message ("Multiple virtual machines with name $ComputerName were found. Are there SRM placeholders? If so, unconfigure SRM and run this again. If there are no placeholders, confirm the virtual machine name.") -ErrorAction Stop
|
|
}
|
|
1 {
|
|
Write-Warning '1'
|
|
$TagAssignment = Get-TagAssignment -Entity $VMs
|
|
$vCenterInfo = $TagAssignment | select Tag, Entity | ConvertTo-Json -Depth 1
|
|
If ($VMs.PowerState -eq 'PoweredOn') {
|
|
#$VMs | Stop-VMGuest -Confirm:$false
|
|
$VMs | Stop-VM -Confirm:$false
|
|
}
|
|
|
|
If ($vCenterInfo) {
|
|
Update-ServiceNowRecord -ID $SCTaskNum -Values @{work_notes = ("vCenter Information: `n " + $vCenterInfo) } # enter work_notes into sctask
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
$CommentsForWorkNotes = ("VMware VM $SNHostName has been deleted. ")
|
|
#$HardwareRemovalDescription = ("$SNHostName hardware is ready for removal.")
|
|
|
|
# if no errors, close sctask
|
|
#$CommentsForWorkNotes += "VMware: Virtual machine named $ComputerName deleted."
|
|
Update-ServiceNowRecord -ID $SCTaskNum -Values @{work_notes = $CommentsForWorkNotes; state = "Closed Complete"; close_notes = $CommentsForWorkNotes}
|
|
}
|
|
|
|
end {
|
|
|
|
}
|
|
} |