shuijingping 发表于 2018-9-3 09:22:18

PowerShell V2 高级函数使用举例(02)

  Function Set-WUAutoUpdateSetting
  {
  
  Param
  (
  $FeaturedUpdatesEnabled,
  $IncludeRecommendedUpdates,
  $NonAdministratorsElevated,
  $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
  $attributeCollection1.Add($paraAttributes1)
  $dyParam1 = New-Object System.Management.Automation.RuntimeDefinedParameter("ScheduledInstallationDay",,$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
  $attributeCollection2.Add($paraAttributes2)
  $dyParam2 = New-Object System.Management.Automation.RuntimeDefinedParameter("ScheduledInstallationTime",,$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)
  }
  }
  }
  }

页: [1]
查看完整版本: PowerShell V2 高级函数使用举例(02)