简体   繁体   English

在 Powershell 中设置变量不覆盖脚本参数? 尝试使用 JSON 读取 JSON 并覆盖脚本参数

[英]In Powershell set-variable not overriding Script Parameters? Trying to read JSON and override script parameters using JSON

I'm wondering about the following code.我想知道下面的代码。 I use powershell to load the following json file called "tool.json":我使用 powershell 加载以下名为“tool.json”的 json 文件:

{
  "1": {
    "Tool":           ["vhdl_component_packager"],
    "Name":           ["a1_stuff"],
    "Dir":            ["./source"],
    "Recurse":        ["true"],
    "ExcludeVhd":     ["*_tb.vhd"],
    "ExcludeEntity":  []
  }
}

Then when I execute my script I would expect the script to read the variables in the JSON file to override the parameters of the script, namely $Dir, and set it to ./source as shown in the JSON file.然后,当我执行我的脚本时,我希望脚本读取 JSON 文件中的变量以覆盖脚本的参数,即 $Dir,并将其设置为 ./source,如 JSON 文件中所示。 But that's not happening, instead its not change $Dir... I'm wondering how to fix it.但这并没有发生,而是它没有改变 $Dir ......我想知道如何解决它。


param(
    [string]$Dir = "."
)
function read_tool_json {
    $tool_json = "./tool.json"
    
    if (-not(test-path $tool_json)) {
        throw "Missing ./tool.json file"
    }       
    
    write-host "Reading file: $tool_json"
    $tool_json  = Get-Content -Raw $tool_json | ConvertFrom-Json
    
    foreach ($item1 in $tool_json.psobject.Properties) {
        write-host $item1.Name
        $top_name = $item1.Name
        $top_node = $tool_json.$top_name                
        foreach ($item2 in $top_node.psobject.Properties) {
           $elem_name = $item2.Name
           $elem_node = [array]$top_node.$elem_name             
           write-host "set-variable -name $elem_name -value $elem_node -scope global"
           set-variable -name $elem_name -value $elem_node -scope global -force
           $dir
        }           
    }       
}

read_tool_json

$script:Dir.GetType()

#QUESTION: how to get $script:Dir to equal "./source" from json file???
write-host "dir:($Dir)"

You've got a variable already defined at script scope called Dir :您已经在脚本范围内定义了一个名为Dir的变量:

param(
    [string]$Dir = "."
)

However, you are setting $Dir at global scope in your set-variable .但是,您在set-variable全局范围内设置$Dir You can see that if you add the following line to your script:如果您在脚本中添加以下行,您会看到:

write-host "dir:($global:Dir)"

If you want to modify the the $Dir at script scope, then just remove the -scope global from your set-variable command, or change it to -scope script .如果要在脚本范围内修改$Dir ,则只需从set-variable命令中删除-scope global ,或将其更改为-scope script

If you want to modify the the variable at script scope and also have it available at global scope, you'll have to do it explicitly at the end of your script:如果要在脚本范围内修改变量并使其在全局范围内可用,则必须在脚本末尾显式执行此操作:

$Dir = $global:Dir

Basciacally, you have 2 different variables here $global:Dir and $script:Dir .基本上,这里有 2 个不同的变量$global:Dir$script:Dir

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

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