<#
.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-ITDVMviaSNowTask {
    [CmdletBinding()]
    param (
        [string]
        $SCTaskNum,

        [string]
        $ComputerName
    )
    
    begin {

    }
    
    process {
        # get current user, SCTask, Ritm, custom variables
        switch ($env:username) {
            'svcitdiaasauto' {
                $assignTo = Get-ITDServiceNowUser -Username svcvmwareadm
            }
            Default {
                $assignTo = Get-ITDServiceNowUser -Username $Env:username
            }
        }

        $SCTask = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum
        $RitmNum = $SCTask.request_item.display_value

        Write-Verbose -Message "Retrieve $RitmNum and its VariableSet"
        $Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $RitmNum -IncludeVariableSet -ErrorAction Stop

        ###### the name in the $ComputerName parameter must match a name in the form to continue
        Write-Verbose -Message "Looking for a match of `$ComputerName $ComputerName and one of the rows' `$TempCi.FQDN.display_value"
        $MatchFound = $false
        ForEach ($Row in $Ritm.VariableSet) {
            $TempCi = Get-ITDServiceNowRecord -Table cmdb_ci -SysId ($Row.host_name_ref) -ErrorAction Stop
            If ($ComputerName -eq $TempCi.FQDN.display_value) {
                $Ci = $TempCi
                $MatchFound = $true
            }
        }

        If ($MatchFound -eq $false) {
            Write-Error -Message "ComputerName $ComputerName was not found in VariableSet for $RitmNum" -ErrorAction Stop
        }

        Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTask.number.display_value -Values @{assigned_to = $assignTo.name }

        $FQDN = $Ci.fqdn.display_value
    
        switch ( $Ci.model_id.display_value ) {
            { $_ -like "*VMware*" } { 
                $hardware_platform = "VMware";
                $hardware_type = 'Virtual Machine' 

                $VMs = Get-VM -Name $FQDN -ErrorAction SilentlyContinue | Where-Object { $_.ExtensionData.summary.config.ManagedBy.Type -ne "placeholderVm" } 
                switch ( @($VMs).count ) {
                    { 0 } {
                        Write-Warning "$FQDN not found in vCenter... is it Azure?  Or does it not exist?"
                    }
                    { $_ -gt 1 } {
                        Write-Verbose -Message "Multiple virtual machines with name $FQDN were found."
                        Write-Error -Message ("Multiple virtual machines with name $FQDN 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-Verbose -Message "One virtual machine with name $FQDN were found."

                        If ($VMs.PowerState -eq 'PoweredOff') {
                            # do nothing
                        }
                        Else {
                            Write-Error -Message "VMware VM $FQDN is still powered on.  "
                        }

                        $TagAssignment = Get-TagAssignment -Entity $VMs
                        $vCenterInfo = $TagAssignment | select Tag, Entity | ConvertTo-Json -Depth 1
                        If ($VMs.PowerState -eq 'PoweredOn') {
                            Write-Verbose -Message "Power off VMware VM $FQDN"
                            $CommentsForWorkNotes += ("`nVMware: VM $FQDN has been powered off.  ")
                            $VMs | Stop-VM -Confirm:$false
                        }

                        #If ($vCenterInfo) {
                        # enter work_notes into sctask
                        Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
                            work_notes = ("vCenter Information: `n " + $vCenterInfo)
                        }
                        #}
                    }
                }
            }
            { $_ -like "*Microsoft Virtual Machine*" } { 
                $hardware_platform = "Azure";
                $hardware_type = 'Virtual Machine' 
            }
            { $_ -like "*HP*" } { 
                $hardware_platform = 'HPE';
                $hardware_type = 'Physical' 
            }
            default { $hardware_platform = 'Other' }
        }

        $CommentsForWorkNotes = ("$hardware_platform $hardware_type $FQDN has been deleted.  ")

        # Set SharePoint status to "Delete"
        #Set-ITDVMwareSharePointVMRecord -Title $FQDN -Status Delete -Verbose

        Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
            work_notes  = $CommentsForWorkNotes;
            state       = 'Closed Complete';
            close_notes = $CommentsForWorkNotes
        }
    }
    
    end {
        
    }
}