Windows之通过powershell 脚本修改更新服务器地址

  • A+
所属分类:Windows  Windows Server
#
# Script to switch Windows Update Service and change WSUS server
# Author: LookBack
# Date:   2017-05-05_172630
#

Clear-Host
 
Write-Host "0 -> 修改Windows更新为系统默认设置(后期将导致系统无法更新)"
Write-Host "1 -> 关闭Windows更新和检查更新"
Write-Host "2 -> 修改Windows更新为-通知下载并通知安装-使用内网源"
Write-Host "3 -> 修改Windows更新为-自动下载并通知安装-使用内网源"
Write-Host "4 -> 修改Windows更新为-自动下载并安排安装-使用内网源"
Write-Host "按任意键退出..."
Write-Host

switch(Read-Host "选择Windows更新设置"){
	0 {$UpdateValue = 0}
	1 {$UpdateValue = 1}
	2 {$UpdateValue = 2}
	3 {$UpdateValue = 3}
	4 {$UpdateValue = 4}
	Default{Exit}
}

$WindowsUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\"
$AutoUpdatePath = "HKLM:SOFTWARE\Policies\Microsoft\Windows\WindowsUpdate\AU"

function LocalUpdateServer() {
	Set-ItemProperty -Path $WindowsUpdatePath -Name AcceptTrustedPublisherCerts -Value 1
	Set-ItemProperty -Path $WindowsUpdatePath -Name UpdateServiceUrlAlternate -Value "http://10.10.240.50:8530"
	Set-ItemProperty -Path $WindowsUpdatePath -Name WUServer -Value "http://10.10.240.50:8530"
	Set-ItemProperty -Path $WindowsUpdatePath -Name WUStatusServer -Value "http://10.10.240.50:8530"
}

If(Test-Path -Path $WindowsUpdatePath) {
	Remove-Item -Path $WindowsUpdatePath -Recurse
}

If ($UpdateValue -gt 0) {
	$null = New-Item -Path $WindowsUpdatePath
	$null = New-Item -Path $AutoUpdatePath
}

if ($UpdateValue -gt 1) {
	Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoUpdate -Value 0
	Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallDay -Value 0
}

If ($UpdateValue -eq 1) {
	Set-ItemProperty -Path $AutoUpdatePath -Name NoAutoUpdate -Value 1
}

If ($UpdateValue -eq 2) {
	LocalUpdateServer
	Set-ItemProperty -Path $AutoUpdatePath -Name AUOptions -Value 2
	Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallTime -Value 3
}

If ($UpdateValue -eq 3) {
	LocalUpdateServer
	Set-ItemProperty -Path $AutoUpdatePath -Name AUOptions -Value 3
	Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallTime -Value 3
}

If ($UpdateValue -eq 4) {
	$null = LocalUpdateServer
	Set-ItemProperty -Path $AutoUpdatePath -Name AUOptions -Value 4
	Set-ItemProperty -Path $AutoUpdatePath -Name ScheduledInstallTime -Value 3
}

Write-Host
Write-Host '执行完毕,按任意键退出...'
Read-Host

由于powershell操作注册表有权限限制,上面的脚本还需要配合下面的脚本才可以实现右键以管理员身份运行的目的

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas]
"HasLUAShield"=""

[HKEY_CLASSES_ROOT\Microsoft.PowerShellScript.1\Shell\runas\command]
@="powershell.exe \"-Command\" \"if((Get-ExecutionPolicy ) -ne 'AllSigned') { Set-ExecutionPolicy -Scope Process Bypass }; & '%1'\""
lookback

发表评论

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: