简体   繁体   中英

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. So basically what I am looking for is something like a JSON or XML file but I am not sure about what to use.

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 :

<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:

"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. So I guess XML might be a better option but I am not sure. Is there any other option ??

I have gone through many weblinks about JSON vs XML and they have only confused me a lot. Kindly let me select a better option among JSON, XML and SOMETHINGELSE. Thanks for going through the question.

For PowerShell 2.0 you probably want to use XML. For anything that supports JSON I would use it: it's more lightweight usually.

Alternative is to use psd1 files with Import-LocalizedData cmdlet. Let say you create file 'Data.psd1' in current folder that looks like this:

@{
    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. 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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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