这是我到目前为止尝试过的:
$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”。无法转换 类型“ System.String”的“ -Recurse”值 键入“ 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
答案 0 :(得分:0)
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