本文介绍了如何在Powershell中将嵌套的任意关联数组值设置为.psd1文件?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Powershell Array对象,它是通过程序生成的,在数组内部,数组内部有时带有属性包或哈希表的数组,但我认为它在本机中被称为包含数组的数组

I have a powershell Array object that is programmatically generated, something with arrays inside arrays, inside arrays, sometimes called a "property bag" or a "hashtable", but I think it's natively called an "Array containing Arrays" in the most native powershell terminology.

例如:

 @{
 Version  = '1.0.0'
 Name     = 'thing'
 Revision = 'c3a89cd20e19bb82f41e95e0806edc5b6cfd224e'
 Date     = '2016-12-09'
 Build    = '1234'
 Contents = @{
      "index.html" = "23dd7b993f40bb3ae8848fe104b3b767"
 }
 }

生成仅将一组特定的内容保存到.PSD1文件的函数似乎不够通用。
我想将确切的内存数组结构保存到PSD1文件中。 Powershell中是否存在任何内置方法来持久存储具有上述值的内存变量? Import-PowershellDataFile 的相反函数。我猜想它会被称为 Export-PowershellDatafile ,但似乎不存在这样的功能。

Generating a function to save only one specific set of contents to a .PSD1 file seems not general enough. I want to save that exact in-memory Array structure to a PSD1 file. Does any built-in way in Powershell exist to Persist an in-memory variable with the values above? The "opposite function" of Import-PowershellDataFile. I would have guessed it would be called Export-PowershellDatafile but no such function seems to exist.

推荐答案

由以下问题产生,我创建了(并且仍在维护)已演变为。该脚本的最终目标是能够根据正确 PowerShell语法而不是像[ PowerShell 对象。 > JSON ](或[ XML ]。

这包括区分 字节数组 byte数组 ,但也可以正确接受(并格式化)单例。

输出为 [ScriptBlock] 可以很容易地另存为表达式。关键是(取决于用法)在最终调用时可能会创建一个安全漏洞(cq )(请参阅:)。

无论如何,我还包括了-探索参数会提供与 PSD1 PowerShell数据文件所需的输出完全相同的输出。实际上,我之前从未考虑过这种用法,但现在考虑别名此参数,并在下一个版本中为 PSD1 文件赋予它唯一的含义。

在换句话说:

Originated from this Save hash table in PowerShell object notation (PSON) question, I created (and still maintaining) a PowerShell cmdlet that evolved to a ConvertTo-Expression. The ultimate goal of this script is to be able to export and import PowerShell objects between systems based on a correct PowerShell syntax rather than an exotic format like [JSON]( or [XML].
This includes differentiating between an array of bytes and a byte array but also correctly accepting (and formatting) singletons.
The output is [ScriptBlock] which can be easily save as an expression. The point is that (depending on the usage) it might create a security hole when eventually invoking (c.q. dot sourcing) it (see: Running partly trusted PowerShell code in a restricted security environment. #12377).
Anyways, I also included an -Explore parameter which I belief gives exactly the same output as required for the PSD1 PowerShell Data File. I actually never thought about this usage earlier but now considering to alias this parameter and give it an exclusive meaning for PSD1 files in my next version.
In other words:

$Data = @{
 Version  = '1.0.0'
 Name     = 'thing'
 Revision = 'c3a89cd20e19bb82f41e95e0806edc5b6cfd224e'
 Date     = '2016-12-09'
 Build    = '1234'
 Contents = @{
      "index.html" = "23dd7b993f40bb3ae8848fe104b3b767"
 }
 }

ConvertTo-Expression -Explore $Data # | Out-File .\MyData.psd1

返回 [ScriptBlock] 会在以下情况下自动转换为以下表达式将其导出到文件或仅显示它:

Returns a [ScriptBlock] which automatically converts to the following expression when e.g. exporting it to a file or simply displaying it:

@{
        'Date' = '2016-12-09'
        'Revision' = 'c3a89cd20e19bb82f41e95e0806edc5b6cfd224e'
        'Version' = '1.0.0'
        'Build' = '1234'
        'Contents' = @{'index.html' = '23dd7b993f40bb3ae8848fe104b3b767'}
        'Name' = 'thing'
}

这篇关于如何在Powershell中将嵌套的任意关联数组值设置为.psd1文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 06:03