我正在PowerShell中进行脚本,其中正在修改Chrome Bookmarks文件。
这就是我想做的。
- 读取文件并解析JSON(完成)
- 检查是否添加了某个文件夹,如果没有添加,请添加。
- 再次解析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.
- Read the file and parse the Json (Done)
- Check if a certain folder is added, if it isn't, add it.
- 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".

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.
发布评论
评论(1)
)本质上是在powershell/.net中,即无法保证其条目的顺序,这意味着到您
new-object
它的
-property
参数,hashtable中条目的定义已经丢失。但是,PowerShell V3+提供句法糖用于构造自定义对象(
[pscustomObject]
(aka[psobject]
)),在这种情况下条目,即保证结果的顺序 可以反映定义顺序,也就是说,如果您将hashtable施加到[pscustomobject]
:注意如果您确实要坚持使用散布(字典),则可以“铸造”标志性的字面形式到
[有序]
,这也可以保证保留输入顺序;具体而言,这种句法糖会产生订购的 hashtable,即asystem.collections.collections.specialized.ordereddictionary
实例,它也实现了indictionary
接口,但是(a)在定义顺序中枚举其条目,(b)允许访问条目<< em>位置索引,作为使用键作为索引的替代方案;例如: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 yourNew-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]
: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. aSystem.Collections.Specialized.OrderedDictionary
instance, which also implements theIDictionary
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.: