Skip to content

Commit

Permalink
add function
Browse files Browse the repository at this point in the history
  • Loading branch information
kmaranionjc committed Aug 28, 2023
1 parent d61b935 commit 9d32e17
Showing 1 changed file with 31 additions and 28 deletions.
59 changes: 31 additions & 28 deletions jumpcloud-ADMU/Powershell/Start-Migration.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1378,39 +1378,42 @@ function Test-DATFilePermission {
}
}
}
function DisableScheduledTask {
# Pass in an array of objects with TaskName and TaskPath
function SetScheduledTask {
# Param op "disable" or "enable" then -tasks (array of tasks)
param (
[Parameter(Mandatory = $true)]
[System.Object[]]
$scheduledTasks
)
# Disable tasks before migration
Write-ToLog -message:("Disabling Scheduled Tasks...")
try {
$scheduledTasks | ForEach-Object {
Write-ToLog -message:("Disabling Scheduled Task: $($_.TaskName)")
Disable-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-Null
}
} catch {
Write-ToLog -message:("Failed to disable Scheduled Tasks $($_.Exception.Message)")
}
}
function EnableScheduledTask {
param (
[ValidateSet("disable", "enable")]
[System.String]
$op,
[Parameter(Mandatory = $true)]
[System.Object[]]
$scheduledTasks
)
try {
$scheduledTasks | ForEach-Object {
Write-ToLog -message("Enabling Scheduled Task: $($_.TaskName)")
Enable-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-Null

# Switch op
switch ($op) {
"disable" {
try {
$scheduledTasks | ForEach-Object {
Write-ToLog -message:("Disabling Scheduled Task: $($_.TaskName)")
Disable-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-Null
}
} catch {
Write-ToLog -message:("Failed to disable Scheduled Tasks $($_.Exception.Message)")
}
}
"enable" {
try {
$scheduledTasks | ForEach-Object {
Write-ToLog -message("Enabling Scheduled Task: $($_.TaskName)")
Enable-ScheduledTask -TaskName $_.TaskName -TaskPath $_.TaskPath | Out-Null
}
} catch {
Write-ToLog -message("Could not enable Scheduled Task: $($_.TaskName)") -Level Warn
}
}
} catch {
Write-ToLog -message("Could not enable Scheduled Task: $($_.TaskName)") -Level Warn
}
}
}
#endregion Agent Install Helper Functions
Function Start-Migration {
[CmdletBinding(HelpURI = "https://github.com/TheJumpCloud/jumpcloud-ADMU/wiki/Start-Migration")]
Expand Down Expand Up @@ -1583,7 +1586,7 @@ Function Start-Migration {
$ScheduledTasks = Get-ScheduledTask | Where-Object { $_.TaskPath -notlike "*\Microsoft\Windows*" -and $_.State -ne "Disabled" -and $_.state -ne "Running" }
# Disable tasks before migration
Write-ToLog -message:("Disabling Scheduled Tasks...")
DisableScheduledTask -scheduledTasks $ScheduledTasks
SetScheduledTask -op "disable" -scheduledTasks $ScheduledTasks
}
Process {
# Start Of Console Output
Expand Down Expand Up @@ -2116,7 +2119,7 @@ Function Start-Migration {
}

# re-enable scheduled tasks if they were disabled
EnableScheduledTask -scheduledTasks $ScheduledTasks
SetScheduledTask -op "enable" -scheduledTasks $ScheduledTasks
# Cleanup Folders Again Before Reboot
Write-ToLog -Message:('Removing Temp Files & Folders.')
try {
Expand Down Expand Up @@ -2162,7 +2165,7 @@ Function Start-Migration {
}
$FixedErrors += "$trackedStep"
# Create a list of scheduled tasks that are disabled
EnableScheduledTask -scheduledTasks $ScheduledTasks
SetScheduledTask -op "enable" -scheduledTasks $ScheduledTasks
}

Default {
Expand Down

0 comments on commit 9d32e17

Please sign in to comment.