Skip to content

Instantly share code, notes, and snippets.

View CodyMathis123's full-sized avatar
🦖
Working from home

Cody Mathis CodyMathis123

🦖
Working from home
View GitHub Profile
$Remediate = $false
$PathShouldBeROOT = Get-ItemProperty -Path "registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Update Services\Server\Setup" -Name ContentDir | Select-Object -ExpandProperty ContentDir
$PathShouldBe = Join-Path -Path $PathShouldBeROOT -ChildPath 'WSUSContent'
[Void][Reflection.Assembly]::LoadWithPartialName("Microsoft.Web.Administration")
$serverManager = New-Object Microsoft.Web.Administration.ServerManager
$site = $serverManager.Sites | Where-Object { $_.Name -eq "WSUS Administration" }
$rootApp = $site.Applications | Where-Object { $_.Path -eq "/" }
$rootVdir = $rootApp.VirtualDirectories | Where-Object { $_.Path -eq "/Content" }
#region detection/remediation
#region define variables
$Remediate = $false
#endregion define variables
try {
$WSUS_Server = Get-WsusServer -ErrorAction Stop
switch ($WSUS_Server -is [Microsoft.UpdateServices.Internal.BaseApi.UpdateServer]) {
$true {
$WSUS_Port = $WSUS_Server | Select-Object -ExpandProperty PortNumber
Set-NetTCPSetting -SettingName InternetCustom -CongestionProvider LEDBAT
New-NetTransportFilter -SettingName InternetCustom -LocalPortStart 8530 -LocalPortEnd 8530 -RemotePortStart 0 -RemotePortEnd 65535
/*
This query will show you the count of records per Hardware Inventory class in your HinvChangeLog table for a period between
21 days ago, and 28 days ago
*/
SELECT map.DisplayName
, map.InvClassName AS 'Inventory View'
, COUNT(HINV.RecordID)
FROM HinvChangeLog hinv
LEFT JOIN v_GroupMap map ON map.GroupID = hinv.GroupKey
WHERE hinv.TimeKey > DATEADD(day, -28, GETDATE()) AND hinv.TimeKey < DATEADD(day, -21, GETDATE())
/*
This query will show you the count of records per Hardware Inventory class in your HinvChangeLog table.
This can help you identify a class that is create a lot of hardware inventory 'traffic' and potentially bloating
your database.
*/
SELECT map.DisplayName
, map.InvClassName AS 'Inventory View'
, COUNT(HINV.RecordID)
FROM HinvChangeLog hinv
LEFT JOIN v_GroupMap map ON map.GroupID = hinv.GroupKey
if ($Include -ne @{ }) {
Write-CMLogEntry -Value "Marking folders [$($Include.Keys -join '; ')] for inclusion in dedplucation - will process $DrivePath"
$AllFolders = Get-ChildItem -Path $DrivePath -Directory
$Exclude = $AllFolders.FullName | Where-Object { $_ -notin $Include.Keys }
$Excludes = $Exclude -replace $DrivePath
switch ($No_SMS_Exists) {
$false {
Write-CMLogEntry -Value "Found that the 'No_SMS_On_Drive.sms' does not exist on $DrivePath - will check for DP folders."
$SMS_PackageShareFolder = [string]::Format('SMSPKG{0}$', $Volume.DriveLetter)
$SMS_PackageShareFolderPath = Get-ChildItem -Path $DrivePath -Filter $SMS_PackageShareFolder
if ($null -ne $SMS_PackageShareFolderPath) {
Write-CMLogEntry -Value "Adding $($SMS_PackageShareFolderPath.FullName) to inclusion list for $DrivePath"
$Include[$SMS_PackageShareFolderPath.FullName] = $true
}
$SCCMContentLibFolderPath = Get-ChildItem -Path $DrivePath -Filter 'SCCMContentLib'
$TimeFrame = (Get-Date).AddMinutes(-17)
$Filter = @{
LogName = 'System'
StartTime = $TimeFrame
Id = '19'
ProviderName = 'Microsoft-Windows-WindowsUpdateClient'
}
$Events = Get-WinEvent -FilterHashtable $Filter
foreach ($Event in $Events) {
switch -Regex ($Event.Message) {
powershell.exe -command "&{$TimeFrame=(Get-Date).AddMinutes(-17);$Filter=@{LogName='System';StartTime=$TimeFrame;Id='19';ProviderName='Microsoft-Windows-WindowsUpdateClient';};$Events=Get-WinEvent -FilterHashtable $Filter;foreach($Event in $Events){switch -Regex ($Event.Message){'Servicing Stack Update'{foreach($Schedule in @('108','113')){$ScheduleString = [string]::Format('{{00000000-0000-0000-0000-000000000{0}}}',$Schedule);$invokeWmiMethodSplat=@{Name='TriggerSchedule';Namespace='root\ccm';Class='sms_client';ArgumentList=$ScheduleString;ErrorAction='Stop';};Invoke-WmiMethod @invokeWmiMethodSplat;}}}}}"
$getWmiObjectSplat = @{
Query = "SELECT LocalizedDisplayName FROM SMS_SoftwareUpdate WHERE LocalizedDisplayName LIKE 'Office 365 Client Update - $Channel%$Bitness%' AND IsDeployed = '1' AND IsLatest = '1'"
ComputerName = $SMSProvider
Namespace = "root\sms\site_$SiteCode"
}
$365Patches = Get-WmiObject @getWmiObjectSplat | Select-Object -ExpandProperty LocalizedDisplayName
if ($365Patches.Count -gt 0) {
$Regex = "\(Build ([0-9]+\.[0-9]+)\)"
[double[]]$Builds = foreach ($Value in $365Patches) {
[regex]::Match($Value, $Regex).Groups[1].Value