Write-Warning 帮助信息

本文介绍PowerShell中Write-Warning命令的用法,包括如何输出警告信息及其控制方式。此外还提供了示例代码,帮助读者更好地理解如何在脚本中应用此命令。
 
如下说明是翻译: help write-warning 产生的帮助信息.
译者: Edengundam(马涛)
 
Write-Warning
 
大纲
输出警告信息.
 
语法
Write-Warning [-message] <string> [<CommonParameters>]
 
详细描述
输出警告信息. 错误信息被直接发往宿主(host). 根据$WarningPreference 变量的值, 决定该警告信息是否被输出. 输入$WarningPreference可以查看该变量的当前值. 为当前会话(session)设置该变量值, 输入$WarningPreference = "<value>". 合法取值为: SilentlyContinue, Stop, Continue Inquire. 如果设置为SilentlyContinue, Write-Warning 将不会向控制台输出警告信息. 如果警告消息被显示,则会被高亮显示.
 
参数
 
-message <string>
指定警告信息.
 
强制参数?
True
参数位置?
1
默认值
None
允许从管道绑定输入?  
True (传值)
允许通配符扩展
False
 
<公共参数>
此命令支持公共参数: -Verbose, -Debug, -ErrorAction, -ErrorVariable, and -OutVariable. 更多信息, 输入, "get-help about_commonparameters".
 
输入类型
字符串(String)
 
返回类型
(None)
 
注意
 
确定像$WarningPreference变量的合法值, 可以尝试赋值一些奇怪的字符串. 这将会导致错误信息中打印出该参数的合法变量值.
 
更多信息, 输入 "Get-Help Write-Warning -detailed". 需要技术信息, 输入 "Get-Help Write-Warning -full".
 
如果需要为该命令提供多个参数, 请使用逗号进行分隔. 例如, "<parameter-name> <value1>, <value2>".
 
1
 
C:/PS>write-warning "This is only a test warning."
 
如果$WarningPreference设置不是SilentlyContinue, 此命令将会输出 "WARNING: This is only a test warning.".
 
相关链接
Write-Debug
Write-Error
Write-Host
Write-Output
Write-Progress
Write-Verbose
 
