This commit is contained in:
Zack Meier
2026-04-15 15:45:50 -05:00
commit 1d304511b8
613 changed files with 140998 additions and 0 deletions
@@ -0,0 +1,131 @@
Param(
[string]
$SCTaskNum
)
New-ITDServiceNowSession -Environment Production -Credential $Secret:SNowVMCred
$Filter = 'active=true^short_descriptionSTARTSWITHAutomated Server Build Task for Windows Machine'
$OpenTasks = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Filter $Filter | Sort-Object Number
If ($PSBoundParameters.ContainsKey("SCTaskNum")) {
Write-Verbose -Message "SCTaskNum parameter found, value is $SCTaskNum" -Verbose
$OpenTasks = $OpenTasks | Where-Object {$_.number.value -EQ $SCTaskNum}
}
$AllRitms = [System.Collections.ArrayList]@()
Write-Verbose -Message ("OpenTasks found: " + @($OpenTasks).Count) -Verbose
ForEach ($OpenTask in $OpenTasks) {
$Ci = $null
$BuildComplete = $null
# get SCTask, Ritm
$SCTaskNum = $OpenTask.number.value
Write-Verbose -Message "Start $SCTasknum" -Verbose
$SCTask = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum
$shortdescription = $SCTask.short_description.display_value
$shortdescription_hostname = $shortdescription.split(' ')[7]
If ($AllRitms | Where-Object sys_id -EQ $SCTask.request_item.value) {
$Ritm = $AllRitms | Where-Object sys_id -EQ $SCTask.request_item.display_value
}
Else {
$Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $SCTask.request_item.display_value -IncludeVariableSet
$null = $AllRitms.Add($Ritm)
}
$ComputerName = ($Ritm.VariableSet | Where-Object host_name -EQ $shortdescription_hostname).host_name
$OperatingSystem = ($Ritm.VariableSet | Where-Object host_name -EQ $shortdescription_hostname).operating_system
switch ( ($Ritm.VariableSet | Where-Object host_name -EQ $shortdescription_hostname).target_platform ) {
'azure' { $target_platform = "Azure" }
'vmware' { $target_platform = "VMware" }
}
$FormFQDN = ($Ritm.VariableSet | Where-Object host_name -EQ $shortdescription_hostname).host_name
$FormHostName = $FormFQDN.split('.')[0]
$Ci = Get-ITDServiceNowRecord -Table cmdb_ci -Filter ("name=" + $FormHostName)
If ($Ci) {
Write-Verbose -Message ("Ci found, sys_id = " + $Ci.sys_id + ", name = " + $Ci.name + ", fqdn = " + $Ci.fqdn) -Verbose
}
Else {
# Ci does not exist
Write-Verbose -Message ("Ci not found") -Verbose
}
switch ($Ci.model_id.display_value) {
{ $_ -like "*VMware*" } { $hardware_platform = "VMware"; $hardware_type = 'Virtual Machine' }
{ $_ -like "*Microsoft Virtual Machine*" } { $hardware_platform = "Azure"; $hardware_type = 'Virtual Machine' }
{ $_ -like "*HP*" } { $hardware_platform = 'HPE'; $hardware_type = 'Physical' }
default { $hardware_platform = 'Other' }
}
Write-Verbose -Message "Confirm all agents are running"
$ProcessList = @('ccmexec', 'cohesity*', 'nessus*', 'cortex*')
switch ($target_platform) {
'VMware' {
$ProcessList += 'vmtoolsd'
}
'Azure' {
Write-Verbose -Message "vmtoolsd not required for Azure VM"
}
Default {
Write-Verbose -Message "no Ci means no platform check"
}
}
try {
#$Secret:itdsccmsrvcpiandgov
$RunningProcess = Invoke-Command -Credential $Secret:itdsccmsrvcpiandgov -ComputerName $FormFQDN -ArgumentList $ProcessList -ErrorAction Stop -ScriptBlock {
Get-Process
}
If ($RunningProcess) {
ForEach ($ProcessName in $ProcessList) {
If ($RunningProcess -match $ProcessName) {
Write-Verbose -Message "Process $ProcessName found." -Verbose
}
Else {
Write-Warning -Message "Process $ProcessName not found"
$BuildComplete = $false
}
}
}
}
catch [System.Management.Automation.Remoting.PSRemotingTransportException] {
Write-Warning -Message "$FormFQDN unreachable via PSRemoting"
$BuildComplete = $false
}
# if Task has been open for more than x hours, update description for humans to review
$Hours = 6
If ($SCTask.opened_at.value -lt (Get-Date).AddHours(-$Hours)) {
$work_notes = ("New build Ci has not been found after $Hours hours, problem may have occurred. Please review.`nPSU Job Id #" + $UAJob.Id)
$shortdescription = "$target_platform $OperatingSystem VM Build for $ComputerName, NEED HUMAN REVIEW"
<#Update-ServiceNowRecord -ID $SCTask.number -Values @{
work_notes = $work_notes;
shortdescription = $shortdescription;
}#>
}
If ($BuildComplete -ne $false) {
Write-Verbose "All required processes running, Windows is ready for use. Update SCTask to notify physical/virtual hardware stakeholders." -Verbose
$work_notes = ("$target_platform $hardware_type $FormFQDN Windows Guest OS complete. `nPSU Job Id #" + $UAJob.Id)
$shortdescription = "$target_platform $hardware_type $FormFQDN Windows Guest OS complete."
Write-Verbose -Message "Work notes: $work_notes" -Verbose
Write-Verbose -Message "Short description: $shortdescription" -Verbose
Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{
work_notes = $work_notes;
close_notes = "$FQDN $target_platform Windows Guest OS complete.";
#short_description = $shortdescription;
state = 'Closed Complete'
}
}
Write-Verbose -Message "End $SCTasknum" -Verbose
}
@@ -0,0 +1,13 @@
Param(
[string]
$ComputerName
)
If ($PSBoundParameters.ContainsKey('ComputerName')) {
$GetITDExpiredFilesParams = @{
ComputerName = $ComputerName;
}
Get-ITDExpiredFilesAuto @GetITDExpiredFilesParams -Credential $Secret:itdsccmsrvcpiandgov -Verbose | Select-Object Name,DirectoryName,Extension,LastWriteTime,Length,PSComputerName
} Else {
Get-ITDExpiredFilesAuto -Credential $Secret:itdsccmsrvcpiandgov -Verbose | Select-Object Name,DirectoryName,Extension,LastWriteTime,Length,PSComputerName
}
@@ -0,0 +1,59 @@
[CmdletBinding()]
param (
[string]
$ComputerName,
[int]
$CPU = 1,
[int]
$MemoryGB = 4,
[int]
$DiskOsGB = 128,
[int]
$DiskDataGB = 0,
[string]
$Subnet,
[string]
$OS,
[string]
$Environment,
[string]
$Subscription,
[string]
$AppName,
[string]
$LicensingRestrictions
)
$params = @{
ComputerName = $ComputerName;
CPU = $CPU;
MemoryGB = $MemoryGB;
DiskOsGB = $DiskOsGB;
DiskDataGB = $DiskDataGB;
Subnet = $Subnet;
OS = $OS;
Environment = $Environment;
Subscription = $Subscription;
AppName = $AppName;
LicensingRestrictions = $LicensingRestrictions;
Credential = $Credential;
}
Write-Verbose -Message "Connect to Azure using Service Principal" -Verbose
$tenantId = '2dea0464-da51-4a88-bae2-b3db94bc0c54'
$AppId = '60244573-7130-4026-9c6d-47de73f8ca29'
$SecureStringPwd = 'Pqt8Q~E-dDmQugcPPWdaK2t_4retS41VVVVOZbOx' # $Secret:AzureVMServicePrincipal # $IaasAutoAzApp
$PSCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $AppId, ($SecureStringPwd | ConvertTo-SecureString -AsPlainText -Force)
Connect-AzAccount -ServicePrincipal -Credential $PSCredential -Tenant $tenantId
New-ITDWindowsVmAzure @params -Credential $PrvCred -Verbose
@@ -0,0 +1,82 @@
Param(
[string]
$ComputerName,
[switch]
$WhatIf,
[switch]
$Quiet
)
$RemoveITDExpiredFilesAutoParams = @{
}
If ($PSBoundParameters.ContainsKey('ComputerName')) {
Write-Verbose -Message "ComputerName parameter"
$RemoveITDExpiredFilesAutoParams += @{ComputerName = $ComputerName }
}
If ($PSBoundParameters.ContainsKey('WhatIf')) {
Write-Verbose -Message "WhatIf parameter"
$RemoveITDExpiredFilesAutoParams += @{WhatIf = $true }
}
$StartTime = (Get-Date)
$FilesRemoved = Remove-ITDExpiredFilesAuto @RemoveITDExpiredFilesAutoParams -Verbose -Credential $Secret:itdsccmsrvcpiandgov
# get information for notes
$DiskBytesRecovered = ($FilesRemoved | Measure-Object -Sum length).Sum
$Notes = "PSComputerName~Length~FullName" + "`n"
$Notes += ForEach ($File in $FilesRemoved) {
If ($File) {
$File.PSComputerName + "~" + $File.Length + "~" + $File.FullName + "`n"
}
}
$Notes += "$DiskBytesRecovered bytes of disk saved."
Write-Verbose -Message "Notes: `n$Notes" -Verbose
$EndTime = (Get-Date)
If ($PSBoundParameters.ContainsKey('Quiet') -and $Quiet -eq $true) {
Write-Verbose -Message "Quiet mode enabled. No ServiceNow interactions will be done." -Verbose
}
Else {
Write-Verbose -Message "Quiet mode disabled. ServiceNow CHG will be generated."
# create std chg and close it
New-ITDServiceNowSession Test -Credential $Secret:SNowVMCred
$NewITDServiceNowChangeRequestParams = @{
TemplateName = 'NDIT-SPS-Server Add/Chg/Del'
RequestedByUsername = 'zmeier';
Category = 'Systems Platforms - Systems';
Subcategory = 'Windows';
Impact = 3;
ShortDescription = "Remove files flagged for expiration and cleanup - Remove-ITDExpiredFilesAuto_script - $UAJobId";
Description = "Remove files flagged for expiration and cleanup";
Justification = "Some files are generated on a recurring basis causing increase in disk space usage. This automation removes specific file types from specified file paths that have been flagged for removal.";
Implementation = "PSUniversal execution";
RiskImpactAnalysis = "Low, files can be discovered before the removal";
BackoutPlan = "Restore from backup (if applicable)"
TestPlan = "n/a"
WhoIsImpacted = "Windows System Administrators";
StartTime = $StartTime
EndTime = $EndTime;
AssignmentGroup = 'NDIT-Computer Systems Windows';
ChangeManagerUsername = 'khellman';
ChangeCoordinatorUsername = 'gpgolberg';
AssignedToUsername = 'zmeier';
}
If ($PSBoundParameters.ContainsKey('WhatIf')) {
$NewITDServiceNowChangeRequestParams.ShortDescription += " -WhatIf"
}
$CHG = New-ITDServiceNowChangeRequest @NewITDServiceNowChangeRequestParams
Update-ITDServiceNowRecord -ItemType "Change Request" -Number $CHG.Number.Value -Values @{
work_notes = $Notes;
}
Complete-ITDServiceNowChangeRequest -Number $CHG.Number.value -CloseCode "Successful" -CloseNotes "Files removed."
}
@@ -0,0 +1,10 @@
Param(
[Parameter(Mandatory = $true)]
[string[]]
$ComputerName
)
ForEach ($cn in $ComputerName) {
Write-Verbose -Message "Attempt Solarwinds removal for $cn" -Verbose
Remove-ITDSolarwindsNode -ComputerName $cn -Credential $Secret:svcitdiaasauto -Verbose
}
@@ -0,0 +1,86 @@
param(
[string]
$SCTaskNum
)
#New-ServiceNowSession -Url 'northdakota.service-now.com' -Credential $Secret:SNowVMCred -Verbose
New-ITDServiceNowSession -Environment Production -Credential $Secret:SNowVMCred
#$Filter = @('assignment_group', '-like', 'NDIT-Server Build Automation'), '-and', @('short_description', '-like', 'VMware Windows Removal for'), '-and', @('state', '-eq', '1')
$Filter = "active=true^short_descriptionLIKEWindows Removal for "
#sysparm_query%3Dactive=true^short_descriptionLIKEWindows Removal for
#$OpenTasks = Get-ServiceNowRecord -Table 'Catalog Task' -Filter $Filter -WarningAction SilentlyContinue | Sort-Object Number
$OpenTasks = Get-ITDServiceNowRecord -ItemType 'Catalog Task' -Filter $Filter -IncludeTotalCount | Sort-Object {$_.Number.value}
If ($PSBoundParameters.ContainsKey("SCTaskNum")) {
Write-Verbose -Message "SCTaskNum parameter found, value is $SCTaskNum"
$OpenTasks = $OpenTasks | Where-Object {$_.number.value -EQ $SCTaskNum}
}
$AllRitms = [System.Collections.ArrayList]@()
Write-Verbose -Message ("Number of OpenTasks is " + @($OpenTasks).count) -Verbose
Connect-ITDvCenter -Credential $Secret:svcitdiaasauto
ForEach ($OpenTask in $OpenTasks) {
# get SCTask, Ritm
$SCTask = $OpenTask
$SCTaskNum = $OpenTask.number.display_value
Write-Verbose -Message ("Start " + $SCTaskNum) -Verbose
$short_description = $SCTask.short_description.display_value
$short_description_hostname = $short_description.split(' ')[4]
$RitmNum = $SCTask.request_item.display_value
If ($AllRitms | Where-Object sys_id -EQ $SCTask.request_item.value) {
$Ritm = $AllRitms | Where-Object sys_id -EQ $SCTask.request_item.display_value
}
Else {
$Ritm = Get-ITDServiceNowRecord -ItemType 'Request Item' -Number $SCTask.request_item.display_value -IncludeVariableSet
$null = $AllRitms.Add($Ritm)
}
#$Ci = Get-ITDServiceNowRecord -Table cmdb_ci -Filter ("name=" + $short_description_hostname)
#$Ci = Get-ITDServiceNowRecord -Table cmdb_ci -SysId ($Ritm.VariableSet | Where-Object .host_name_ref) -ErrorAction Stop
Write-Verbose -Message "Gathering VariableSet data from $RitmNum"
$MatchFound = $false
ForEach ($Row in $Ritm.VariableSet) {
$TempCi = Get-ITDServiceNowRecord -Table cmdb_ci -SysId ($Row.host_name_ref) -ErrorAction Stop
If ($short_description_hostname -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
}
$HostName = $Ci.Name.display_value
$FQDN = $Ci.FQDN.display_value
Write-Verbose -Message ("Ci Name " + $Ci.Name.display_value) -Verbose
Write-Verbose -Message ("Ci FQDN " + $Ci.FQDN.display_value) -Verbose
switch ($Ci.model_id.display_value) {
{ $_ -like "*VMware*" } { $hardware_platform = "VMware"; $hardware_type = 'Virtual Machine' }
{ $_ -like "*Microsoft Virtual Machine*" } { $hardware_platform = "Azure"; $hardware_type = 'Virtual Machine' }
{ $_ -like "*HP*" } { $hardware_platform = 'HPE'; $hardware_type = 'Physical' }
default { $hardware_platform = 'Unknown'; $hardware_type = 'Other' }
}
try {
Write-Verbose -Message "Start Removal of $FQDN, $hardware_platform $hardware_type" -Verbose
Remove-ITDWindowsServer -ComputerName $FQDN -SCTaskNum $SCTaskNum -Credential $Secret:svcitdiaasauto -Verbose # $Secret:svcitdiaasauto
#$short_description_new = "$hardware_platform $hardware_type $FQDN is ready for removal."
#Update-ServiceNowRecord -ID $SCTask.number -Values @{short_description = $short_description; }
#Update-ITDServiceNowRecord -ItemType 'Catalog Task' -Number $SCTaskNum -Values @{short_description = $short_description_new}
Write-Verbose -Message "End Removal of $FQDN" -Verbose
}
catch {
Write-Error $error[0]
}
Write-Verbose -Message "End $SCTasknum" -Verbose
}
Disconnect-ITDvCenter