【发布时间】:2014-02-21 08:33:20
【问题描述】:
我正在开发一个将一些数据写入 XML 文件的 PS 脚本。这是我使用的一个函数:
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 部分,问题仍然存在。
【问题讨论】:
-
Get-Content -Path $storePath--> 什么是$storePath?它不是你的函数的参数,你从哪里得到它? -
是的,忘了提 - 这是函数内部定义的变量。编辑了问题
标签: xml powershell