Windows Server 2016 & 2019 工作站速配脚本

发布时间 2024-01-13 15:46:58作者: fmcdr

之前有一篇关于把Windows Server打造成工作站系统的[随笔],其中的步骤完全基于手工操作,恐怕部分对系统不熟悉的朋友会找不到设置的入口。与其弄一堆截图写所谓的教程,还不如写一个程序来自动化处理。

init.ps1


Write-Host "`n正在启用声音服务"
Set-Service -Name "Audiosrv" -Status running -StartupType automatic

Write-Host "`n禁用登陆前Ctrl+Alt+Del组合键"
$regPath = "HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System"
Remove-ItemProperty -Path $regPath -Name 'DisableCAD' -Force;
New-ItemProperty -Path $regPath -Name 'DisableCAD' -Value 1 -PropertyType DWORD -Force;

$regPath = "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion"
$owner = (Get-ItemProperty -Path $regPath -Name 'RegisteredOwner').RegisteredOwner
Write-Host "本机所有者为: '$owner'"
$ans = Read-Host "需要修改所有者吗? [y/N] "
if ($ans -eq 'y') {
    $newOwner = Read-Host -Prompt '请输入新的所有者'
    Set-ItemProperty -Path $regPath -Name 'RegisteredOwner' -Value $newOwner
}
# $org = (Get-ItemProperty -Path $regPath -Name 'RegisteredOrganization').RegisteredOrganization

Write-Host "`n禁用强制性的密码强度要求"
secedit /export /cfg c:\secpol.cfg;
(gc C:\secpol.cfg).replace("PasswordComplexity = 1", "PasswordComplexity = 0") | Out-File C:\secpol.cfg;
secedit /configure /db c:\windows\security\local.sdb /cfg c:\secpol.cfg /areas SECURITYPOLICY;
rm -force c:\secpol.cfg -confirm:$false;

$user = $Env:username
$ans = Read-Host "`n密码策略已修改,是否需要修改当前密码 [y/N]"
if ($ans -eq 'y') {
    $p1 = Read-Host "请输入新密码" -AsSecureString
    $p1_txt = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($p1))
    $p2 = Read-Host "请再次输入新密码" -AsSecureString
    $p2_txt = [Runtime.InteropServices.Marshal]::PtrToStringAuto([Runtime.InteropServices.Marshal]::SecureStringToBSTR($p2))
    if ($p1_txt.compareTo($p2_txt) -eq 0) {
        $Account = Get-LocalUser -Name $user
        $Account | Set-LocalUser -Password $p1
    } else {
        Write-Host "两次输入的密码不一致,密码未修改"
    }
}

$ans = Read-Host "当前登陆用户名为:$user,需要修改当前用户名吗 [y/N]"

if ($ans -eq 'y') {
    $nuser = Read-Host "请输入新的用户名"
    if ($nuser -eq '') {
        Write-Host '用户名无效'
    } else {
        Rename-LocalUser -Name $user -NewName $nuser
    }
}

Write-Host "`n正在设置内存面页压缩"
$mc = Get-MMAgent
if ( -Not($mc.MemoryCompression)) {
    Enable-MMAgent -MemoryCompression
}

Write-Host "`n禁止关机事件跟踪器"
if ( -Not (Test-Path 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Reliability')) {
    New-Item -Path 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT' -Name Reliability -Force
}
# add registry setting
Set-ItemProperty -Path 'registry::HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Microsoft\Windows NT\Reliability' -Name ShutdownReasonOn -Value 0

Write-Host "`n请在打开的对话框中选择‘仅为基本 Windows 程序和服务启用 DEP’"
Start-Process SystemPropertiesDataExecutionPrevention -Wait

Write-Host "`n请在打开的对话框中选择‘调整为最佳外观’, 在‘高级’选项卡中选择 调整以优化性能‘程序’"
Start-Process SystemPropertiesPerformance -Wait

上面的内容保存为 init.ps1,可以尝试以管理员权限直接运行,如果运行成功,后面的步骤可以跳过。

如果你通过下载获得此脚本,系统大概率会阻止你运行。此时,要么修改系统的脚本运行策略,要么参考前一篇随笔, 把run.bat的内容保存在相同目录下面,通过管理员权限运行run.bat脚本,输入上面的脚本文件名init.ps1去执行脚本。