Created
November 18, 2019 12:51
-
-
Save CodyMathis123/689f3bb653bd07774691125e5f411003 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
This snippet is for use in a PSADT script. It assumes you have added parameters, or hardcoded the following | |
* $DeploymentID - The deployment ID of the task sequence you want to check the cache for, and which will be invoked | |
* $CheckAC - A boolean (or switch) that determines if you want to force the user to plug in prior to upgrading. This step is only check if | |
the pre-caching has passed | |
#> | |
#region Validate that all content is pre-cached so we do not bother the user until the TS is ready to start | |
$TS = Get-WmiObject -Namespace ROOT\ccm\Policy\Machine\ActualConfig -Class CCM_SoftwareDistribution -Filter "ADV_AdvertisementID='$DeploymentID'" -Property PKG_PackageID, PKG_Name | |
if ($null -ne $TS) { | |
$TS_References = $TS | Where-Object { $_.__Class -eq 'CCM_SoftwareDistribution' } | |
$TS_Info = $TS | Where-Object { $_.__Class -eq 'CCM_TaskSequence' } | |
Write-Log -Message "Identified Task Sequence [Name=$($TS_Info.PKG_Name)] with [References=$($TS_References.PKG_PackageID -join ';')]" | |
} | |
else { | |
Write-Log -Message "Unable to find policy in WMI for [DeploymentID=$DeploymentID]" -Severity 3 | |
Invoke-SCCMTask -ScheduleID EvaluateMachinePolicy | |
Exit-Script -ExitCode 69005 | |
} | |
$ClientCOMObj = New-Object -ComObject "uiresource.uiresourcemgr" | |
$Cache = $ClientCOMObj.GetCacheInfo() | |
$CacheContent = $Cache.GetCacheElements() | |
$CacheInfo = foreach ($Package in $TS_References) { | |
$PackageName = $Package.PKG_Name | |
$CachedStatus = [bool]($CacheContent | Where-Object { $_.ContentID -match $Package.PKG_PackageID }) | |
Write-Log -Message "Identified referenced package [Name=$PackageName] [Cached=$CachedStatus]" | |
[pscustomobject]@{ | |
Name = $PackageName | |
Cached = $CachedStatus | |
} | |
} | |
#endregion Validate that all content is pre-cached so we do not bother the user until the TS is ready to start | |
#region if content is cached, pop deferral option and start task sequence if allowed | |
if ($CacheInfo.Cached -notcontains $false) { | |
## Show Welcome Message, validate at least 20gb free, 30 days to defer | |
Write-Log -Message "All referenced packages are cached, prompting user" | |
Show-InstallationWelcome -AllowDefer -CheckDiskSpace -RequiredDiskSpace 20480 -PersistPrompt -DeferDays $AllowDeferDays -TopMost $true -CustomText | |
if ($CheckAC) { | |
while (-not (Test-Battery)) { | |
if (Get-LoggedOnUser) { | |
Show-InstallationPrompt -Title 'Plugin Power' -Message 'Please attach your power adapter and press OK' -ButtonMiddleText 'OK' -PersistPrompt -MinimizeWindows $true | |
} | |
else { | |
Write-Log -Value "User is not logged in and AC is not attached, exiting script" -Severity 3 | |
Exit-Script 69007 | |
} | |
} | |
} | |
# Retrieve the ScheduleID used for triggering a new required assignment for task sequence | |
$ScheduleID = Get-WmiObject -Namespace "root\ccm\scheduler" -Class "CCM_Scheduler_History" -Filter "ScheduleID LIKE '%$DeploymentID%'" | Where-Object { $_.ScheduleID -notmatch '^DEP-' } | Select-Object -ExpandProperty ScheduleID | |
# Invoke the task sequence | |
if ($ScheduleID) { | |
Invoke-WmiMethod -Namespace "root\ccm" -Class "SMS_Client" -Name "TriggerSchedule" -ArgumentList $ScheduleID -ErrorAction Stop | |
} | |
else { | |
Write-Log -Message "Unable to find Schedule ID in WMI for $DeploymentID" | |
Exit-Script 69008 | |
} | |
} | |
else { | |
Write-Log -Message "Failed to verify that all referenced packages are cached, exiting script" | |
Invoke-SCCMTask -ScheduleID EvaluateMachinePolicy | |
Exit-Script -ExitCode 69006 | |
} | |
#endregion if content is cached, pop deferral option and start task sequence if allowed |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment