View/2-ApplySettings.ps1
[Flags()]Enum MSFT_ScheduledTaskDaysOfWeek{ Sunday = 1 Monday = 2 Tuesday = 4 Wednesday = 8 Thursday = 16 Friday = 32 Saturday = 64 } [Flags()]Enum MSFT_ScheduledTaskWeeksOfMonth{ First = 1 Second = 2 Third = 4 Fourth = 8 } [Flags()]Enum MSFT_ScheduledTaskTrigger{ Event = 0 Time = 1 Daily = 2 Weekly = 3 Monthly = 4 MonthlyDayOfWeek = 5 Idle = 6 Registration = 7 Boot = 8 Logon = 9 SessionStateChange = 11 CustomTrigger01 = 12 } [Flags()]Enum MSFT_TaskCreation{ ValidateOnly = 1 Create = 2 Update = 4 CreateOrUpdate = 6 Disable = 8 DontAddPrincipalAce = 16 IgnoreRegistrationTriggers = 32 } [Flags()]Enum MSFT_TaskLogonType{ LogonNone = 0 LogonPassword = 1 LogonS4u = 2 LogonInteractiveToken = 3 LogonGroup = 4 LogonServiceAccount = 5 LogonInteractiveTokenOrPassword = 6 } [Flags()]Enum MSFT_TaskMonthlyTriggerMonthsOfYear{ January = 1 February = 2 March = 4 April = 8 May = 16 June = 32 July = 64 August = 128 September = 256 October = 512 November = 1024 December = 2048 } Function Global:Set-MainWindowSettings{ Function Set-ValueWsusPool($ItemName, $DisplayName, $NewValue){ $BeforeValue = (Get-ItemProperty $CurrentConfig.Wsus.IisWsusPoolPath -Name $ItemName).Value Set-ItemProperty -Path ($CurrentConfig.Wsus.IisWsusPoolPath) -Name ($ItemName) -Value ($NewValue) $AfterValue = (Get-ItemProperty $CurrentConfig.Wsus.IisWsusPoolPath -Name $ItemName).Value Write-Verbose "$($DisplayName) <$ItemName>: (変更前: $BeforeValue, 要求値: $NewValue, 変更後: $AfterValue)" } Function Get-SqlMinimumMemorySize{ $SqlQueryPath = "Assets\Get-SqlMinimumMemorySize.sql" } Function Set-SqlMinimumMemorySize($MinimumMemorySize){ $SqlQueryPath = (Join-Path (Get-ProgramDataDirectory) "Assets\Set-SqlMinimumMemorySize.sql") (Get-Content "Assets\Set-SqlMinimumMemorySize.sql.txt" -Encoding UTF8).Replace("{Value}", $MinimumMemorySize) | Out-File -Encoding UTF8 If ($Config.MaintenanceSql.SqlCmdExeMode -eq "psmodule"){ Invoke-Sqlcmd -ServerInstance "np:\\.\pipe\Microsoft##WID\tsql\query" -InputFile $SqlQueryPath } Else{ Start-Process } } Write-Verbose "選択された構成を確認しています..." # Options If ([String]::IsNullOrEmpty($CurrentConfig.MaintenanceSql.SqlCmdPath)){ Write-Verbose "[削除] CurrentConfig.MaintenanceSql.SqlCmdPath が空です" $Global:CurrentConfig.MaintenanceSql.PsObject.Members.Remove("SqlCmdPath") } ElseIf (-not (Test-Path $CurrentConfig.MaintenanceSql.SqlCmdPath)){ Write-Verbose "[削除] CurrentConfig.MaintenanceSql.SqlCmdPath $($CurrentConfig.MaintenanceSql.SqlCmdPath) が見つかりません" $Global:CurrentConfig.MaintenanceSql.PsObject.Members.Remove("SqlCmdPath") } # ChooseProducts $Global:CurrentConfig.ChooseProducts."Windows 11" = $Windows11Products | Sort-Object Version -Unique | Select-Object Version, @{Name="arm64";Expression={$_.SelectedArm64}}, @{Name="x64";Expression={$_.SelectedX64}} $Global:CurrentConfig.ChooseProducts."Windows 10" = $Windows10Products | Sort-Object Version -Unique | Select-Object Version, @{Name="arm64";Expression={$_.SelectedArm64}}, @{Name="x64";Expression={$_.SelectedX64}}, @{Name="x86";Expression={$_.SelectedX86}} $Global:CurrentConfig.ChooseProducts."Windows 8.1".x86 = $MainWindow.FindName("SynWindowsChooseWindows81x86CheckBox").IsChecked $Global:CurrentConfig.ChooseProducts."Windows 8.1".x64 = $MainWindow.FindName("SynWindowsChooseWindows81x64CheckBox").IsChecked $Global:CurrentConfig.ChooseProducts."Microsoft Edge".x86 = $MainWindow.FindName("SynWindowsChooseMsEdgeX86CheckBox").IsChecked $Global:CurrentConfig.ChooseProducts."Microsoft Edge".x64 = $MainWindow.FindName("SynWindowsChooseMsEdgeX64CheckBox").IsChecked $Global:CurrentConfig.ChooseProducts."Microsoft Edge".arm64 = $MainWindow.FindName("SynWindowsChooseMsEdgeArm64CheckBox").IsChecked # ApproveRule $Global:CurrentConfig.ApproveRule = ($MainWindow.FindName("ApproveRuleList").DataContext | Select-Object "FeatureUpdates", "QualityUpdates", "ApproveWaitDays", "TargetGroupName", "TargetGroupNameDisplayText", "ToStringWithoutTargetGroupName") $MainWindow.FindName("ApproveRuleList").DataContext | ForEach-Object{ Write-Verbose "-> $($_.TargetGroupName): [QualityUpdates:$($_.QualityUpdates)], [ApproveWaitDays:$($_.ApproveWaitDays)]" } $CurrentConfig | ConvertTo-Json -Depth 10 | Out-File -FilePath (Join-Path (Get-ProgramDataDirectory) "Config.json") -Encoding UTF8 # Schedule If ($MainWindow.FindName("EditiingScheduleTriggersCheckBox").IsChecked){ Write-Verbose "EditiingScheduleTriggersCheckBox is checked." $Schedule = $MainWindow.FindName("ScheduleTab").DataContext $DaysOfWeek = 0 $WeeksOfMonth = 0 # Weekly If ($Schedule.Sunday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Sunday} If ($Schedule.Monday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Monday} If ($Schedule.Tuesday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Tuesday} If ($Schedule.Wednesday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Wednesday} If ($Schedule.Thursday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Thursday} If ($Schedule.Friday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Friday} If ($Schedule.Saturday){$DaysOfWeek += [MSFT_ScheduledTaskDaysOfWeek]::Saturday} If ($Schedule.First -and $Schedule.Second -and $Schedule.Third -and $Schedule.Fourth -and $Schedule.Last){ $Schedule.First = $False $Schedule.Second = $False $Schedule.Third = $False $Schedule.Fourth = $False $Schedule.Last = $False $Schedule.LoadedWeeksOfMonth = $False Write-Verbose "-> switch to weekly mode." } ElseIf ($Schedule.First -or $Schedule.Second -or $Schedule.Third -or $Schedule.Fourth -or $Schedule.Last){ # Monthly If ($Schedule.First){$WeeksOfMonth += [MSFT_ScheduledTaskWeeksOfMonth]::First} If ($Schedule.Second){$WeeksOfMonth += [MSFT_ScheduledTaskWeeksOfMonth]::Second} If ($Schedule.Third){$WeeksOfMonth += [MSFT_ScheduledTaskWeeksOfMonth]::Third} If ($Schedule.Fourth){$WeeksOfMonth += [MSFT_ScheduledTaskWeeksOfMonth]::Fourth} If ($Schedule.Last){$WeeksOfMonth += [MSFT_ScheduledTaskWeeksOfMonth]::Last} $Schedule.LoadedWeeksOfMonth = $True Write-Verbose ([String]::Format("-> switch to Montyhly mode. First:{0}, Second:{1}, Third:{2}, Fourth:{3}, Last:{4}", $Schedule.First, $Schedule.Second, $Schedule.Third, $Schedule.Fourth, $Schedule.Last)) } Else { $Schedule.LoadedWeeksOfMonth = $False Write-Verbose "-> switch to weekly mode." } $ScheduleService = New-Object -ComObject Schedule.Service $ScheduleService.Connect() $ScheduleTask = $ScheduleService.NewTask(0) $ScheduleTask.RegistrationInfo.Description = "Optimize-WsusContents" $ScheduleTask.RegistrationInfo.Author = "Wsustainable" $ScheduleTask.Settings.Enabled = $True #$ScheduleTask.Settings.AllowDemandStart = $True $ScheduleTask.Settings.WakeToRun = $True $ScheduleTask.Principal.UserId = "System" $ScheduleTask.Principal.RunLevel = 1 #TASK_RUNLEVEL_HIGHEST $ScheduleTaskAction = $ScheduleTask.Actions.Create(0) $ScheduleTaskAction.Path = "%WinDir%\system32\WindowsPowerShell\v1.0\powershell.exe" $ScheduleTaskAction.Arguments = ("-ExecutionPolicy ByPass -Command ""Import-Module Wsustainable; Optimize-WsusContents -Config " + "'" + "$(Join-Path (Get-ProgramDataDirectory) "Config.json")'""") If ($Schedule.LoadedWeeksOfMonth){ $ScheduleTaskTrigger = $ScheduleTask.Triggers.Create([MSFT_ScheduledTaskTrigger]::MonthlyDayOfWeek) $ScheduleTaskTrigger.DaysOfWeek = $DaysOfWeek $ScheduleTaskTrigger.MonthsOfYear = 4095 $ScheduleTaskTrigger.WeeksOfMonth = $WeeksOfMonth } ElseIf ($Schedule.Sunday -and $Schedule.Monday -and $Schedule.Tuesday -and $Schedule.Wednesday -and $Schedule.Thursday -and $Schedule.Friday -and $Schedule.Saturday){ $ScheduleTaskTrigger = $ScheduleTask.Triggers.Create([MSFT_ScheduledTaskTrigger]::Daily) } ElseIf ($Schedule.Sunday -or $Schedule.Monday -or $Schedule.Tuesday -or $Schedule.Wednesday -or $Schedule.Thursday -or $Schedule.Friday -or $Schedule.Saturday){ $ScheduleTaskTrigger = $ScheduleTask.Triggers.Create([MSFT_ScheduledTaskTrigger]::Weekly) $ScheduleTaskTrigger.DaysOfWeek = $DaysOfWeek } Else{ $ScheduleTaskTrigger = $ScheduleTask.Triggers.Create([MSFT_ScheduledTaskTrigger]::Daily) } $ScheduleTaskTrigger.StartBoundary = (Get-Date $MainWindow.FindName("WeeklyScheduleDateTimePicker").Value -Format s) Write-Verbose "-> Time: $($ScheduleTaskTrigger.StartBoundary)" #$ScheduleTaskTrigger.Enabled = $True $ScheduleService.GetFolder("\").RegisterTaskDefinition("Optimize-WsusContents", $ScheduleTask, [MSFT_TaskCreation]::CreateOrUpdate,"","",[MSFT_TaskLogonType]::LogonServiceAccount) If ($StartTask){ Start-ScheduledTask -TaskName $TaskName } } # Quata If ($MainWindow.FindName("ConfigureFileQuotaCheckBox").IsChecked){ $WsusContentDirectory = (Get-ItemProperty "HKLM:\SOFTWARE\Microsoft\Update Services\Server\Setup").ContentDir If (@(Get-FsrmQuota | Where-Object Path -like $WsusContentDirectory)[0] -eq $Null){ New-FsrmQuota -Path $WsusContentDirectory -Size ([System.UInt64]($MainWindow.FindName("QuotaSizeTextBox").Value * 1073741824)) } Else{ Set-FsrmQuota -Path $WsusContentDirectory -Size ([System.UInt64]($MainWindow.FindName("QuotaSizeTextBox").Value * 1073741824)) } } # IIS If (Test-IisWsusPoolPath){ Write-Verbose "IIS Wsus pool:" Set-ValueWsusPool -ItemName "queueLength" -DisplayName "(全般)\キューの長さ" -NewValue ([Uint64]$MainWindow.FindName("WsusQueueLengthTextBox").Value) Set-ValueWsusPool -ItemName "Cpu.Limit" -DisplayName "CPU\制限 (%)" -NewValue ([Uint64]$MainWindow.FindName("WsusCpuLimitTextBox").Value) Set-ValueWsusPool -ItemName "Failure.RapidFailProtectionInterval" -DisplayName "ラピッド フェール保護\エラー間隔 (分)" -NewValue $MainWindow.FindName("WsusRapidFailProtectionIntervalTextBox").Value Set-ValueWsusPool -ItemName "Failure.RapidFailProtectionMaxCrashes" -DisplayName "ラピッド フェール保護\最大エラー数" -NewValue ([System.Int64]$MainWindow.FindName("WsusRapidFailProtectionMaxCrashesTextBox").Value) #Set-ValueWsusPool -ItemName "processModel.idleTimeout" -DisplayName "プロセスモデル\アイドル状態のタイムアウト" -NewValue $MainWindow.FindName("WsusIdleTimeoutTextBox").Value #Set-ValueWsusPool -ItemName "processModel.pingingEnabled" -DisplayName "プロセスモデル\Pingの有効化" -NewValue $MainWindow.FindName("WsusPingingEnabledCheckBox").IsChecked #Set-ValueWsusPool -ItemName "recycling.periodicRestart.time" -DisplayName "リサイクル\定期的な期間" -NewValue $MainWindow.FindName("WsusPeriodicRestartTimeTextBox").Value Set-ValueWsusPool -ItemName "recycling.periodicRestart.privateMemory" -DisplayName "リサイクル\プライベートメモリ制限" -NewValue ([System.Int64]$MainWindow.FindName("WsusPeriodicRestartPrivateMemoryTextBox").Value) Set-ValueWsusPool -ItemName "recycling.periodicRestart.Memory" -DisplayName "リサイクル\仮想メモリ制限 (KB)" -NewValue ([System.Int64]$MainWindow.FindName("WsusPeriodicRestartMemoryTextBox").Value) } } |