非常奇怪的Powershell xml编写行为

非常奇怪的Powershell xml编写行为,xml,powershell,Xml,Powershell,我正在开发一个PS脚本,将一些数据写入XML文件。下面是我使用的一个函数: function addDeploymentRecord($serverPath, $typed, $branchName) { $storePath = "C:\work\deployment\testing.xml" $document = [System.Xml.XmlDocument](Get-Content -Path $storePath) $record = $document.se

我正在开发一个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)
}
我这样称呼它:addDeploymentRecordhttp://localhost:${portNumber}/${applicationName},后端,$branchName 我的xml文件包含一个空节点: 运行脚本后,我在文件中添加了以下行:

<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
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

获取内容-路径$storePath->什么是$storePath?它不是你函数的参数,你从哪里得到它?是的,忘了提到它-它是函数内部定义的变量。编辑了问题获取内容-Path$storePath->What is$storePath?它不是你函数的参数,你从哪里得到它?是的,忘了提到它-它是函数内部定义的变量。编辑了问题,这就是我错过的。谢谢。我只是需要多想想这就是我错过的。谢谢。我只是需要多想想