Write-host 帮助信息

本文介绍了PowerShell中的Write-Host命令的用法,包括如何显示带有特定颜色的对象、使用自定义分隔符以及如何禁用自动换行。此外还提供了几个示例来演示这些功能的实际应用。
如下说明是翻译: help write-host 产生的帮助信息.
译者: Edengundam(马涛)
 
Write-Host
 
大纲
使用host用户接口显示输入对象
 
语法
Write-Host [[-object] <Object>] [-noNewLine] [-separator <Object>] [-foregroundcolor {<Black> | <DarkBlue> | <DarkGreen> | <DarkCyan> | <DarkRed> | <DarkMagenta> | <DarkYellow> | <Gray> | <DarkGray> | <Blue> | <Green> | <Cyan> | <Red> | <Magenta> | <Yellow> | <White>}] [-backgroundColor {<Black> | <DarkBlue> | <DarkGreen> | <DarkCyan> | <DarkRed> | <DarkMagenta> | <DarkYellow> | <Gray> | <DarkGray> | <Blue> | <Green> | <Cyan> | <Red> | <Magenta> | <Yellow> | <White>}] [<CommonParameters>]
 
详细描述
使用host用户接口显示输入对象. Write-Host创建一个自定义的host窗口. 你可以通过ForegroundColor参数指定现在该窗口中文本的颜色, 也可以通过BackgroundColor 参数指定显示在该窗口中本文的背景颜色. 分隔符参数允许指定特定字符串, 作为不同对象之间的分隔符.
 
参数
 
-object <Object>
要显示在控制台上的对象.
 
强制参数?
false
参数位置?
1
默认值
 
允许从管道绑定输入?  
true (传值)
允许通配符扩展
false
 
-noNewLine <SwitchParameter>
指定显示在控制台的信息不以换行结尾.
 
强制参数?
false
参数位置?
named
默认值
False
允许从管道绑定输入?  
false
允许通配符扩展
false
 
-separator <Object>
指定显示在控制台上的对象之间使用的分隔字符串.
 
强制参数?
false
参数位置?
named
默认值
一个空格
允许从管道绑定输入?  
false
允许通配符扩展
false
 
-foregroundcolor <ConsoleColor>
指定文本颜色.
 
此参数接受的取值如下:
 
·         Black
·         DarkBlue
·         DarkGreen
·         DarkCyan
·         DarkRed
·         DarkMagenta
·         DarkYellow
·         Gray
·         DarkGray
·         Blue
·         Green
·         Cyan
·         Red
·         Magenta
·         Yellow
·         White
 
强制参数?
false
参数位置?
named
默认值
Host-defined
允许从管道绑定输入?  
false
允许通配符扩展
false
 
-backgroundColor <ConsoleColor>
指定背景颜色.
 
此参数接受的取值如下:
 
·         Black
·         DarkBlue
·         DarkGreen
·         DarkCyan
·         DarkRed
·         DarkMagenta
·         DarkYellow
·         Gray
·         DarkGray
·         Blue
·         Green
·         Cyan
·         Red
·         Magenta
·         Yellow
·         White
 
强制参数?
false
参数位置?
named
默认值
Host-defined
允许从管道绑定输入?   
false
允许通配符扩展
false
 
<公共参数>
此命令支持公共参数: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. 更多信息, 输入, "get-help about_commonparameters".
 
输入类型
任意
 
注意
 
更多信息, 输入"Get-Help Write-Host -detailed".需要技术信息, 输入"Get-Help Write-Host -full".
 
如果需要为该命令提供多个参数, 请使用逗号进行分隔. 例如, "<parameter-name> <value1>, <value2>".
 
1
 
C:/PS>write-host "no newline test" -nonewline
 
此命令显示输入信息到控制台, 由于制定了nonewline参数, 提示符直接跟在输出信息之后.
 
2
 
C:/PS>write-host (2,4,6,8,10,12) -Separator ", +2= "
 
此命令显示从212的所有偶数. 分隔符参数指定使用 , +2= (逗号, 空格, +, 2, =, 空格)作为分割.
 
2, +2= 4, +2= 6, +2= 8, +2= 10, +2= 12
 
3
 
C:/PS>write-host (2,4,6,8,10,12) -Separator ", -> " -foregroundcolor DarkGreen -backgroundcolor white
 
此命令显示从212的所有偶数. Foregroundcolor参数指定输出文本为绿色, Backgroundcolor参数指定背景色为白色.
 
