如何在每次调用时重新构建Hashtable的特定键?

huangapple go评论56阅读模式
英文:

How to rebuild a specific key of a Hashtable every time its called?

问题

I find PowerShell growing on me and consequently I am spending allot of time on the terminal.
我发现PowerShell越来越适合我,因此我在终端上花了很多时间。

I have many hash tables in my profile, for various uses but mostly to keep common data right under my fingers.
我的个人配置文件中有许多哈希表,用于各种用途,但主要是为了将常用数据随手可得。

I have one such Hashtable here:
我这里有一个这样的哈希表:

$SYS = [Ordered]@{
$SYS = [Ordered]@{
$SYS = [Ordered]@{
$SYS = [Ordered]@{
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ShareXUploadHistory = Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")
ShareXUploadHistory = Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")
ShareXUploadHistory = Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")
ShareXUploadHistory = Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")
}

The ShareXUploadHistory key reads from a file that is constantly changing, I would like to rebuild the key, so that every time I call $SYS.ShareXUploadHistory the json data will be based on the latest state of the file History.Json NOT when my $Profile was loaded.
ShareXUploadHistory键从一个不断变化的文件中读取数据,我希望重新构建该键,以便每次调用$SYS.ShareXUploadHistory时,JSON数据将基于文件History.Json的最新状态,而不是我的$Profile加载时的状态。

I tried the following but the key is not updated when I call $SYS.ShareXUploadHistory:
我尝试了以下方法,但在调用$SYS.ShareXUploadHistory时键未更新:

$SYS = [Ordered]@{
$SYS = [Ordered]@{
$SYS = [Ordered]@{
$SYS = [Ordered]@{
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
Posh = "C:\Program Files\PowerShell\7\pwsh.exe"
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter .html
ShareXUploadHistory = . {Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")}
ShareXUploadHistory = . {Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")}
ShareXUploadHistory = . {Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")}
ShareXUploadHistory = . {Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users...\Documents\Sharex\History.Json")}
}

英文:

I find PowerShell growing on me and consequently I am spending allot of time on the terminal.
I have many hash tables in my profile, for various uses but mostly to keep common data right under my fingers.

I have one such Hashtable here:

$SYS = [Ordered]@{
	Posh = "C:\Program Files\PowerShell\pwsh.exe"
	ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter *.html*
	ShareXUploadHistory = Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users\...\Documents\Sharex\History.Json")
	}

The ShareXUploadHistory key reads from a file that is constantly changing, I would like to rebuild the key, so that every time I call $SYS.ShareXUploadHistory the json data will be based on the latest state of the file History.Json NOT when my $Profile was loaded.

I tried the following but the key is not updated when I call $SYS.ShareXUploadHistory:

$SYS = [Ordered]@{
	Posh = "C:\Program Files\PowerShell\pwsh.exe"
	ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter *.html*
	ShareXUploadHistory = . {Convertfrom-Json -Inputobject (Get-Content -Path "C:\Users\...\Documents\Sharex\History.Json")}
	}

答案1

得分: 3

一种方法是将其添加为 PSScriptProperty,然后每次调用成员 ($SYS.ShareXUploadHistory) 时,它将被重新评估。不足之处是,由于这是一个有序字典而不是 pscustomobject,因此您将无法将其视为键。

英文:

One way to do it can be adding it as PSScriptProperty, then each time you invoke the member ($SYS.ShareXUploadHistory) it would be re-evaluated. Downside is, since this is an ordered dictionary and not a pscustomobject you wouldn't see it as a Key.

$SYS = [ordered]@{
   Posh        = 'C:\Program Files\PowerShell\pwsh.exe'
   ReflectLogs = Get-ChildItem -Path 'path\to\stuff' -Filter *.html*
}

$SYS.PSObject.Members.Add([psscriptproperty]::new(
   'ShareXUploadHistory',
   { Get-Content 'path\to\Sharex\History.Json' -Raw | ConvertFrom-Json }))

$SYS

答案2

得分: 1

以下是您要翻译的内容:

我想出的解决方案涉及将分配给键的代码包装在 {} 中,当调用特定键时,在其前加上 .,后跟一个空格,不像Santiago的回答那么明显,但它有效:

$SYS = [Ordered]@{
    Posh = "C:\Program Files\PowerShell\pwsh.exe"
    ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter *.html*
    ShareXUploadHistory = {Get-Content -Path "c:\temp\History.Json" | ConvertFrom-Json}
    }

然后通过以下方式调用它:

 . $sys.ShareXUploadHistory
英文:

The solution I came up with involves wrapping the code assigned to the key in {} and when calling that specific key, to prefix it with a . followed by a space, not as prompt as Santiago's answer but it works:

    $SYS = [Ordered]@{
        Posh = "C:\Program Files\PowerShell\pwsh.exe"
        ReflectLogs = Get-ChildItem -Path "C:\Users\All Users\Macrium\Reflect" -Filter *.html*
        ShareXUploadHistory = {Get-Content -Path "c:\temp\History.Json" | ConvertFrom-Json}
        }

and then calling it with:

 . $sys.ShareXUploadHistory

huangapple
  • 本文由 发表于 2023年6月9日 00:47:12
  • 转载请务必保留本文链接:https://go.coder-hub.com/76434073.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定