在 power shell 脚本中将 json 作为参数传递
pass json as parameter in power shell script
我创建了一个函数来创建新的 xml node.there 是我函数中的两个参数,一个是现有的 xml 文件引用,第二个是元素 value.while 运行 脚本显示错误
代码
function createProviderNode($xmlData,$propertyValue){
Write-Host 'inside createProviderNode'
Write-Host ($propertyValue)
#[xml]$xmlData = get-content E:\powershell\data.xml
$newProviderNode = $xmlData.CreateNode("element","provider","")
$newProviderNode.SetAttribute("name",$propertyValue)
$xmlData.SelectSingleNode('providers').AppendChild($newProviderNode)
$xmlData.save("E:\powershell\data.xml")
}
我是否遗漏了这段代码中的任何内容?
嗯,你没有显示你原来的 XML 格式。
为什么要注释掉 Get-Content?没有它就无法工作。
因此,如果我们采用以下示例,它会按预期工作。
# Simple XML version
$SimpleXml = $null
$SimpleXml = @"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<name>Apple</name>
<size>1234</size>
</configuration>
"@
# New node code
[xml]$XmlDoc = Get-Content -Path variable:\SimpleXml
$runtime = $XmlDoc.CreateNode("element","runtime","")
$generated = $XmlDoc.CreateNode("element","generatePublisherEvidence","")
$generated.SetAttribute("enabled","false")
$runtime.AppendChild($generated)
$XmlDoc.configuration.AppendChild($runtime)
$XmlDoc.save("$pwd\SimpleXml.xml")
Get-Content -Path "$pwd\SimpleXml.xml"
# Which creates this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<name>Apple</name>
<size>1234</size>
<runtime>
<generatePublisherEvidence enabled="false" />
</runtime>
</configuration>
此外,除非您要为屏幕输出着色,否则永远不需要 Write-Host。
Write-Output 是默认的,并自动写入屏幕,无论您是否指定 Write-Output。
因此,所有这些都是同一件事 - 输出到屏幕。
$SomeString = 'hello'
Write-Host $SomeString
Write-Output $SomeString
'hello'
"hello"
$SomeString
"$SomeString"
($SomeString)
("$SomeString")
$($SomeString)
# Results
hello
hello
hello
hello
hello
hello
hello
……然而,这是你的选择。
错误消息暗示 虽然您 期望 $xmlData
包含 [xml]
类型的对象(System.Xml.XmlDocument
) - 即 XML 文档 - 实际上它是 string ([string]
).
换句话说:当您调用 createProviderNode
函数时,您传递的第一个参数是 字符串 ,而不是 XML 文档(类型[xml
]).
将您的 $xmlData
参数变量键入 [xml]
解决了这个问题,因为这甚至会隐式隐藏 字符串 按需 XML 文档的参数 - 如果可能的话。
一个简化的例子,使用脚本块代替函数:
$xmlString = @'
<?xml version="1.0"?><catalog><book id="bk101"><title>De Profundis</title></book></catalog>
'@
# Note how $xmlData is [xml]-typed.
& { param([xml] $xmlData) $xmlData.catalog.book.title } $xmlString
以上结果为 De Profundis
,表明字符串参数已转换为 [xml]
实例(由于 PowerShell 的类型适配魔法,使得元素名称可作为直接属性使用)。
然后在 $xmlData
.
上调用 .CreateNode()
方法是安全的
我创建了一个函数来创建新的 xml node.there 是我函数中的两个参数,一个是现有的 xml 文件引用,第二个是元素 value.while 运行 脚本显示错误
代码
function createProviderNode($xmlData,$propertyValue){
Write-Host 'inside createProviderNode'
Write-Host ($propertyValue)
#[xml]$xmlData = get-content E:\powershell\data.xml
$newProviderNode = $xmlData.CreateNode("element","provider","")
$newProviderNode.SetAttribute("name",$propertyValue)
$xmlData.SelectSingleNode('providers').AppendChild($newProviderNode)
$xmlData.save("E:\powershell\data.xml")
}
我是否遗漏了这段代码中的任何内容?
嗯,你没有显示你原来的 XML 格式。 为什么要注释掉 Get-Content?没有它就无法工作。
因此,如果我们采用以下示例,它会按预期工作。
# Simple XML version
$SimpleXml = $null
$SimpleXml = @"
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<name>Apple</name>
<size>1234</size>
</configuration>
"@
# New node code
[xml]$XmlDoc = Get-Content -Path variable:\SimpleXml
$runtime = $XmlDoc.CreateNode("element","runtime","")
$generated = $XmlDoc.CreateNode("element","generatePublisherEvidence","")
$generated.SetAttribute("enabled","false")
$runtime.AppendChild($generated)
$XmlDoc.configuration.AppendChild($runtime)
$XmlDoc.save("$pwd\SimpleXml.xml")
Get-Content -Path "$pwd\SimpleXml.xml"
# Which creates this:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<name>Apple</name>
<size>1234</size>
<runtime>
<generatePublisherEvidence enabled="false" />
</runtime>
</configuration>
此外,除非您要为屏幕输出着色,否则永远不需要 Write-Host。 Write-Output 是默认的,并自动写入屏幕,无论您是否指定 Write-Output。
因此,所有这些都是同一件事 - 输出到屏幕。
$SomeString = 'hello'
Write-Host $SomeString
Write-Output $SomeString
'hello'
"hello"
$SomeString
"$SomeString"
($SomeString)
("$SomeString")
$($SomeString)
# Results
hello
hello
hello
hello
hello
hello
hello
……然而,这是你的选择。
错误消息暗示 虽然您 期望 $xmlData
包含 [xml]
类型的对象(System.Xml.XmlDocument
) - 即 XML 文档 - 实际上它是 string ([string]
).
换句话说:当您调用 createProviderNode
函数时,您传递的第一个参数是 字符串 ,而不是 XML 文档(类型[xml
]).
将您的 $xmlData
参数变量键入 [xml]
解决了这个问题,因为这甚至会隐式隐藏 字符串 按需 XML 文档的参数 - 如果可能的话。
一个简化的例子,使用脚本块代替函数:
$xmlString = @'
<?xml version="1.0"?><catalog><book id="bk101"><title>De Profundis</title></book></catalog>
'@
# Note how $xmlData is [xml]-typed.
& { param([xml] $xmlData) $xmlData.catalog.book.title } $xmlString
以上结果为 De Profundis
,表明字符串参数已转换为 [xml]
实例(由于 PowerShell 的类型适配魔法,使得元素名称可作为直接属性使用)。
然后在 $xmlData
.
.CreateNode()
方法是安全的