Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Module with Add-WindowsCapability proxy function to which allows bypassing WSUS for Add-WindowsCapability.
# iex ((New-Object System.Net.WebClient).DownloadString('https://gist.githubusercontent.com/realslacker/fc8784324eea99e7aea48e54d8847704/raw'))
#Requires -Modules DISM
function Add-WindowsCapability {
[CmdletBinding()]
param(
[Parameter(ParameterSetName='Online', ValueFromPipelineByPropertyName=$true)]
[Parameter(ParameterSetName='Offline', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]
${Name},
[Parameter(ParameterSetName='Online', ValueFromPipelineByPropertyName=$true)]
[Parameter(ParameterSetName='Offline', ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string]
${Recipe},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[switch]
${LimitAccess},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[ValidateNotNullOrEmpty()]
[string[]]
${Source},
[Parameter(ParameterSetName='Offline', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[string]
${Path},
[Parameter(ParameterSetName='Online', Mandatory=$true, ValueFromPipelineByPropertyName=$true)]
[switch]
${Online},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
${WindowsDirectory},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
${SystemDrive},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('LP')]
[string]
${LogPath},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[string]
${ScratchDirectory},
[Parameter(ValueFromPipelineByPropertyName=$true)]
[Alias('LL')]
[Microsoft.Dism.Commands.LogLevel]
${LogLevel},
[Parameter(ParameterSetName='Online')]
[switch]
${BypassWsus}
)
begin {
$BypassWsus = $PSBoundParameters.Keys -contains 'BypassWsus'
$PSBoundParameters.Remove( 'BypassWsus' ) > $null
if ( $BypassWsus ) {
# get the current WSUS setting
$UseWUServerValue = Get-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty UseWUServer
# get current servicing settings
$LocalSourcePath = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name LocalSourcePath -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty LocalSourcePath
$RepairContentServerSource = Get-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -ErrorAction SilentlyContinue |
Select-Object -ExpandProperty RepairContentServerSource
# if WSUS is turned on we turn it off temporarily
if ( $UseWUServerValue -eq 1 ) {
Write-Verbose 'WSUS is enabled, bypassing for installation...'
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value 0
if ( $LocalSourcePath -eq $null ) {
New-Item -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Force -ErrorAction SilentlyContinue > $null
New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name LocalSourcePath -Value '' -Force > $null
$RemoveLocalSourcePathProperty = $true
} elseif ( $LocalSourcePath -ne '' ) {
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name LocalSourcePath -Value '' -Force > $null
$ResetLocalSourcePathProperty = $true
}
if ( $RepairContentServerSource -eq $null ) {
New-Item -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Force -ErrorAction SilentlyContinue > $null
New-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -Value 2 -PropertyType Dword -Force > $null
$RemoveRepairContentServerSource = $true
} elseif ( $RepairContentServerSource -ne 2 ) {
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -Value 2 -Force > $null
$ResetRepairContentServerSource = $true
}
}
}
try {
$outBuffer = $null
if ( $PSBoundParameters.TryGetValue( 'OutBuffer', [ref]$outBuffer ) ) {
$PSBoundParameters['OutBuffer'] = 1
}
$wrappedCmd = $ExecutionContext.InvokeCommand.GetCommand( 'Dism\Add-WindowsCapability', [System.Management.Automation.CommandTypes]::Cmdlet )
$scriptCmd = { & $wrappedCmd @PSBoundParameters }
$steppablePipeline = $scriptCmd.GetSteppablePipeline( $myInvocation.CommandOrigin )
$steppablePipeline.Begin( $PSCmdlet )
} catch { throw }
}
process {
try {
$steppablePipeline.Process( $_ )
} catch { throw }
}
end {
try {
$steppablePipeline.End()
} catch { throw }
# turn back on WSUS if applicable
if ( $BypassWsus -and $UseWUServerValue -eq 1 ) {
Write-Verbose 'Resetting WSUS settings...'
Set-ItemProperty -Path HKLM:\SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU -Name UseWUServer -Value 1
if ( $RemoveLocalSourcePathProperty ) {
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name LocalSourcePath -Force -Confirm:$false -ErrorAction SilentlyContinue
}
if ( $ResetLocalSourcePathProperty ) {
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name LocalSourcePath -Value $LocalSourcePath
}
if ( $RemoveRepairContentServerSource ) {
Remove-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -Force -Confirm:$false -ErrorAction SilentlyContinue
}
if ( $ResetRepairContentServerSource ) {
Set-ItemProperty -Path HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\Servicing -Name RepairContentServerSource -Value $RepairContentServerSource
}
}
}
<#
.ForwardHelpTargetName Dism\Add-WindowsCapability
.ForwardHelpCategory Cmdlet
#>
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment