如何在powershell中的psobject添加精确值?

发布于 2025-02-04 05:58:56 字数 987 浏览 0 评论 0 原文

我正在PowerShell中进行脚本,其中正在修改Chrome Bookmarks文件。 这就是我想做的。

  1. 读取文件并解析JSON(完成)
  2. 检查是否添加了某个文件夹,如果没有添加,请添加。
  3. 再次解析JSON的对象,并保存一个新的书签文件。 (我知道该怎么做)

这就是我将其转换为对象的方式:

$BkmrkJson = Get-Content $GoogleBkmrk | ConvertFrom-Json

这就是我将新对象添加到当前的“儿童(URL或书签)”中的方式。

$BkmrkJson.roots.bookmark_bar.children += New-Object -TypeName psobject -Property @{children=@();date_added="";date_modified="";guid="";id="";name="HV2";type="folder"}

我的主要问题是,当我添加时,它不尊重属性的顺序。通常的订单是“孩子,date_added,date_modified,guid,id,name,type”。

我在空白中添加了一些值,因为Chrome会自动添加新值,或者在添加该值或孩子之后,我再次将psobject解析到JSON中。

$MyFinalJson = ConvertTo-Json $BkmrkJson -Depth 9

但是,当我创建文件时,它没有正确制作。因此,我的主要问题是,如何正确添加一个新的对象,因此,当我再次解析时,可以正确识别新对象。

I'm doing a script in powershell where i'm modifying the Chrome Bookmarks file.
This is what i want to do.

  1. Read the file and parse the Json (Done)
  2. Check if a certain folder is added, if it isn't, add it.
  3. Parse again the object to Json, and save a new bookmark file. (i know how to do it)

This is how i convert it to Object:

$BkmrkJson = Get-Content $GoogleBkmrk | ConvertFrom-Json

And this is how i'm adding a new Object to the current "Childrens(Urls or bookmarks)".

$BkmrkJson.roots.bookmark_bar.children += New-Object -TypeName psobject -Property @{children=@();date_added="";date_modified="";guid="";id="";name="HV2";type="folder"}

My main problem, it's that when i add it, it isn't respecting the order of the properties. The usual order it's "children, date_added, date_modified, guid, id, name, type".

enter image description here

I add some values in blank, because Chrome adds new values automatically, after i add that value, or children, i parse again the psobject to Json.

$MyFinalJson = ConvertTo-Json $BkmrkJson -Depth 9

But when i create the file, it wasn't made correctly. So my principal question it's, how i can add correctly a new object to the parsed one, so when i parse it again, can recognize correctly the new ones.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

埋葬我深情 2025-02-11 05:58:56

)本质上是在powershell/.net中,即无法保证其条目的顺序,这意味着到您 new-object 它的 -property 参数,hashtable中条目的定义已经丢失。

但是,PowerShell V3+提供句法糖用于构造自定义对象( [pscustomObject] (aka [psobject] )),在这种情况下条目,即保证结果的顺序 可以反映定义顺序,也就是说,如果您将hashtable施加到 [pscustomobject]

$BkmrkJson.roots.bookmark_bar.children += 
  [pscustomobject] @{
    children=@();  # Note: ";" only strictly needed in *single-line* defs.
    date_added="";
    date_modified="";
    guid="";
    id="";
    name="Humach V2";
    type="folder"
  }

注意如果您确实要坚持使用散布(字典),则可以“铸造”标志性的字面形式到 [有序] ,这也可以保证保留输入顺序;具体而言,这种句法糖会产生订购的 hashtable,即a system.collections.collections.specialized.ordereddictionary 实例,它也实现了 indictionary 接口,但是(a)在定义顺序中枚举其条目,(b)允许访问条目<< em>位置索引,作为使用作为索引的替代方案;例如:

$orderedHashTable = [ordered] @{ zebra = 26; quebec = 17; alpha = 1}

$orderedHashTable.Keys # -> 'zebra', 'quebec', 'alpha'

# Access by key.
$orderedHashTable['quebec'] # -> 17

# Access by positional index
$orderedHashTable[0] # -> 26 (key 'zebra')

hashtables (@{ ... }) are inherently unordered in PowerShell / .NET, i.e the order in which their entries are later enumerated isn't guaranteed, which means that by the time your New-Object call receives its -Property argument, the definition order of the entries in the hashtable is already lost.

However, PowerShell v3+ offers syntactic sugar for constructing custom objects ([pscustomobject] (aka [psobject])), in which case the order of entries, i.e. the order of the resulting properties is guaranteed to reflect the definition order, namely if you cast a hashtable to [pscustomobject]:

$BkmrkJson.roots.bookmark_bar.children += 
  [pscustomobject] @{
    children=@();  # Note: ";" only strictly needed in *single-line* defs.
    date_added="";
    date_modified="";
    guid="";
    id="";
    name="Humach V2";
    type="folder"
  }

Note that in cases where you do want to stick with a hashtable (dictionary), you can "cast" a hashtable literal to [ordered], which also guarantees preserving the input order; specifically, this syntactic sugar creates an ordered hashtable, i.e. a System.Collections.Specialized.OrderedDictionary instance, which also implements the IDictionary interface, but (a) enumerates its entries in definition order and (b) allows accessing entries by positional index, as an alternative to using a key as the index; e.g.:

$orderedHashTable = [ordered] @{ zebra = 26; quebec = 17; alpha = 1}

$orderedHashTable.Keys # -> 'zebra', 'quebec', 'alpha'

# Access by key.
$orderedHashTable['quebec'] # -> 17

# Access by positional index
$orderedHashTable[0] # -> 26 (key 'zebra')
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文