在PowerShell中Write-Host/Output的区别

本文详细介绍了PowerShell中的Write-Host和Write-Output命令的区别,通过实例展示了它们如何在控制台上输出不同类型的值,并探讨了它们在参数支持上的差异。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

许多用PowerShell的人一定会发现在PowerShell中有Write-Outup和Write-Host这两个命令,但是很多人都不是很清楚两者的区别。

表明上看这两者都可以输出字符到Console上,但是他们到底有什么不同呢?下面让我们一起测试看看:

Write-Host的测试:

Function Test   
{   
    Write-Host "Result A"   
    "Result B"   
}   
  
  
Test   
Result A   
Result B   
  
  
$a = test   
Result A   
  
$a   
Result B   

当直接运行方法的时候,会得到所有的两个输出,但是write-host是直接输出到控制台的,所以这个值是无法通过变量获得的。

Write-Output的测试:

Function WriteTest($amount, $rate=0.8)   
{  
    $amount * $rate  
}  
Function WriteOutputTest($amount, $rate=0.8)   
{  
    Write-Output $amount * $rate  
}  
  
WriteTest 100  
  
WriteOutputTest 100  
得到的结果是:
80  
  
100  
*  
0.8  
当然两者还有个字不同的参数支持,比如Write-Host支持NewLine等参数,实际上两者的区别是Write-Host只是仅仅将字符串输出到屏幕,而Write-Output则是可以把对象传递输出,再来看看如下一个简单的例子对比便知:
PS C:\Users> $process = get-process  
PS C:\Users> Write-Host $process  
System.Diagnostics.Process (AcroRd32) System.Diagnostics.Process (AcroRd32) System.Dia  
.Diagnostics.Process (AdobeARM) System.Diagnostics.Process (AlipaySecSvc) System.Diagn  
gnostics.Process (atieclxx) System.Diagnostics.Process (atiesrxx) System.Diagnostics.P  
s.Process (CcmExec) System.Diagnostics.Process (CISVC) System.Diagnostics.Process (com  
ess (conhost) System.Diagnostics.Process (conhost) System.Diagnostics.Process (csrss) 


PS C:\Users> $process = get-process  
PS C:\Users> Write-Output $process  
  
Handles  NPM(K)    PM(K)      WS(K) VM(M)   CPU(s)     Id ProcessName  
-------  ------    -----      ----- -----   ------     -- -----------  
    208      16     7776       9024    99     1.48   1416 AcroRd32  
    333      42   151452     113888   362    49.22   5104 AcroRd32  
     68       9     1880        664    70     0.37   5328 acrotray  
    304      19     4400       1476   107     2.96   4512 AdobeARM  
    224      15     3836       5020    59 5,413.81   1800 AlipaySecSvc  
     72       8     1352       1116    43     1.33   1696 armsvc  
    139      10     2204        872    73     0.33   3800 atieclxx  
    121       7     1640       1056    29     1.11    924 atiesrxx  



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
# 卸载模块 $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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值