install software and make configurations
This commit is contained in:
54
lib/invoke-cmdexec.ps1
Normal file
54
lib/invoke-cmdexec.ps1
Normal file
@@ -0,0 +1,54 @@
|
||||
param(
|
||||
[Parameter(Mandatory=$true)] [string]$Command,
|
||||
[string]$WorkDir = $null
|
||||
)
|
||||
|
||||
# 1. 处理工作目录
|
||||
if ([string]::IsNullOrEmpty($WorkDir)) {
|
||||
$WorkDir = $PSScriptRoot
|
||||
} else {
|
||||
# 如果 WorkDir 里包含变量(如 $env:APPDATA),展开它
|
||||
$WorkDir = $ExecutionContext.InvokeCommand.ExpandString($WorkDir)
|
||||
}
|
||||
|
||||
# 确保目录存在,否则命令会报错
|
||||
if (-not (Test-Path $WorkDir)) {
|
||||
Write-Warning "`n[CMD] WARNING: The work dir does not exist, using ($WorkDir)"
|
||||
$WorkDir = $PSScriptRoot
|
||||
}
|
||||
|
||||
# 2. 处理命令中的环境变量
|
||||
$ProjectRoot = Split-Path $PSScriptRoot -Parent
|
||||
# === 替换 $PSScriptRoot 为实际的绝对路径 ===
|
||||
# 注意:要处理反斜杠转义问题,直接用字符串替换最安全
|
||||
if ($Command.Contains('$PSScriptRoot')) {
|
||||
$Command = $Command.Replace('$PSScriptRoot', $ProjectRoot)
|
||||
}
|
||||
|
||||
# 这一步很关键,让你可以写 "echo $env:USERNAME"
|
||||
$Command = $ExecutionContext.InvokeCommand.ExpandString($Command)
|
||||
|
||||
Write-Host "`n[CMD] Execute: $Command" -ForegroundColor Gray
|
||||
|
||||
# 3. 启动进程
|
||||
# /c 表示执行完命令后关闭 cmd 窗口
|
||||
# /s 开启参数的一般处理(忽略第一个和最后一个引号,为了兼容复杂引号情况)
|
||||
$processOptions = @{
|
||||
FilePath = "cmd.exe"
|
||||
ArgumentList = "/s", "/c", "`"$Command`""
|
||||
WorkingDirectory = $WorkDir
|
||||
Wait = $true
|
||||
NoNewWindow = $true
|
||||
PassThru = $true
|
||||
}
|
||||
|
||||
try {
|
||||
$proc = Start-Process @processOptions
|
||||
|
||||
# 4. 检查退出代码 (ExitCode)
|
||||
if ($proc.ExitCode -ne 0) {
|
||||
Write-Error "`n[CMD] Failed to execute, exit code: $($proc.ExitCode)"
|
||||
}
|
||||
} catch {
|
||||
Write-Error "`n[CMD] Failed to start process: $_"
|
||||
}
|
||||
41
lib/invoke-filecopy.ps1
Normal file
41
lib/invoke-filecopy.ps1
Normal file
@@ -0,0 +1,41 @@
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Source,
|
||||
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Destination
|
||||
)
|
||||
|
||||
try {
|
||||
# 1. 路径预处理:展开环境变量 (例如把 $env:APPDATA 变成 C:\Users\...\AppData\Roaming)
|
||||
$ResolvedSource = $ExecutionContext.InvokeCommand.ExpandString($Source)
|
||||
$ResolvedDest = $ExecutionContext.InvokeCommand.ExpandString($Destination)
|
||||
|
||||
# 2. 处理相对路径 (针对源文件)
|
||||
# 如果源路径是相对路径 (./assets/...), 尝试将其转换为绝对路径
|
||||
# 假设脚本是从项目根目录调用的
|
||||
if (-not (Test-Path $ResolvedSource) -and (Test-Path "$PWD\$ResolvedSource")) {
|
||||
$ResolvedSource = Join-Path $PWD $ResolvedSource
|
||||
}
|
||||
|
||||
# 3. 再次检查源文件是否存在
|
||||
if (-not (Test-Path $ResolvedSource -PathType Leaf)) {
|
||||
Write-Warning "`n[FileCopy] [Skip] File not found: $ResolvedSource"
|
||||
return # 退出脚本
|
||||
}
|
||||
|
||||
# 4. 处理目标目录 (自动创建不存在的文件夹)
|
||||
$DestDir = Split-Path -Path $ResolvedDest -Parent
|
||||
if (-not (Test-Path $DestDir)) {
|
||||
Write-Host "`n[FileCopy] Create dir: $DestDir" -ForegroundColor DarkGray
|
||||
New-Item -Path $DestDir -ItemType Directory -Force | Out-Null
|
||||
}
|
||||
|
||||
# 5. 执行复制 (Force = 覆盖)
|
||||
Copy-Item -Path $ResolvedSource -Destination $ResolvedDest -Force -ErrorAction Stop
|
||||
|
||||
Write-Host "`n[FileCopy] Success: $ResolvedDest" -ForegroundColor Green
|
||||
|
||||
} catch {
|
||||
Write-Error "`n[FileCopy] Fail: $_"
|
||||
}
|
||||
37
lib/invoke-regimport.ps1
Normal file
37
lib/invoke-regimport.ps1
Normal file
@@ -0,0 +1,37 @@
|
||||
param(
|
||||
[Parameter(Mandatory=$true)]
|
||||
[string]$Path
|
||||
)
|
||||
|
||||
try {
|
||||
# 1. 路径预处理
|
||||
# 展开环境变量 (虽然 .reg 路径通常是固定的,但支持一下没坏处)
|
||||
$ResolvedPath = $ExecutionContext.InvokeCommand.ExpandString($Path)
|
||||
|
||||
# 2. 处理相对路径
|
||||
# 如果路径是相对的 (./assets/...), 转换为绝对路径
|
||||
if (-not (Test-Path $ResolvedPath) -and (Test-Path "$PWD\$ResolvedPath")) {
|
||||
$ResolvedPath = Join-Path $PWD $ResolvedPath
|
||||
}
|
||||
|
||||
# 3. 检查文件是否存在
|
||||
if (-not (Test-Path $ResolvedPath)) {
|
||||
Write-Warning "`n[RegImport] [Skip] Cannot find: $ResolvedPath"
|
||||
return
|
||||
}
|
||||
|
||||
Write-Host "`n[RegImport] Importing: $(Split-Path $ResolvedPath -Leaf)" -ForegroundColor Gray
|
||||
|
||||
# 4. 调用 reg.exe 导入
|
||||
# 使用 Start-Process 以获取退出代码
|
||||
$proc = Start-Process -FilePath "reg.exe" -ArgumentList "import", "`"$ResolvedPath`"" -Wait -PassThru -NoNewWindow
|
||||
|
||||
if ($proc.ExitCode -eq 0) {
|
||||
Write-Host "`n[RegImport] Successfully imported." -ForegroundColor Green
|
||||
} else {
|
||||
Write-Error "`n[RegImport] Failed to import: $($proc.ExitCode)"
|
||||
}
|
||||
|
||||
} catch {
|
||||
Write-Error "`n[RegImport] Failed to start process: $_"
|
||||
}
|
||||
Reference in New Issue
Block a user