# 卸载模块 $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> # 导出公共函数 >> 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 "找不到卸载脚本" >> } >> } >> Export-ModuleMember : 只能从模块内调用 Export-ModuleMember cmdlet。 所在位置 行:2 字符: 1 + Export-ModuleMember -Function Get-CurlPath, Get-CurlVersion, Invoke-S ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : PermissionDenied: (:) [Export-ModuleMember], InvalidOperationException + FullyQualifiedErrorId : Modules_CanOnlyExecuteExportModuleMemberInsideAModule,Microsoft.PowerShell.Commands.Expo rtModuleMemberCommand 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. 定义模块文件内容 >> $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 "找不到卸载脚本" >> } >> } >> '@ >> >> # 4. 卸载脚本内容 (保持不变) >> $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 "按回车键退出" >> '@ >> >> # 5. 保存所有文件 >> $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 >> >> # 调试:显示文件前5行 >> $contentPreview = Get-Content -Path $filePath -TotalCount 5 >> Write-Host "文件预览:" >> $contentPreview | ForEach-Object { Write-Host " $_" } >> } catch { >> Write-Error "❌ 创建文件失败: $filePath - $_" >> Read-Host "按回车键退出" >> exit 1 >> } >> } >> >> # 6. 测试模块安装(增强版) >> try { >> Write-Host "`n测试模块安装..." -ForegroundColor Cyan >> >> # 1. 导入模块 >> Import-Module $moduleDir -Force -ErrorAction Stop >> Write-Host "✅ 模块导入成功" -ForegroundColor Green >> >> # 2. 验证模块已加载 >> $moduleInfo = Get-Module CurlTools >> if (-not $moduleInfo) { >> throw "模块加载后未在会话中找到" >> } >> Write-Host "✅ 模块信息: $($moduleInfo.Version)" -ForegroundColor Green >> >> # 3. 验证导出函数 >> $commands = Get-Command -Module CurlTools >> if (-not $commands) { >> throw "模块未导出任何命令" >> } >> Write-Host "✅ 导出命令: $($commands.Name -join ', ')" -ForegroundColor Green >> >> # 4. 验证基本功能 >> $curlPath = Get-CurlPath >> if (-not $curlPath) { >> throw "Get-CurlPath 返回空值" >> } >> Write-Host "✅ curl路径: $curlPath" -ForegroundColor Green >> >> # 5. 测试下载功能(可选) >> try { >> $testFile = Join-Path $env:TEMP "test_download_$(Get-Date -Format 'yyyyMMddHHmmss').txt" >> $result = Invoke-SecureDownload -Url "https://example.com" -OutputPath $testFile >> if (Test-Path $testFile) { >> Write-Host "✅ 下载测试成功: $testFile" -ForegroundColor Green >> Remove-Item $testFile -Force >> } >> } catch { >> Write-Warning "⚠️ 下载测试失败: $_ (不影响整体安装)" >> } >> >> # 显示使用说明 >> 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 "❌ 模块测试失败: $_" >> >> # 显示详细错误信息 >> Write-Host "`n调试信息:" -ForegroundColor Red >> Write-Host "模块目录: $moduleDir" -ForegroundColor Red >> Write-Host "模块内容:" >> if (Test-Path "$moduleDir\CurlTools.psm1") { >> Get-Content "$moduleDir\CurlTools.psm1" -TotalCount 20 | ForEach-Object { Write-Host " $_" } >> } >> >> Read-Host "`n按回车键退出查看错误详情" >> exit 1 >> } >> ✅ 创建文件: C:\Program Files\WindowsPowerShell\Modules\CurlTools\Uninstall.ps1 文件预览: param([switch]$Force) $module = Get-Module CurlTools -ListAvailable | Select-Object -First 1 if (-not $module) { Write-Warning "未找到CurlTools模块" ✅ 创建文件: C:\Program Files\WindowsPowerShell\Modules\CurlTools\CurlTools.psm1 文件预览: # 导出公共函数 Export-ModuleMember -Function Get-CurlPath, Get-CurlVersion, Invoke-SecureDownload, Uninstall-CurlTools # 私有函数 function Get-Platform { 测试模块安装... ✅ 模块导入成功 ✅ 模块信息: 0.0 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. 定义模块文件内容 $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 "找不到卸载脚本" } } '@ # 4. 卸载脚本内容 (保持不变) $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 "按回车键退出" '@ # 5. 保存所有文件 $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 # 调试:显示文件前5行 $contentPreview = Get-Content -Path $filePath -TotalCount 5 Write-Host "文件预览:" $contentPreview | ForEach-Object { Write-Host " $_" } } catch { Write-Error "❌ 创建文件失败: $filePath - $_" Read-Host "按回车键退出" exit 1 } } # 6. 测试模块安装(增强版) try { Write-Host "`n测试模块安装..." -ForegroundColor Cyan # 1. 导入模块 Import-Module $moduleDir -Force -ErrorAction Stop Write-Host "✅ 模块导入成功" -ForegroundColor Green # 2. 验证模块已加载 $moduleInfo = Get-Module CurlTools if (-not $moduleInfo) { throw "模块加载后未在会话中找到" } Write-Host "✅ 模块信息: $($moduleInfo.Version)" -ForegroundColor Green # 3. 验证导出函数 $commands = Get-Command -Module CurlTools if (-not $commands) { throw "模块未导出任何命令" } Write-Host "✅ 导出命令: $($commands.Name -join ', ')" -ForegroundColor Green # 4. 验证基本功能 $curlPath = Get-CurlPath if (-not $curlPath) { throw "Get-CurlPath 返回空值" } Write-Host "✅ curl路径: $curlPath" -ForegroundColor Green # 5. 测试下载功能(可选) try { $testFile = Join-Path $env:TEMP "test_download_$(Get-Date -Format 'yyyyMMddHHmmss').txt" $result = Invoke-SecureDownload -Url "https://example.com" -OutputPath $testFile if (Test-Path $testFile) { Write-Host "✅ 下载测试成功: $testFile" -ForegroundColor Green Remove-Item $testFile -Force } } catch { Write-Warning "⚠️ 下载测试失败: $_ (不影响整体安装)" } # 显示使用说明 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 "❌ 模块测试失败: $_" # 显示详细错误信息 Write-Host "`n调试信息:" -ForegroundColor Red Write-Host "模块目录: $moduleDir" -ForegroundColor Red Write-Host "模块内容:" if (Test-Path "$moduleDir\CurlTools.psm1") { Get-Content "$moduleDir\CurlTools.psm1" -TotalCount 20 | ForEach-Object { Write-Host " $_" } } Read-Host "`n按回车键退出查看错误详情" exit 1 } : ❌ 模块测试失败: 模块未导出任何命令 + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException 调试信息: 模块目录: C:\Program Files\WindowsPowerShell\Modules\CurlTools 模块内容: # 导出公共函数 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 } } 按回车键退出查看错误详情:
最新发布
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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值