Итерация по массиву json с помощью Power shell - PullRequest
0 голосов
/ 07 сентября 2018

Это то, что я пробовал до сих пор:

$Path="C:\Users\Desktop\test.json"
$json=(Get-Content $Path | ConvertFrom-Json).parameters.FileArray 
//FileArray is array name in json file
$demo=$json.GetType().FullName //System.Object[]

foreach($file in Get-ChildItem "D:\A\DF\ls\" -Include $json|ForEach Object{$_} -Recurse){

Write-Output $file.BaseName 
Write-Output $file.FullName

Set-AzureRmDataFactoryV2LinkedService -DataFactoryName "adfg1" - ResourceGroupName "bgc" -Name $file.BaseName -DefinitionFile $file.FullName -Force
}

Json Я использую test.json: {
"параметры":
{ "FileArray": [ "ABC.json", "DEF.json"] } }

Исключение, которое я получаю: ForEach-Object: Невозможно связать параметр 'RemainingScripts'. Не могу конвертировать значение "-Recurse" типа "System.String" для введите "System.Management.Automation.ScriptBlock".

Что я ожидаю (имена файлов): ABC.json DEF.json

Any help how it can be handled.
How I can iterate over array values from json.
P.S: I am beginner in powershell

1 Ответ

0 голосов
/ 07 сентября 2018
foreach($file in (Get-ChildItem "D:\A\DF\ls\" -Include $json -Recurse)) {

$file.BaseName
$file.FullName

}

Я не понимаю, зачем вам нужно ForEach Object{$_}, -include принимает массив

Пример кода:

$jsonfile = '{"parameters":
             { "FileArray": [ "ABC.json", "DEF.json"] } }' | ConvertFrom-Json
$json = $jsonfile.parameters.FileArray

foreach($file in (Get-ChildItem "c:\temp" -Include $json -Recurse)) {
    $file.BaseName
    $file.FullName
}

Пример вывода:

ABC
C:\temp\example-folder\ABC.json
DEF
C:\temp\example-folder\DEF.json
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...