提示:记录常用脚本
前言
提示:记录常用脚本
工作中常用的脚本记录,方便新环境使用
一、设置CMD默认管理员权限运行
powershell 脚本,管理员权限运行 Disable-UACAdminApproval.ps1
# 检查管理员权限 / Check administrator privileges
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
Write-Host "错误:此脚本需要管理员权限 / Error: This script requires Administrator privileges" -ForegroundColor Red
pause
exit 1
}
Write-Host "正在禁用UAC管理员批准模式... / Disabling UAC Admin Approval Mode..." -ForegroundColor Yellow
try {
# 修改注册表设置以禁用UAC核心功能 / Modify registry to disable UAC core feature
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "EnableLUA" -Value 0 -Force
# 禁用管理员提升提示 / Disable admin elevation prompts
Set-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System" -Name "ConsentPromptBehaviorAdmin" -Value 0 -Force
Write-Host "操作成功完成!系统需要重启 / Operation completed successfully! System restart required." -ForegroundColor Green
# 询问重启选项 / Prompt for restart
$choice = Read-Host "现在重启?(Y/N) / Restart now? (Y/N)"
if ($choice -eq 'Y') {
Restart-Computer -Confirm:$false -Force
}
else {
Write-Host "更改将在下次重启后生效 / Changes will take effect after next reboot" -ForegroundColor Yellow
pause
}
}
catch {
Write-Host "操作失败: $_ / Operation failed: $_" -ForegroundColor Red
pause
exit 1
}
1.1 问题
powershell 中文乱码问题
- 在 VS Code 中打开脚本文件。
- 点击右下角编码名称(如 UTF-8)→ 选择 Save with Encoding → 选择 UTF-8 with BOM 。
- 保存文件并重新运行。
- 什么是 BOM?
UTF-8 with BOM(Byte Order Mark)是 UTF-8 编码的一种变体,其特点是在文件开头添加一个 不可见的 BOM 字符 (0xEF 0xBB 0xBF),用于标识文件编码。以下是详细解释:
BOM(Byte Order Mark) 是一个特殊的 Unicode 字符(U+FEFF),用于标记文本文件的编码格式和字节顺序(endianness)。
在 UTF-8 中,BOM 并非必需(因为 UTF-8 的字节顺序是固定的),但某些软件(如旧版 Windows 程序或 PowerShell)依赖 BOM 来识别文件编码。
二、JDK 版本切换,自动设置环境变量
2.1 安装好jdk版本(自行安装)
2.2 powershell 脚本
管理员权限运行 , jdkman2.ps1
# JDK版本管理工具 / JDK Version Manager for Windows
function Get-InstalledJDKs {
$jdkPaths = @()
# 搜索常见JDK安装路径 / Search common JDK installation locations
$searchPaths = @('C:\Program Files\Java', 'C:\Program Files (x86)\Java', "$env:USERPROFILE\AppData\Local\Programs\Java")
foreach ($path in $searchPaths) {
if (Test-Path $path) {
$jdkPaths += Get-ChildItem $path | Where-Object {
$_.Name -match '^jdk-?(\d+[\d._]*)' -and $_.PSIsContainer
} | ForEach-Object {
[PSCustomObject]@{
Version = $matches[1]
FullName = $_.FullName
}
}
}
}
return $jdkPaths
}
function Set-JDK {
param([string]$jdkPath)
# 更新当前会话环境变量 / Update current session environment
$env:JAVA_HOME = $jdkPath
$env:PATH = (Join-Path $jdkPath "bin") + ";" + $env:PATH
# 以管理员身份运行时更新用户环境变量 / Update user environment if admin
if ([Security.Principal.WindowsPrincipal]::new([Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
[Environment]::SetEnvironmentVariable('JAVA_HOME', $jdkPath, [EnvironmentVariableTarget]::User)
$userPath = [Environment]::GetEnvironmentVariable('Path', [EnvironmentVariableTarget]::User)
$newPath = (Join-Path $jdkPath "bin") + ";$userPath"
[Environment]::SetEnvironmentVariable('Path', $newPath, [EnvironmentVariableTarget]::User)
}
Write-Host "成功设置JDK路径至 $jdkPath / Successfully set JDK to $jdkPath" -ForegroundColor Green
Write-Host "当前会话已更新!当前Java版本 / Current session updated! New Java version:"
java -version
}
# 主程序执行 / Main execution
$jdks = Get-InstalledJDKs
if ($jdks.Count -eq 0) {
Write-Host "未在默认路径找到JDK安装 / No JDK installations found in default locations" -ForegroundColor Red
exit 1
}
Write-Host "`n可用的JDK安装列表 / Available JDK Installations:"
for ($i = 0; $i -lt $jdks.Count; $i++) {
Write-Host "$($i+1)). [$($jdks[$i].Version)] $($jdks[$i].FullName)"
}
$selection = Read-Host "`n输入要使用的JDK编号 / Enter the number of the JDK to use"
if ($selection -match "^\d+$" -and $selection -le $jdks.Count) {
Set-JDK $jdks[$selection-1].FullName
} else {
Write-Host "无效的选择 / Invalid selection" -ForegroundColor Red
exit 1
}