我有一个PowerShell脚本,我在其中生成xml文件。
我的脚本如下:
$filePath= "C:\Powershell\Report.xml"
if(Test-Path $filePath)
{
Remove-Item $filePath
}
#set encoding
$encoding = [System.Text.Encoding]::UTF8
# Create The Document
$XmlWriter = New-Object System.XMl.XmlTextWriter($filePath,$encoding)
# Set The Formatting
$xmlWriter.Formatting = "Indented"
$xmlWriter.Indentation = "4"
# Write the XML Decleration
$xmlWriter.WriteStartDocument()
SetNamespace($xmlWriter)
# Write Root Element
$xmlWriter.WriteStartElement($RootElement)
#set Root Element namespaces
SetRootElementNamespace($xmlWriter)
#Create <my:PruefAuftragHeader>
$Ablagenummer = "234"
$Beschrieb = "This is testing"
$Auftraggeber = @("ADAMS","Alu König Stahl","Caradon")
CreatePruefAuftragHeader($xmlWriter ,$Ablagenummer)
Function CreatePruefAuftragHeader([System.Xml.XmlTextWriter] $xmlWriter ,[String] $Ablagenummer)
{
$xmlWriter.WriteStartElement($PruefAuftragHeaderElement) # opening <my:PruefAuftragHeader>
$xmlWriter.WriteElementString($AblagenummerElement,$Ablagenummer)
$xmlWriter.WriteElementString($BeschriebElement,$Beschrieb)
foreach($elem in $Auftraggeber)
{
$xmlWriter.WriteElementString($AuftraggeberElement,$elem)
}
$xmlWriter.WriteEndElement #closing <my:PruefAuftragHeader>
}
当执行到达行CreatePruefAuftragHeader($xmlWriter ,$Ablagenummer)
时,它会抛出错误:Cannot process argument transformation on parameter 'xmlWriter'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type
"System.Xml.XmlTextWriter".
我该怎么做才能解决错误?
答案 0 :(得分:2)
如果你这样调用脚本:
CreatePruefAuftragHeader($xmlWriter ,$Ablagenummer)
您将第一个参数作为数组类型传递
你必须这样称呼它:
CreatePruefAuftragHeader $xmlWriter $Ablagenummer
在powershell中,脚本/函数的参数由空格分开传递,而不是包含在()
中。