英文:
Why do my powershell code behaviors differently when called from another file as a module?
问题
I wrote a powershell function to automatically merge .json files. It's quite simple. In summary:
- It receives three paths. The first for the old file, the second for the new file, the third for the target.
- It will iterate over all leaves of the json tree of the new file and generate a list containing a unique address for each single leaf.
- It will iterate over the list of addresses and: if that address exists in the new and old json, the value of this address in the new json will be the same as the old json. If the value exists in the new json and does not exist in the old json, the address and the value are preserved in the new json. If the address exists in the old json and does not exist in the new json, it will continue not existing in the new json.
- The resultant new json will be saved in the target path.
I wrote this code as a part of a project in which I'm writing this kind of simple auto merger for some file types like .ini and .xml. I wrote all the merger functions as powershell modules (.psm1) and then I call them from a .ps1 file that makes the interface between the user and the merge scripts.
The issue I'm having is the following. When I test the json merger as a .ps1 file it works flawlessly.
Example running it as .ps1:
JsonMerge.ps1
function MergeJsonFiles($oldCfgPath, $newCfgPath, $targetCfgPath) {
# CODE
$json | Out-File -FilePath "$targetCfgPath"
}
MergeJsonFiles './oldtest.json' './newtest.json' './targettest.json'
oldtest.json
{
"A": {
"B": [
{
"ABC": 1,
"DEF": 2
},
{
"A$C": "AB$CE",
"ASDC": "ASDHJ"
}
]
},
"D": "Deprecated Value"
}
newtest.json
{
"A": {
"B" : [
{
"ABC": 0,
"DEF": 0
},
{
"A$C": "A",
"ASDC": "A"
}
]
},
"N": "New Value"
}
Result: targettest.json
{
"A": {
"B": [
{
"ABC": 1,
"DEF": 2
},
{
"A$C": "AB$CE",
"ASDC": "ASDHJ"
}
]
},
"N": "New Value"
}
But this is what happens when I call the same function, with the same arguments but importing it as a module:
The old and new test files are the same, but this is the resultant target json
{
"A": {
"B": [
{
"ABC": "1$",
"DEF": "2$"
},
{
"A$C": "A",
"ASDC": "ASDHJ$",
"A": "AB$"
}
]
},
"N": "New Value"
}
Why does it happen? Why is there a "$" at the end of each value? Why do the numeric values become strings ended with "$"? I simply don't understand.
All code is on github. Please help.
EDIT
I find out that the issue was happening due to the fact I was running the code within WSL using the powershell installation of the Linux system. As my vscode is set for Linux and I don't want to have to set it for windows I use it to make and test my powershell scripts. When running it on Windows it worked fine. I don't know if it is a misbehavior of the Linux build of powershell or just an issue with my environment. When I ran the code directly on Windows powershell it worked as expected.
英文:
I wrote a powershell function to automatically merge .json files. It's quite simple. In summary:
- It receives three paths. The first for the old file, the second for the new file, the third for target.
- It will iterate over all leaves of the json tree of the new file and generate a list containing an unique address for each single leaf.
- It will iterate over the list of addresses and: if that address exists in the new and old json, the value of this address in the new json will be the same as the old json. If the value exists in the new json and does not exists in the old json, the address and the value are preserved in the new json. If the address exists in the old json and does not exists in the new json, it will continue not existing in the new json.
- The resultant new json will be saved in the target path.
I wrote this code as a part of a project in which I'm writting this kind of simple auto merger for some file types like .ini and .xml. I wrote all the merger functions as powershell modules (.psm1) and then I call them from a .ps1 file that makes the interface between the user and the merge scripts.
The issue I'm having is the following. When I test the json merger as a .ps1 file it works flawless.
Example running it as .ps1:
JsonMerge.ps1
function MergeJsonFiles($oldCfgPath, $newCfgPath, $targetCfgPath) {
# CODE
$json | Out-File -FilePath "$targetCfgPath"
}
MergeJsonFiles './oldtest.json' './newtest.json' './targettest.json'
oldtest.json
{
"A": {
"B": [
{
"ABC": 1,
"DEF": 2
},
{
"A$C": "AB$CE",
"ASDC": "ASDHJ"
}
]
},
"D": "Deprecated Value"
}
newtest.json
{
"A": {
"B" : [
{
"ABC": 0,
"DEF": 0
},
{
"A$C": "A",
"ASDC": "A"
}
]
},
"N": "New Value"
}
Result: targettest.json
{
"A": {
"B": [
{
"ABC": 1,
"DEF": 2
},
{
"A$C": "AB$CE",
"ASDC": "ASDHJ"
}
]
},
"N": "New Value"
}
But this is what happnes when I call the same function, with the same arguments but importing it as a module:
The old and new test files are the same, but this is the resultant target json
{
"A": {
"B": [
{
"ABC": "1$",
"DEF": "2$"
},
{
"A$C": "A",
"ASDC": "ASDHJ$",
"A": "AB$"
}
]
},
"N": "New Value"
}
Why does it happens? Why there is an "$" in the end of each value? Why does the numeric values became strings ended with "$"? I simply don't understand.
All code is on github. Please help.
EDIT
I find out that the issue was happening due to the fact I was running the code within WSL using the powershell installation of the linux system. As my vscode is set for linux and I don't want to have to set it for windows I use it to make and test my powershell scripts. When running it on windows it worked fine. I don't know if it is an misbehavior of the linux build of powershell or just an issue with my environment. When I ran the code directly on windows powershell it worked as expected.
答案1
得分: 1
问题出在WSL和Windows中的PowerShell环境不同。我删除了之前的回答,因为@mklement0指出,在更深入地了解后,我推荐的两种解决方案都不太合理。无法删除此答案,因为它已被标记为已接受。
英文:
It turned out that the problem was different PowerShell environments in WSL and Windows. I deleted my previous answer, since @mklement0 pointed out that neither of my two recommended solutions make that much sense once you look deeper into it. Cannot delete this answer, since it has been marked as accepted.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论