简体   繁体   English

将数据传递到Powershell脚本

[英]Pass data to powershell script

I am working with a PowerShell script which needs to take data from a file and use them as parameters to the functions written in it. 我正在使用PowerShell脚本,该脚本需要从文件中获取数据并将它们用作写入其中的函数的参数。 So basically what I am looking for is something like a JSON or XML file but I am not sure about what to use. 因此,基本上我正在寻找的是JSON或XML文件之类的东西,但是我不确定该使用什么。

What I am looking for is the one which is relatively easier to use, passes datatypes or objects instead of strings. 我正在寻找的是一种相对易于使用的方法,它传递数据类型或对象而不是字符串。

A part of data would look something like this in XML : 一部分数据在XML中看起来像这样:

<RegistryEntriesList>
<registryPath>HKLM:\\Software\\Lenovo\\Configuration\\</registryPath> <registryProperty>PATH </registryProperty>
<registryPath>HKLM:\\Software\\Lenovo\\Configuration\\</registryPath> <registryProperty>LOGS </registryProperty>
<registryPath>HKLM:\\Software\\Lenovo\\Connections\\</registryPath> <registryProperty>MinConnectionsPerTarget</registryProperty>
<registryPath>HKLM:\\Software\\Lenovo\\Connections\\</registryPath> <registryProperty>MaxWorkingIscsiConnections</registryProperty>
<registryPath>HKLM:\\Software\\Lenovo\\Connections\\</registryPath> <registryProperty>WaitIntervalInMilliseconds</registryProperty>

Another part would like this in JSON: 另一部分在JSON中会这样:

"entredInput":[{"title":"Powershell","desc":"first"},
      {"title":"json","desc":"third"},
      {"title":"Configfile","desc":"second"}]

And many more like this. 还有更多类似的东西。

Basically I would be using these for automation purpose. 基本上,我会将这些用于自动化目的。 I know PowerShell4.0 alone has the cmdlet ConvertFrom-JSON but it dosent work on PS2.0 and I want my script to run on any version of PowerShell, ie any release of Windows. 我知道PowerShell4.0本身具有cmdlet ConvertFrom-JSON,但它在PS2.0上也可以正常工作,我希望我的脚本可以在任何版本的PowerShell(即Windows的任何版本)上运行。 So I guess XML might be a better option but I am not sure. 因此,我想XML可能是一个更好的选择,但我不确定。 Is there any other option ?? 还有其他选择吗?

I have gone through many weblinks about JSON vs XML and they have only confused me a lot. 我浏览了许多有关JSON和XML的Web链接,它们只是让我很困惑。 Kindly let me select a better option among JSON, XML and SOMETHINGELSE. 请允许我在JSON,XML和SOMETHINGELSE中选择一个更好的选项。 Thanks for going through the question. 感谢您解决问题。

For PowerShell 2.0 you probably want to use XML. 对于PowerShell 2.0,您可能要使用XML。 For anything that supports JSON I would use it: it's more lightweight usually. 对于任何支持JSON的东西,我都会使用它:通常更轻巧。

Alternative is to use psd1 files with Import-LocalizedData cmdlet. 替代方法是将psd1文件与Import-LocalizedData cmdlet一起使用。 Let say you create file 'Data.psd1' in current folder that looks like this: 假设您在当前文件夹中创建文件“ Data.psd1”,如下所示:

@{
    RegistryEntriesList = @(
        @{
            registryPath = 'HKLM:\Software\Lenovo\Configuration\'
            registryProperty = 'PATH'
        },
        @{
            registryPath = 'HKLM:\Software\Lenovo\Configuration\'
            registryProperty = 'LOGS'
        }
    )
}

As you can see it's hash table with single key RegistryEntriesList. 如您所见,它是带有单键RegistryEntriesList的哈希表。 It's value is collection of hash tables that can be used for splatting. 它的值是可用于散列的哈希表的集合。 You can import this data and pass to your command like this: 您可以导入此数据并将其传递给命令,如下所示:

# Our test command...

function New-RegistryEntry {
param (
    [string]$RegistryProperty,
    [string]$RegistryPath
)

    "Got RegistryPath: $RegistryPath and RegistryProperty: $RegistryProperty"
}

Import-LocalizedData -BindingVariable Params -BaseDirectory . -FileName Data.psd1

foreach ($item in $Params.RegistryEntriesList) {
    New-RegistryEntry @item
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM