gpt4 book ai didi

xml - 非常奇怪的Powershell xml编写行为

转载 作者:行者123 更新时间:2023-12-03 00:52:02 25 4
gpt4 key购买 nike

我正在开发一个PS脚本,该脚本将一些数据写入XML文件。这是我使用的功能:

function addDeploymentRecord($serverPath, $typed, $branchName)
{
$storePath = "C:\work\deployment\testing.xml"
$document = [System.Xml.XmlDocument](Get-Content -Path $storePath)
$record = $document.selectSingleNode("records").AppendChild($document.CreateElement("deployment"))

$currentDate = Get-Date

# Add a Attribute
$record.SetAttribute("url", $serverPath)
$record.SetAttribute("type", $typed)
$record.SetAttribute("branch", $branchName)
$record.SetAttribute("date", $currentDate)

$document.Save($storePath)
}

我这样称呼: addDeploymentRecord("http://localhost:${portNumber}/${applicationName}", "backend", $branchName)我的xml文件包含一个空节点: <records></records>运行脚本后,这是我添加到文件中的行:
<deployment url="http://localhost:90/task20118 backend task20118" type="" branch="" date="02/20/2014 19:16:13" />

这是正常的吗?我不是PowerShell专家,但这不是我所期望的。有任何想法我在这里做错了吗?
附言我最初的想法是我在URL中搞砸了字符串插值。似乎并非如此-即使我删除了http部分,问题仍然存在。

最佳答案

Powershell不使用括号来调用函数。
("http://localhost:${portNumber}/${applicationName}", "backend", $branchName)只是创建一个数组,然后调用该函数,仅将该数组传递给$serverPath。然后,当您替换变量时,使用空格将数组的元素连接在一起。
您需要丢失括号和逗号分隔的参数:

PS D:\> function addDeploymentRecord($serverPath, $typed, $branchName)
{
Write-Host "ServerPath is $serverPath"
Write-Host "typed is $typed"
Write-Host "branchName is $branchName"
}

PS D:\> $branchName = "myBranch"

PS D:\> addDeploymentRecord("http://localhost:${portNumber}/${applicationName}", "backend", $branchName)
ServerPath is http://localhost:/ backend myBranch
typed is
branchName is

PS D:\> addDeploymentRecord "http://localhost:${portNumber}/${applicationName}" "backend" $branchName
ServerPath is http://localhost:/
typed is backend
branchName is myBranch

或通过名称传递参数:
PS D:\> addDeploymentRecord -serverPath "http://localhost:${portNumber}/${applicationName}" -typed "backend" -branchName $branchName
ServerPath is http://localhost:/
typed is backend
branchName is myBranch

关于xml - 非常奇怪的Powershell xml编写行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21929192/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com