|
Function Set-WUAutoUpdateSetting
{
[CmdletBinding(SupportsShouldProcess=$true,ConfirmImpact="High")]
Param
(
[Parameter(Mandatory=$false,Position=0)][Boolean]$FeaturedUpdatesEnabled,
[Parameter(Mandatory=$false,Position=1)][Boolean]$IncludeRecommendedUpdates,
[Parameter(Mandatory=$false,Position=2)][Boolean]$NonAdministratorsElevated,
[Parameter(Mandatory=$false,Position=3)][ValidateRange(0,4)][INT]$NotificationLevel
)
DynamicParam
{
if ($NotificationLevel -eq 4) {
#Define dynamic parameter ScheduledInstallationDay
$paramDictionary = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary
$validateRange1 = New-Object System.Management.Automation.ValidateRangeAttribute(0,7)
$paraAttributes1 = New-Object System.Management.Automation.ParameterAttribute
$paraAttributes1.Mandatory = $false
$paraAttributes1.Position = 4
$attributeCollection1 = new-object -Type System.Collections.ObjectModel.Collection``1[System.Attribute]
$attributeCollection1.Add($paraAttributes1)
$dyParam1 = New-Object System.Management.Automation.RuntimeDefinedParameter("ScheduledInstallationDay",[INT],$attributeCollection1)
$dyParam1.Attributes.Add($attrCol1)
$dyParam1.Attributes.Add($validateRange1)
$paramDictionary.Add("ScheduledInstallationDay",$dyParam1)
#Define dynamic parameter ScheduledInstallationTime
$validateRange2 = New-Object System.Management.Automation.ValidateRangeAttribute(0,23)
$paraAttributes2 = New-Object System.Management.Automation.ParameterAttribute
$paraAttributes2.Mandatory = $false
$paraAttributes2.Position = 5
$attributeCollection2 = new-object -Type System.Collections.ObjectModel.Collection``1[System.Attribute]
$attributeCollection2.Add($paraAttributes2)
$dyParam2 = New-Object System.Management.Automation.RuntimeDefinedParameter("ScheduledInstallationTime",[INT],$attributeCollection2)
$dyParam2.Attributes.Add($attrCol2)
$dyParam2.Attributes.Add($validateRange2)
$paramDictionary.Add("ScheduledInstallationTime",$dyParam2)
return $paramDictionary
}
}
Process
{
$params = $pscmdlet.MyInvocation.BoundParameters
foreach ($commonParameter in $commonParameters) {
if ($params.ContainsKey($commonParameter)) {
$params.Remove($commonParameter) | Out-Null
}
}
if (-not $(Test-Privilege)) {
$customError = New-CustomErrorRecord `
-ExceptionString "Please run Set-WUAutoUpdateSetting under administrator's privilege." `
-ErrorID "Error" -ErrorCategory "NotSpecified" `
-TargetObject $pscmdlet
$pscmdlet.ThrowTerminatingError($customError)
}
if ($params.Count -eq 0) {
$customError = New-CustomErrorRecord `
-ExceptionString "Set-WUAutoUpdateSetting failed. Please specify at least one parameter." `
-ErrorID "Error" -ErrorCategory "NotSpecified" `
-TargetObject $pscmdlet
$pscmdlet.ThrowTerminatingError($customError)
} else {
$serviceEnabled = (Get-WUAutoUpdateSetting).ServiceEnabled
if ($serviceEnabled) {
foreach ($param in $params.GetEnumerator()) {
$propertyName = $param.Key
$propertyValue = $param.Value
$muAU.Settings.$propertyName = $propertyValue
Try
{
$returnWithError = $false
if ($pscmdlet.ShouldProcess($propertyName)) {
$muAU.Settings.Save()
}
}
Catch
{
$returnWithError = $true
}
Finally
{
if (-not $returnWithError) {
switch ($propertyName) {
"NotificationLevel" {
$propertyValue = $HTNotificationLevel[$propertyValue]
$pscmdlet.WriteVerbose("Succeeded to change the setting $propertyName to $propertyValue.")
}
"ScheduledInstallationDay" {
$propertyValue = $HTScheduledInstallationDay[$propertyValue]
$pscmdlet.WriteVerbose("Succeeded to change the setting $propertyName to $propertyValue.")
}
"ScheduledInstallationTime" {
$propertyValue = $propertyValue.ToString() + ":00"
$pscmdlet.WriteVerbose("Succeeded to change the setting $propertyName to $propertyValue.")
}
Default {
$pscmdlet.WriteVerbose("Succeeded to change the setting $propertyName to $propertyValue.")
}
}
} else {
$customError = New-CustomErrorRecord `
-ExceptionString "Failed to change the setting $propertyName to $propertyValue." `
-ErrorID "UnknownError" -ErrorCategory "NotSpecified" `
-TargetObject $pscmdlet
$pscmdlet.WriteError($customError)
}
}
}
} else {
$customError = New-CustomErrorRecord `
-ExceptionString "Windows update service is disabled." `
-ErrorID "Error" -ErrorCategory "NotSpecified" `
-TargetObject $pscmdlet
$pscmdlet.ThrowTerminatingError($customError)
}
}
}
}
|
|
|