【发布时间】:2020-08-31 06:15:58
【问题描述】:
如果我在 powershell 中定义了以下字符串(或任何其他有效命令的值)
$foo = "Get-ChildItem -filter *.cmake"
如何将该字符串(不执行命令)注入到 powershell 历史记录中,以便下次用户按下向上箭头时,此命令可用。
【问题讨论】:
标签: powershell
如果我在 powershell 中定义了以下字符串(或任何其他有效命令的值)
$foo = "Get-ChildItem -filter *.cmake"
如何将该字符串(不执行命令)注入到 powershell 历史记录中,以便下次用户按下向上箭头时,此命令可用。
【问题讨论】:
标签: powershell
更新 -- 修复了前面示例中提供的设置无法正常工作
为了两个不同的目的,需要更新两个历史记录。
向上/向下箭头 PowerShell 历史记录
更新控制台主机历史记录将更改任何新创建的 Window PowerShell 显示的向上/向下箭头历史记录
Add-Content -path (Get-PSReadlineOption).HistorySavePath "Get-ChildItem -filter *.cmake"
PowerShell 历史
通过Add-History更新会修改通过Get-History显示历史的历史文件
您可以使用以下命令为每个文档添加历史记录
Add-History
[[-InputObject] <PSObject[]>]
[-Passthru]
[<CommonParameters>]
步骤:
使用您在示例中提供的实际示例命令
PS C:\> $foo = [pscustomobject]@{
>> CommandLine = "Get-ChildItem -filter *.cmake"
>> ExecutionStatus = "Completed"
>> StartExecutionTime = "8/31/2020 2:44:16 AM"
>> EndExecutionTime = "8/31/2020 2:44:16 AM"
>> }
PS C:\> $commands = @()
PS C:\> $commands += $foo
PS C:\> Add-History -InputObject $commands -PassThru
Id CommandLine
-- -----------
133 Get-ChildItem -filter *.cmake
passthru 仅用于显示插入的输出 https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/add-history?view=powershell-7
【讨论】:
Add-History: Cannot add history because the input object has a format that is not valid.