相关链接
Write-Verbose
Write-Error
Write-Progress
Write-Debug
Write-Output
Write-Warning
Out-Host
 
 
# 卸载模块 $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 if ($module) { $moduleDir = $module.ModuleBase Write-Host "正在卸载 CurlTools 模块..." -ForegroundColor Yellow Write-Host "模块位置: $moduleDir" # 检查是否以管理员权限运行(Windows) if ($env:OS -like "Windows*" -and -not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { Write-Warning "建议使用管理员权限运行卸载脚本" } # 确保卸载前移除模块 try { Remove-Module CurlTools -Force -ErrorAction Stop Write-Host "✅ 模块已从会话中移除" -ForegroundColor Green } catch { Write-Warning "无法从会话中移除模块: $_" } # 删除模块目录 try { Write-Host "正在删除模块文件..." Remove-Item -Path $moduleDir -Recurse -Force -ErrorAction Stop Write-Host "✅ 模块文件已删除" -ForegroundColor Green # 清理配置文件 $configDir = switch (Get-Platform) { "Linux" { Join-Path $HOME ".config" "curltools" } "macOS" { Join-Path $HOME "Library" "Application Support" "CurlTools" } default { Join-Path $env:APPDATA "CurlTools" } } if (Test-Path $configDir) { Remove-Item -Path $configDir -Recurse -Force Write-Host "✅ 配置文件已清理" -ForegroundColor Green } Write-Host "`n✅ 模块已成功卸载" -ForegroundColor Green exit 0 } catch { Write-Error "❌ 卸载失败: $_" Write-Host "请手动删除目录: $moduleDir" -ForegroundColor Red exit 1 } } else { Write-Warning "未找到 CurlTools 模块" exit 0 } 我按完回车 这里小蓝窗就闪退出去了!!(然后我再重新启动powershell,复制粘贴# ===== 安装后测试 ===== try { # 重新加载模块 Write-Host "`n重新加载模块..." -ForegroundColor Cyan Remove-Module CurlTools -ErrorAction SilentlyContinue -Force Import-Module $moduleDir -Force -ErrorAction Stop # 创建测试状态变量 $global:TestFailed = $false # 基本功能测试 $tests = @( { Write-Host "测试: Get-CurlPath" -ForegroundColor Cyan $path = Get-CurlPath if (-not $path) { throw "返回空路径" } if (-not (Test-Path $path)) { throw "路径不存在: $path" } Write-Host "✅ 成功 | 路径: $path" -ForegroundColor Green $path } { Write-Host "测试: Get-CurlVersion" -ForegroundColor Cyan $version = Get-CurlVersion if (-not $version) { throw "返回空版本" } if (-not $version.Version) { throw "版本号解析失败" } Write-Host "✅ 成功 | 版本: $($version.Version)" -ForegroundColor Green $version } { Write-Host "测试: Invoke-SecureDownload" -ForegroundColor Cyan $url = "https://www.example.com" $out = Join-Path $env:TEMP "test_$(Get-Date -Format 'yyyyMMddHHmmss').html" $result = Invoke-SecureDownload -Url $url -OutputPath $out if (-not (Test-Path $out)) { throw "文件未创建" } $fileSize = (Get-Item $out).Length if ($fileSize -eq 0) { throw "文件大小为0字节" } Write-Host "✅ 成功 | 文件大小: $fileSize bytes" -ForegroundColor Green $out } { Write-Host "测试: 卸载功能" -ForegroundColor Cyan $uninstallPath = Join-Path $moduleDir "Uninstall.ps1" if (-not (Test-Path $uninstallPath)) { throw "卸载脚本不存在" } # 测试卸载函数是否存在 $uninstallCmd = Get-Command Uninstall-CurlTools -ErrorAction SilentlyContinue if (-not $uninstallCmd) { throw "卸载命令未注册" } Write-Host "✅ 成功 | 卸载功能正常" -ForegroundColor Green $uninstallPath } ) foreach ($test in $tests) { try { $result = & $test if (-not $result) { Write-Warning "⚠️ 测试返回空结果: $($test.ToString())" } } catch { Write-Error "❌ 测试失败: $($test.ToString()) - $_" $global:TestFailed = $true } } if ($global:TestFailed) { Write-Error "❌ 模块安装完成,但部分测试失败" -ForegroundColor Red exit 1 } else { Write-Host "`n✅ 模块安装成功并通过所有测试" -ForegroundColor Green # 显示卸载说明 Write-Host "`n卸载说明:" -ForegroundColor Magenta Write-Host "1. 运行命令: Uninstall-CurlTools" -ForegroundColor Yellow Write-Host "2. 或执行脚本: . '$uninstallPath'" -ForegroundColor Yellow exit 0 } } catch { Write-Error "❌ 模块加载失败: $_" exit 1 } 然后回车 然后小蓝窗又闪退出去了)
08-13
PS C:\Users\Administrator> param( >> [switch]$Force >> ) >> >> # 1. 确定模块目录 >> $moduleDir = if ($PSVersionTable.PSEdition -eq "Core") { >> Join-Path $HOME ".local/share/powershell/Modules/CurlTools" >> } else { >> Join-Path $env:ProgramFiles "WindowsPowerShell/Modules/CurlTools" >> } >> >> # 2. 确保目录结构存在 >> $requiredDirs = @( >> $moduleDir, >> (Join-Path $moduleDir "Public"), >> (Join-Path $moduleDir "Private") >> ) >> >> foreach ($dir in $requiredDirs) { >> if (-not (Test-Path $dir)) { >> try { >> New-Item -ItemType Directory -Path $dir -Force | Out-Null >> Write-Host "✅ 创建目录: $dir" -ForegroundColor Green >> } catch { >> Write-Error "❌ 创建目录失败: $dir - $_" >> Read-Host "按回车键退出" >> exit 1 >> } >> } elseif ($Force) { >> Write-Warning "目录已存在: $dir (使用 -Force 覆盖)" >> } >> } >> >> # 3. 定义模块文件内容 >> # 模块主文件 (CurlTools.psm1) >> $moduleContent = @' >> # 导出公共函数 >> Export-ModuleMember -Function Get-CurlPath, Get-CurlVersion, Invoke-SecureDownload, Uninstall-CurlTools >> >> # 私有函数 >> function Get-Platform { >> if ($IsLinux) { "Linux" } >> elseif ($IsMacOS) { "macOS" } >> else { "Windows" } >> } >> >> # 公共函数 >> function Get-CurlPath { >> if ($IsWindows) { >> $curlPath = Get-Command curl -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source >> if (-not $curlPath) { $curlPath = "C:\Windows\System32\curl.exe" } >> return $curlPath >> } else { >> return (Get-Command curl).Source >> } >> } >> >> function Get-CurlVersion { >> $curlPath = Get-CurlPath >> $versionOutput = & $curlPath --version | Select-Object -First 1 >> if ($versionOutput -match 'curl (\d+\.\d+\.\d+)') { >> return @{ >> Version = $matches[1] >> FullOutput = $versionOutput >> } >> } else { >> throw "无法解析curl版本" >> } >> } >> >> function Invoke-SecureDownload { >> param( >> [Parameter(Mandatory=$true)] >> [ValidatePattern('^https?://')] >> [string]$Url, >> [Parameter(Mandatory=$true)] >> [string]$OutputPath >> ) >> >> $curlPath = Get-CurlPath >> $arguments = @("-L", "-o", "`"$OutputPath`"", "`"$Url`"") >> >> try { >> $process = Start-Process -FilePath $curlPath -ArgumentList $arguments -Wait -NoNewWindow -PassThru >> if ($process.ExitCode -ne 0) { >> throw "下载失败 (退出代码: $($process.ExitCode))" >> } >> >> if (-not (Test-Path $OutputPath)) { >> throw "文件未创建" >> } >> >> return Get-Item $OutputPath >> } catch { >> throw "安全下载失败: $_" >> } >> } >> >> function Uninstall-CurlTools { >> <# >> .SYNOPSIS >> 安全卸载CurlTools模块 >> #> >> $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 >> if (-not $module) { >> Write-Warning "未找到CurlTools模块" >> return >> } >> >> $uninstallPath = Join-Path $module.ModuleBase "Uninstall.ps1" >> if (Test-Path $uninstallPath) { >> Write-Host "运行卸载脚本..." -ForegroundColor Cyan >> & $uninstallPath >> } else { >> Write-Error "找不到卸载脚本" >> } >> } >> '@ >> >> # 卸载脚本 (Uninstall.ps1) >> $uninstallScript = @' >> param([switch]$Force) >> >> $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 >> if (-not $module) { >> Write-Warning "未找到CurlTools模块" >> return >> } >> >> $moduleDir = $module.ModuleBase >> Write-Host "`n正在卸载CurlTools模块..." -ForegroundColor Yellow >> Write-Host "模块位置: $moduleDir" >> >> # 用户确认 >> if (-not $Force) { >> $confirmation = Read-Host "确定要卸载CurlTools模块吗? (y/n)" >> if ($confirmation -ne 'y') { >> Write-Host "卸载已取消" -ForegroundColor Yellow >> return >> } >> } >> >> # 确保卸载前移除模块 >> try { >> Remove-Module CurlTools -Force -ErrorAction Stop >> Write-Host "✅ 模块已从会话中移除" -ForegroundColor Green >> } catch { >> Write-Warning "无法从会话中移除模块: $_" >> } >> >> # 删除模块目录 >> try { >> Write-Host "正在删除模块文件..." >> Remove-Item -Path $moduleDir -Recurse -Force -ErrorAction Stop >> Write-Host "✅ 模块文件已删除" -ForegroundColor Green >> } catch { >> Write-Error "❌ 删除模块文件失败: $_" >> Write-Host "请手动删除目录: $moduleDir" -ForegroundColor Red >> return >> } >> >> # 清理配置文件 >> $configDir = switch (Get-Platform) { >> "Linux" { Join-Path $HOME ".config" "curltools" } >> "macOS" { Join-Path $HOME "Library" "Application Support" "CurlTools" } >> default { Join-Path $env:APPDATA "CurlTools" } >> } >> >> if (Test-Path $configDir) { >> try { >> Remove-Item -Path $configDir -Recurse -Force >> Write-Host "✅ 配置文件已清理" -ForegroundColor Green >> } catch { >> Write-Warning "清理配置文件失败: $_" >> } >> } >> >> Write-Host "`n✅ 模块已成功卸载" -ForegroundColor Green >> Read-Host "按回车键退出" >> '@ >> >> # 4. 保存所有文件 >> $filesToCreate = @{ >> "CurlTools.psm1" = $moduleContent >> "Uninstall.ps1" = $uninstallScript >> } >> >> foreach ($fileName in $filesToCreate.Keys) { >> $filePath = Join-Path $moduleDir $fileName >> try { >> Set-Content -Path $filePath -Value $filesToCreate[$fileName] -Force >> Write-Host "✅ 创建文件: $filePath" -ForegroundColor Green >> } catch { >> Write-Error "❌ 创建文件失败: $filePath - $_" >> Read-Host "按回车键退出" >> exit 1 >> } >> } >> >> # 5. 测试模块安装 >> try { >> Write-Host "`n测试模块安装..." -ForegroundColor Cyan >> Import-Module $moduleDir -Force -ErrorAction Stop >> >> # 验证基本功能 >> $curlPath = Get-CurlPath >> if (-not $curlPath) { >> Write-Error "❌ Get-CurlPath 返回空值" >> throw "Get-CurlPath 返回空值" >> } >> >> Write-Host "✅ 模块安装成功! curl路径: $curlPath" -ForegroundColor Green >> >> # 显示使用说明 >> Write-Host "`n使用说明:" -ForegroundColor Magenta >> Write-Host "1. 导入模块: Import-Module CurlTools" -ForegroundColor Yellow >> Write-Host "2. 查看帮助: Get-Help Invoke-SecureDownload" -ForegroundColor Yellow >> Write-Host "3. 卸载模块: Uninstall-CurlTools" -ForegroundColor Yellow >> >> # 添加用户交互保持窗口打开 >> Read-Host "`n按回车键退出" >> exit 0 >> } catch { >> Write-Error "❌ 模块测试失败: $_" >> Read-Host "`n按回车键退出查看错误详情" >> exit 1 >> } >> ✅ 创建文件: C:\Program Files\WindowsPowerShell\Modules\CurlTools\Uninstall.ps1 ✅ 创建文件: C:\Program Files\WindowsPowerShell\Modules\CurlTools\CurlTools.psm1 测试模块安装... param( [switch]$Force ) # 1. 确定模块目录 $moduleDir = if ($PSVersionTable.PSEdition -eq "Core") { Join-Path $HOME ".local/share/powershell/Modules/CurlTools" } else { Join-Path $env:ProgramFiles "WindowsPowerShell/Modules/CurlTools" } # 2. 确保目录结构存在 $requiredDirs = @( $moduleDir, (Join-Path $moduleDir "Public"), (Join-Path $moduleDir "Private") ) foreach ($dir in $requiredDirs) { if (-not (Test-Path $dir)) { try { New-Item -ItemType Directory -Path $dir -Force | Out-Null Write-Host "✅ 创建目录: $dir" -ForegroundColor Green } catch { Write-Error "❌ 创建目录失败: $dir - $_" Read-Host "按回车键退出" exit 1 } } elseif ($Force) { Write-Warning "目录已存在: $dir (使用 -Force 覆盖)" } } # 3. 定义模块文件内容 # 模块主文件 (CurlTools.psm1) $moduleContent = @' # 导出公共函数 Export-ModuleMember -Function Get-CurlPath, Get-CurlVersion, Invoke-SecureDownload, Uninstall-CurlTools # 私有函数 function Get-Platform { if ($IsLinux) { "Linux" } elseif ($IsMacOS) { "macOS" } else { "Windows" } } # 公共函数 function Get-CurlPath { if ($IsWindows) { $curlPath = Get-Command curl -ErrorAction SilentlyContinue | Select-Object -ExpandProperty Source if (-not $curlPath) { $curlPath = "C:\Windows\System32\curl.exe" } return $curlPath } else { return (Get-Command curl).Source } } function Get-CurlVersion { $curlPath = Get-CurlPath $versionOutput = & $curlPath --version | Select-Object -First 1 if ($versionOutput -match 'curl (\d+\.\d+\.\d+)') { return @{ Version = $matches[1] FullOutput = $versionOutput } } else { throw "无法解析curl版本" } } function Invoke-SecureDownload { param( [Parameter(Mandatory=$true)] [ValidatePattern('^https?://')] [string]$Url, [Parameter(Mandatory=$true)] [string]$OutputPath ) $curlPath = Get-CurlPath $arguments = @("-L", "-o", "`"$OutputPath`"", "`"$Url`"") try { $process = Start-Process -FilePath $curlPath -ArgumentList $arguments -Wait -NoNewWindow -PassThru if ($process.ExitCode -ne 0) { throw "下载失败 (退出代码: $($process.ExitCode))" } if (-not (Test-Path $OutputPath)) { throw "文件未创建" } return Get-Item $OutputPath } catch { throw "安全下载失败: $_" } } function Uninstall-CurlTools { <# .SYNOPSIS 安全卸载CurlTools模块 #> $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 if (-not $module) { Write-Warning "未找到CurlTools模块" return } $uninstallPath = Join-Path $module.ModuleBase "Uninstall.ps1" if (Test-Path $uninstallPath) { Write-Host "运行卸载脚本..." -ForegroundColor Cyan & $uninstallPath } else { Write-Error "找不到卸载脚本" } } '@ # 卸载脚本 (Uninstall.ps1) $uninstallScript = @' param([switch]$Force) $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 if (-not $module) { Write-Warning "未找到CurlTools模块" return } $moduleDir = $module.ModuleBase Write-Host "`n正在卸载CurlTools模块..." -ForegroundColor Yellow Write-Host "模块位置: $moduleDir" # 用户确认 if (-not $Force) { $confirmation = Read-Host "确定要卸载CurlTools模块吗? (y/n)" if ($confirmation -ne 'y') { Write-Host "卸载已取消" -ForegroundColor Yellow return } } # 确保卸载前移除模块 try { Remove-Module CurlTools -Force -ErrorAction Stop Write-Host "✅ 模块已从会话中移除" -ForegroundColor Green } catch { Write-Warning "无法从会话中移除模块: $_" } # 删除模块目录 try { Write-Host "正在删除模块文件..." Remove-Item -Path $moduleDir -Recurse -Force -ErrorAction Stop Write-Host "✅ 模块文件已删除" -ForegroundColor Green } catch { Write-Error "❌ 删除模块文件失败: $_" Write-Host "请手动删除目录: $moduleDir" -ForegroundColor Red return } # 清理配置文件 $configDir = switch (Get-Platform) { "Linux" { Join-Path $HOME ".config" "curltools" } "macOS" { Join-Path $HOME "Library" "Application Support" "CurlTools" } default { Join-Path $env:APPDATA "CurlTools" } } if (Test-Path $configDir) { try { Remove-Item -Path $configDir -Recurse -Force Write-Host "✅ 配置文件已清理" -ForegroundColor Green } catch { Write-Warning "清理配置文件失败: $_" } } Write-Host "`n✅ 模块已成功卸载" -ForegroundColor Green Read-Host "按回车键退出" '@ # 4. 保存所有文件 $filesToCreate = @{ "CurlTools.psm1" = $moduleContent "Uninstall.ps1" = $uninstallScript } foreach ($fileName in $filesToCreate.Keys) { $filePath = Join-Path $moduleDir $fileName try { Set-Content -Path $filePath -Value $filesToCreate[$fileName] -Force Write-Host "✅ 创建文件: $filePath" -ForegroundColor Green } catch { Write-Error "❌ 创建文件失败: $filePath - $_" Read-Host "按回车键退出" exit 1 } } # 5. 测试模块安装 try { Write-Host "`n测试模块安装..." -ForegroundColor Cyan Import-Module $moduleDir -Force -ErrorAction Stop # 验证基本功能 $curlPath = Get-CurlPath if (-not $curlPath) { Write-Error "❌ Get-CurlPath 返回空值" throw "Get-CurlPath 返回空值" } Write-Host "✅ 模块安装成功! curl路径: $curlPath" -ForegroundColor Green # 显示使用说明 Write-Host "`n使用说明:" -ForegroundColor Magenta Write-Host "1. 导入模块: Import-Module CurlTools" -ForegroundColor Yellow Write-Host "2. 查看帮助: Get-Help Invoke-SecureDownload" -ForegroundColor Yellow Write-Host "3. 卸载模块: Uninstall-CurlTools" -ForegroundColor Yellow # 添加用户交互保持窗口打开 Read-Host "`n按回车键退出" exit 0 } catch { Write-Error "❌ 模块测试失败: $_" Read-Host "`n按回车键退出查看错误详情" exit 1 } : ❌ 模块测试失败: 无法将“Get-CurlPath”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括 路径,请确保路径正确,然后再试一次。 + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException 按回车键退出查看错误详情:
08-13
PS C:\Users\Administrator> # 卸载模块 - 安全版 >> param( >> [switch]$Force >> ) >> >> $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 >> if (-not $module) { >> Write-Warning "未找到 CurlTools 模块" >> return >> } >> >> $moduleDir = $module.ModuleBase >> Write-Host "`n正在卸载 CurlTools 模块..." -ForegroundColor Yellow >> Write-Host "模块位置: $moduleDir" >> >> # 用户确认 >> if (-not $Force) { >> $confirmation = Read-Host "确定要卸载 CurlTools 模块吗? (y/n)" >> if ($confirmation -ne 'y') { >> Write-Host "卸载已取消" -ForegroundColor Yellow >> return >> } >> } >> >> # 检查管理员权限 >> $isAdmin = $false >> if ($env:OS -like "Windows*") { >> $isAdmin = ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator) >> >> if (-not $isAdmin) { >> Write-Warning "建议使用管理员权限运行卸载脚本" >> $confirmation = Read-Host "继续卸载? (y/n)" >> if ($confirmation -ne 'y') { >> Write-Host "卸载已取消" -ForegroundColor Yellow >> return >> } >> } >> } >> >> # 确保卸载前移除模块 >> try { >> Remove-Module CurlTools -Force -ErrorAction Stop >> Write-Host "✅ 模块已从会话中移除" -ForegroundColor Green >> } catch { >> Write-Warning "无法从会话中移除模块: $_" >> } >> >> # 删除模块目录 >> try { >> Write-Host "正在删除模块文件..." >> Remove-Item -Path $moduleDir -Recurse -Force -ErrorAction Stop >> Write-Host "✅ 模块文件已删除" -ForegroundColor Green >> } catch { >> Write-Error "❌ 删除模块文件失败: $_" >> Write-Host "请手动删除目录: $moduleDir" -ForegroundColor Red >> return >> } >> >> # 清理配置文件 >> $configDir = switch (Get-Platform) { >> "Linux" { Join-Path $HOME ".config" "curltools" } >> "macOS" { Join-Path $HOME "Library" "Application Support" "CurlTools" } >> default { Join-Path $env:APPDATA "CurlTools" } >> } >> >> if (Test-Path $configDir) { >> try { >> Remove-Item -Path $configDir -Recurse -Force >> Write-Host "✅ 配置文件已清理" -ForegroundColor Green >> } catch { >> Write-Warning "清理配置文件失败: $_" >> } >> } >> >> Write-Host "`n✅ 模块已成功卸载" -ForegroundColor Green >> 警告: 未找到 CurlTools 模块 PS C:\Users\Administrator> # ===== 安全安装后测试 ===== >> try { >> # 重新加载模块 >> Write-Host "`n重新加载模块..." -ForegroundColor Cyan >> Remove-Module CurlTools -ErrorAction SilentlyContinue -Force >> Import-Module $moduleDir -Force -ErrorAction Stop >> >> # 创建测试状态变量 >> $TestFailed = $false >> >> # 基本功能测试 >> $tests = @( >> { >> Write-Host "测试: Get-CurlPath" -ForegroundColor Cyan >> $path = Get-CurlPath >> if (-not $path) { throw "返回空路径" } >> if (-not (Test-Path $path)) { throw "路径不存在: $path" } >> Write-Host "✅ 成功 | 路径: $path" -ForegroundColor Green >> $path >> } >> { >> Write-Host "测试: Get-CurlVersion" -ForegroundColor Cyan >> $version = Get-CurlVersion >> if (-not $version) { throw "返回空版本" } >> if (-not $version.Version) { throw "版本号解析失败" } >> Write-Host "✅ 成功 | 版本: $($version.Version)" -ForegroundColor Green >> $version >> } >> { >> Write-Host "测试: Invoke-SecureDownload" -ForegroundColor Cyan >> $url = "https://www.example.com" >> $out = Join-Path $env:TEMP "test_$(Get-Date -Format 'yyyyMMddHHmmss').html" >> $result = Invoke-SecureDownload -Url $url -OutputPath $out >> if (-not (Test-Path $out)) { throw "文件未创建" } >> $fileSize = (Get-Item $out).Length >> if ($fileSize -eq 0) { throw "文件大小为0字节" } >> Write-Host "✅ 成功 | 文件大小: $fileSize bytes" -ForegroundColor Green >> $out >> } >> { >> Write-Host "测试: 卸载功能" -ForegroundColor Cyan >> $uninstallPath = Join-Path $moduleDir "Uninstall.ps1" >> if (-not (Test-Path $uninstallPath)) { throw "卸载脚本不存在" } >> >> # 测试卸载函数是否存在 >> $uninstallCmd = Get-Command Uninstall-CurlTools -ErrorAction SilentlyContinue >> if (-not $uninstallCmd) { throw "卸载命令未注册" } >> >> Write-Host "✅ 成功 | 卸载功能正常" -ForegroundColor Green >> $uninstallPath >> } >> ) >> >> foreach ($test in $tests) { >> try { >> $result = & $test >> if (-not $result) { >> Write-Warning "⚠️ 测试返回空结果: $($test.ToString())" >> } >> } catch { >> Write-Error "❌ 测试失败: $($test.ToString()) - $_" >> $TestFailed = $true >> } >> } >> >> if ($TestFailed) { >> Write-Error "❌ 模块安装完成,但部分测试失败" -ForegroundColor Red >> return $false >> } else { >> Write-Host "`n✅ 模块安装成功并通过所有测试" -ForegroundColor Green >> >> # 显示卸载说明 >> Write-Host "`n卸载说明:" -ForegroundColor Magenta >> Write-Host "1. 运行命令: Uninstall-CurlTools" -ForegroundColor Yellow >> Write-Host "2. 或执行脚本: . '$uninstallPath'" -ForegroundColor Yellow >> >> return $true >> } >> } catch { >> Write-Error "❌ 模块加载失败: $_" >> return $false >> } >> 重新加载模块... # ===== 安全安装后测试 ===== try { # 重新加载模块 Write-Host "`n重新加载模块..." -ForegroundColor Cyan Remove-Module CurlTools -ErrorAction SilentlyContinue -Force Import-Module $moduleDir -Force -ErrorAction Stop # 创建测试状态变量 $TestFailed = $false # 基本功能测试 $tests = @( { Write-Host "测试: Get-CurlPath" -ForegroundColor Cyan $path = Get-CurlPath if (-not $path) { throw "返回空路径" } if (-not (Test-Path $path)) { throw "路径不存在: $path" } Write-Host "✅ 成功 | 路径: $path" -ForegroundColor Green $path } { Write-Host "测试: Get-CurlVersion" -ForegroundColor Cyan $version = Get-CurlVersion if (-not $version) { throw "返回空版本" } if (-not $version.Version) { throw "版本号解析失败" } Write-Host "✅ 成功 | 版本: $($version.Version)" -ForegroundColor Green $version } { Write-Host "测试: Invoke-SecureDownload" -ForegroundColor Cyan $url = "https://www.example.com" $out = Join-Path $env:TEMP "test_$(Get-Date -Format 'yyyyMMddHHmmss').html" $result = Invoke-SecureDownload -Url $url -OutputPath $out if (-not (Test-Path $out)) { throw "文件未创建" } $fileSize = (Get-Item $out).Length if ($fileSize -eq 0) { throw "文件大小为0字节" } Write-Host "✅ 成功 | 文件大小: $fileSize bytes" -ForegroundColor Green $out } { Write-Host "测试: 卸载功能" -ForegroundColor Cyan $uninstallPath = Join-Path $moduleDir "Uninstall.ps1" if (-not (Test-Path $uninstallPath)) { throw "卸载脚本不存在" } # 测试卸载函数是否存在 $uninstallCmd = Get-Command Uninstall-CurlTools -ErrorAction SilentlyContinue if (-not $uninstallCmd) { throw "卸载命令未注册" } Write-Host "✅ 成功 | 卸载功能正常" -ForegroundColor Green $uninstallPath } ) foreach ($test in $tests) { try { $result = & $test if (-not $result) { Write-Warning "⚠️ 测试返回空结果: $($test.ToString())" } } catch { Write-Error "❌ 测试失败: $($test.ToString()) - $_" $TestFailed = $true } } if ($TestFailed) { Write-Error "❌ 模块安装完成,但部分测试失败" -ForegroundColor Red return $false } else { Write-Host "`n✅ 模块安装成功并通过所有测试" -ForegroundColor Green # 显示卸载说明 Write-Host "`n卸载说明:" -ForegroundColor Magenta Write-Host "1. 运行命令: Uninstall-CurlTools" -ForegroundColor Yellow Write-Host "2. 或执行脚本: . '$uninstallPath'" -ForegroundColor Yellow return $true } } catch { Write-Error "❌ 模块加载失败: $_" return $false } : ❌ 模块加载失败: 无法将参数绑定到参数“Name”,因为该参数是空值。 + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException False PS C:\Users\Administrator> # 1. 模块目录确定 >> $moduleDir = if ($PSVersionTable.PSEdition -eq "Core") { >> Join-Path $HOME ".local/share/powershell/Modules/CurlTools" >> } else { >> Join-Path $env:ProgramFiles "WindowsPowerShell/Modules/CurlTools" >> } >> >> # 2. 创建目录结构 >> # ... (省略目录创建代码) >> >> # 3. 定义所有函数内容 >> # ... (省略函数定义代码) >> >> # 4. 保存所有文件 >> # ... (省略文件保存代码) >> >> # 5. 添加安全的卸载脚本 >> $uninstallScript = @' >> # 安全卸载脚本内容(如上所示) >> '@ >> >> $uninstallPath = Join-Path $moduleDir "Uninstall.ps1" >> Set-Content -Path $uninstallPath -Value $uninstallScript -Force >> >> # 6. 安全执行安装后测试 >> $testResult = . { >> # 安全测试代码(如上所示) >> } >> >> if ($testResult) { >> Write-Host "`n✅ CurlTools 模块安装成功" -ForegroundColor Green >> } else { >> Write-Host "`n⚠️ 模块安装完成,但测试未通过" -ForegroundColor Yellow >> } >> >> # 7. 显示使用说明 >> Write-Host "`n使用说明:" -ForegroundColor Cyan >> Write-Host "1. 导入模块: Import-Module CurlTools" -ForegroundColor Green >> Write-Host "2. 查看帮助: Get-Help about_CurlTools" -ForegroundColor Green >> Write-Host "3. 卸载模块: . '$uninstallPath'" -ForegroundColor Green >> Set-Content : 未能找到路径“C:\Program Files\WindowsPowerShell\Modules\CurlTools\Uninstall.ps1”的一部分。 所在位置 行:23 字符: 1 + Set-Content -Path $uninstallPath -Value $uninstallScript -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (C:\Program File...s\Uninstall.ps1:String) [Set-Content], DirectoryNotFo undException + FullyQualifiedErrorId : GetContentWriterDirectoryNotFoundError,Microsoft.PowerShell.Commands.SetContentCommand ⚠️ 模块安装完成,但测试未通过 使用说明: 1. 导入模块: Import-Module CurlTools 2. 查看帮助: Get-Help about_CurlTools 3. 卸载模块: . 'C:\Program Files\WindowsPowerShell\Modules\CurlTools\Uninstall.ps1' PS C:\Users\Administrator> # 手动运行安装后测试 >> Import-Module CurlTools -Force >> . (Join-Path (Get-Module CurlTools).ModuleBase "Test-Installation.ps1") >> Import-Module : 未能加载指定的模块“CurlTools”,因为在任何模块目录中都没有找到有效模块文件。 所在位置 行:2 字符: 1 + Import-Module CurlTools -Force + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ResourceUnavailable: (CurlTools:String) [Import-Module], FileNotFoundException + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand Join-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。 所在位置 行:3 字符: 14 + . (Join-Path (Get-Module CurlTools).ModuleBase "Test-Installation.ps1 ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path],ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom mand PS C:\Users\Administrator> # 方法1: 使用卸载函数 >> Uninstall-CurlTools >> >> # 方法2: 运行卸载脚本 >> . (Join-Path (Get-Module CurlTools).ModuleBase "Uninstall.ps1") >> Uninstall-CurlTools : 无法将“Uninstall-CurlTools”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写 ,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:2 字符: 1 + Uninstall-CurlTools + ~~~~~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (Uninstall-CurlTools:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Join-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。 所在位置 行:5 字符: 14 + . (Join-Path (Get-Module CurlTools).ModuleBase "Uninstall.ps1") + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Join-Path],ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.JoinPathCom mand PS C:\Users\Administrator> function Uninstall-CurlTools { >> <# >> .SYNOPSIS >> 安全卸载 CurlTools 模块 >> >> .DESCRIPTION >> 此函数提供交互式卸载体验,避免意外关闭 PowerShell 窗口 >> #> >> $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 >> if (-not $module) { >> Write-Warning "未找到 CurlTools 模块" >> return >> } >> >> $uninstallPath = Join-Path $module.ModuleBase "Uninstall.ps1" >> if (-not (Test-Path $uninstallPath)) { >> Write-Error "找不到卸载脚本" >> return >> } >> >> Write-Host "运行安全卸载程序..." -ForegroundColor Cyan >> . $uninstallPath >> } >> PS C:\Users\Administrator>
08-13
# Check massgrave.dev for more details write-host Write-Host "The current command (irm https://massgrave.dev/get | iex) will be retired in the future." Write-Host -ForegroundColor Green "Use the new command (irm https://get.activated.win | iex) moving forward." write-host if ($ExecutionContext.SessionState.LanguageMode.value__ -ne 0) { $ExecutionContext.SessionState.LanguageMode Write-Host "Windows PowerShell is not running in Full Language Mode." Write-Host "Help - https://massgrave.dev/fix_powershell" -ForegroundColor White -BackgroundColor Blue return } function Check3rdAV { $avList = Get-CimInstance -Namespace root\SecurityCenter2 -Class AntiVirusProduct | Where-Object { $_.displayName -notlike '*windows*' } | Select-Object -ExpandProperty displayName if ($avList) { Write-Host '3rd party Antivirus might be blocking the script - ' -ForegroundColor White -BackgroundColor Blue -NoNewline Write-Host " $($avList -join ', ')" -ForegroundColor DarkRed -BackgroundColor White } } function CheckFile { param ([string]$FilePath) if (-not (Test-Path $FilePath)) { Check3rdAV Write-Host "Failed to create MAS file in temp folder, aborting!" Write-Host "Help - https://massgrave.dev/troubleshoot" -ForegroundColor White -BackgroundColor Blue throw } } [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 $URLs = @( 'https://raw.githubusercontent.com/massgravel/Microsoft-Activation-Scripts/60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd/MAS/All-In-One-Version-KL/MAS_AIO.cmd', 'https://dev.azure.com/massgrave/Microsoft-Activation-Scripts/_apis/git/repositories/Microsoft-Activation-Scripts/items?path=/MAS/All-In-One-Version-KL/MAS_AIO.cmd&versionType=Commit&version=60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd', 'https://git.activated.win/massgrave/Microsoft-Activation-Scripts/raw/commit/60c99742ce9ff1c675c6e381e17b0f4ccf1a57bd/MAS/All-In-One-Version-KL/MAS_AIO.
03-08
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值