Powershell commands 之 Add-History

本文介绍如何使用PowerShell命令Add-History将特定命令添加到会话历史记录,并探讨其与invoke-history结合使用的应用场景,例如在脚本异常后重新执行之前保存的命令。

 Add-History
作用:
      添加条目到当前会话历史记录中去。
语法:
      Add-History [[-inputObject] <PSObject[]>] [-passthru] [<CommonParameters>]
例子 1:
        get-history -id 6  -count 5 | add-history
管道符左边的作用是取得当前会话中第2-6条记录,-id 6表示到第6条截止,-count 5表示从第6条记录开始往前取5条记录6,5,4,3,2.
管道符右边接受从左边穿过来的参数,加到历史记录末尾(last , last but 4)。
 例子 2:
         add-history -inputobject (import-clixml c:/temp/history01.xml)
把history01.xml中的命令添加到历史记录末尾。

这个command有什么用?
结合invoke-history可能会有用。
比如如果脚本在某一步出错了,你可以把出错的command保存起来,等异常处理后再把前面保存的command加到history中去,然后invoke-history,重新参试运行以前出错的命令。

PS C:\Users\Administrator> function Get-CurlPath { >> # 从配置获取curl路径 >> $configPath = "E:\CurlTools\Config\config.json" >> if (-not (Test-Path $configPath)) { >> throw "Config file not found: $configPath" >> } >> >> $config = Get-Content $configPath | ConvertFrom-Json >> $curlPath = Join-Path $config.CurlPath "curl.exe" >> >> if (-not (Test-Path $curlPath)) { >> throw "curl.exe not found! Check path: $curlPath" >> } >> >> return $curlPath >> } >> >> function Get-CurlVersion { >> # 获取curl版本信息 >> $curlPath = Get-CurlPath >> $version = & $curlPath --version | Select-Object -First 1 >> return $version >> } >> >> function Invoke-SecureDownload { >> param( >> [Parameter(Mandatory=$true)] >> [string]$Url, >> >> [Parameter(Mandatory=$true)] >> [string]$OutputPath >> ) >> >> # 检查域名是否允许 >> $domain = ([System.Uri]$Url).Host >> $configPath = "E:\CurlTools\Config\config.json" >> >> if (-not (Test-Path $configPath)) { >> throw "Config file not found: $configPath" >> } >> >> $config = Get-Content $configPath | ConvertFrom-Json >> >> # 增强域名检查逻辑 >> if (-not $config.AllowedDomains) { >> throw "No allowed domains configured!" >> } >> >> if ($domain -notin $config.AllowedDomains) { >> throw "Domain not allowed: $domain! Allowed domains: $($config.AllowedDomains -join ', ')" >> } >> >> # 确保输出目录存在 >> $outputDir = Split-Path $OutputPath -Parent >> if (-not (Test-Path $outputDir)) { >> New-Item -Path $outputDir -ItemType Directory -Force | Out-Null >> } >> >> # 执行下载 >> $curlPath = Get-CurlPath >> & $curlPath -L -o $OutputPath $Url >> >> if ($LASTEXITCODE -ne 0) { >> throw "Download failed! Error code: $LASTEXITCODE" >> } >> >> Write-Host "Download successful: $OutputPath" -ForegroundColor Green >> } >> PS C:\Users\Administrator> { >> "CurlPath": "E:\\CurlTools\\CurlBin", >> "AllowedDomains": [ >> "example.com", >> "github.com", >> "microsoft.com" >> ] >> } >> 所在位置 行:2 字符: 15 + "CurlPath": "E:\\CurlTools\\CurlBin", + ~ 表达式或语句中包含意外的标记“:”。 + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : UnexpectedToken PS C:\Users\Administrator> # 设置UTF-8编码 >> [Console]::OutputEncoding = [System.Text.Encoding]::UTF8 >> $OutputEncoding = [System.Text.Encoding]::UTF8 >> chcp 65001 | Out-Null >> >> # 添加模块路径 >> $modulePath = "E:\CurlTools\Modules" >> if ($env:PSModulePath -notlike "*$modulePath*") { >> $env:PSModulePath += ";$modulePath" >> } >> >> # 自动加载模块 >> Import-Module CurlTools -Force -ErrorAction SilentlyContinue >> PS C:\Users\Administrator> graph TD >> A[PowerShell会话] --> B[$PROFILE] >> B --> C[自动加载模块] >> C --> D[CurlTools模块] >> D --> E[Get-CurlPath] >> D --> F[Get-CurlVersion] >> D --> G[Invoke-SecureDownload] >> A --> H[配置文件] >> H --> I[config.json] >> H --> J[日志文件] >> G --> K[域名检查] >> G --> L[安全下载] >> K --> M[AllowedDomains验证] >> L --> N[curl.exe执行] >> graph : 无法将“graph”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:1 字符: 1 + graph TD + ~~~~~ + CategoryInfo : ObjectNotFound: (graph:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException A[PowerShell会话] : 无法将“A[PowerShell会话]”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:2 字符: 5 + A[PowerShell会话] --> B[$PROFILE] + ~~~~~~~~~~~~~~~ + CategoryInfo : ObjectNotFound: (A[PowerShell会话]:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException B : 无法将“B”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:3 字符: 5 + B --> C[自动加载模块] + ~ + CategoryInfo : ObjectNotFound: (B:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException C : 无法将“C”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:4 字符: 5 + C --> D[CurlTools模块] + ~ + CategoryInfo : ObjectNotFound: (C:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException D : 无法将“D”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:5 字符: 5 + D --> E[Get-CurlPath] + ~ + CategoryInfo : ObjectNotFound: (D:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException D : 无法将“D”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:6 字符: 5 + D --> F[Get-CurlVersion] + ~ + CategoryInfo : ObjectNotFound: (D:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException D : 无法将“D”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:7 字符: 5 + D --> G[Invoke-SecureDownload] + ~ + CategoryInfo : ObjectNotFound: (D:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException A : 无法将“A”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:8 字符: 5 + A --> H[配置文件] + ~ + CategoryInfo : ObjectNotFound: (A:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException Get-History : 无法绑定参数“Count”。无法将值“I[config.json]”转换为类型“System.Int32”。错误:“输入字符串的格式不正确。” 所在位置 行:9 字符: 11 + H --> I[config.json] + ~~~~~~~~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-History],ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetHistoryCommand Get-History : 无法绑定参数“Count”。无法将值“J[日志文件]”转换为类型“System.Int32”。错误:“输入字符串的格式不正确。 ” 所在位置 行:10 字符: 11 + H --> J[日志文件] + ~~~~~~~ + CategoryInfo : InvalidArgument: (:) [Get-History],ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetHistoryCommand G : 无法将“G”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:11 字符: 5 + G --> K[域名检查] + ~ + CategoryInfo : ObjectNotFound: (G:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException G : 无法将“G”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:12 字符: 5 + G --> L[安全下载] + ~ + CategoryInfo : ObjectNotFound: (G:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException K : 无法将“K”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:13 字符: 5 + K --> M[AllowedDomains验证] + ~ + CategoryInfo : ObjectNotFound: (K:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException L : 无法将“L”项识别为 cmdlet、函数、脚本文件或可运行程序的名称。请检查名称的拼写,如果包括路径,请确保路径正确,然后再试一次。 所在位置 行:14 字符: 5 + L --> N[curl.exe执行] + ~ + CategoryInfo : ObjectNotFound: (L:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException PS C:\Users\Administrator> # 在模块顶部添加日志函数 >> function Write-Log { >> param( >> [string]$Message, >> [ValidateSet('Info','Warning','Error')] >> [string]$Level = 'Info' >> ) >> >> $logPath = "E:\CurlTools\Logs\curl_tools_$(Get-Date -Format 'yyyyMMdd').log" >> $logEntry = "[$(Get-Date -Format 'yyyy-MM-dd HH:mm:ss')] [$Level] $Message" >> >> Add-Content -Path $logPath -Value $logEntry >> } >> >> # 在关键位置添加日志记录 >> function Invoke-SecureDownload { >> # ... 其他代码 ... >> >> Write-Log -Message "Starting download: $Url" -Level 'Info' >> >> try { >> # ... 域名检查 ... >> Write-Log -Message "Domain allowed: $domain" >> >> # ... 执行下载 ... >> Write-Log -Message "Download completed: $OutputPath" >> } >> catch { >> Write-Log -Message "Download failed: $_" -Level 'Error' >> throw $_ >> } >> } >> PS C:\Users\Administrator> $url = "https://subdomain.example.com/path" >> $domain = ([System.Uri]$url).Host # 返回 "subdomain.example.com" >> PS C:\Users\Administrator> if (-not (Test-Path $config.CurlPath)) { >> throw "CurlPath directory not found: $($config.CurlPath)" >> } >> Test-Path : 无法将参数绑定到参数“Path”,因为该参数是空值。 所在位置 行:1 字符: 21 + if (-not (Test-Path $config.CurlPath)) { + ~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [Test-Path],ParameterBindingValidationException + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.TestPathCom mand PS C:\Users\Administrator>
最新发布
08-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值