格式化字符串时出错:输入字符串的格式不正确

时间:2017-07-26 10:52:22

标签: xml excel powershell

我尝试将Excel文件导入PowerShell,然后将其另存为XML文件。我有一个Excel文件和一个XML模板。我所做的是在下面:

# define XML configuration template
$config = "C:\Users\mustafaal\Desktop\StajyerMustafaAl\Gorev\XMLTemplate_v2.ps1"
Import-Csv C:\Users\mustafaal\Desktop\StajyerMustafaAl\Gorev\VariablesDeneme.csv | ForEach-Object {
    $xml = "C:\Users\mustafaal\Desktop\StajyerMustafaAl\Gorev\MySampleConfig" + $_.UserName + ".xml"
    Write-Host $_.username
    Write-Host $xml
    [xml]$x = (&$config) -f $_.UserName, $_.SwitchIP, $_.SwitchPort, $_.MACAddress
    $x.Save($xml)
}

基本上,我试图为Excel文件中的每个用户名保存XML文件。

我收到此错误:

Error formatting a string: Input string was not in a correct format.
At C:\Users\mustafaal\Desktop\StajyerMustafaAl\Gorev\deneme1.ps1:14 char:1
+ [xml]$x = (&$config) -f $_.UserName, $_.SwitchIP, $_.SwitchPort, $_.MACAddress

XML模板示例:

<AttributeList>
    <RuleAttribute displayValue="Ethernet (15)" value="15" operator="EQUALS" name="NAS-Port-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue="Login-User (1), Framed-User (2), Authenticate-Only (8)" value="1,2,8" operator="BELONGS_TO" name="Service-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue={1} value={1} operator="EQUALS" name="NAS-IP-Address" type="Radius:IETF"/>
    <RuleAttribute displayValue={2} value={2} operator="EQUALS" name="NAS-Port-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue={3} value={3} operator="EQUALS_IGNORE_CASE" name="Calling-Station-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue="TEIDOM\"{0} value="TEIDOM\"{0} operator="EQUALS_IGNORE_CASE" name="User-Name" type="Radius:IETF"/>
</AttributeList>

CSV示例:

UserName,SwitchIP,SwitchPort,MACAddress
AHMETO,10.101.254.104,7,288023041d83
ahmett,10.101.254.136,4,480fcf4a6719

基本上我需要将XML文件中的{}替换为CSV文件中的变量,但我收到字符串格式错误。

编辑好的,现在我发现了问题,还有{}的其他代码,我把它们加倍,但现在我遇到了一个新的错误:

  

键入&#34; System.Xml.XmlDocument&#34;。错误:&#34;&#39; ahmett&#39;是一个意外的令牌。期待白色空间。第5行,第84位。

2 个答案:

答案 0 :(得分:0)

你应该这样做:

$x = [xml](Get-Content $config)
$x | Select-Object -Property UserName, SwitchIP, SwitchPort, MACAddress
$x.Save($xml)

如果您有正确形式的配置文件,它将通过类似的东西;)

答案 1 :(得分:0)

模板中的属性值必须使用引号。改变这个:

<AttributeList>
    <RuleAttribute displayValue="Ethernet (15)" value="15" operator="EQUALS" name="NAS-Port-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue="Login-User (1), Framed-User (2), Authenticate-Only (8)" value="1,2,8" operator="BELONGS_TO" name="Service-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue={1} value={1} operator="EQUALS" name="NAS-IP-Address" type="Radius:IETF"/>
    <RuleAttribute displayValue={2} value={2} operator="EQUALS" name="NAS-Port-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue={3} value={3} operator="EQUALS_IGNORE_CASE" name="Calling-Station-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue="TEIDOM\"{0} value="TEIDOM\"{0} operator="EQUALS_IGNORE_CASE" name="User-Name" type="Radius:IETF"/>
</AttributeList>

进入这个:

<AttributeList>
    <RuleAttribute displayValue="Ethernet (15)" value="15" operator="EQUALS" name="NAS-Port-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue="Login-User (1), Framed-User (2), Authenticate-Only (8)" value="1,2,8" operator="BELONGS_TO" name="Service-Type" type="Radius:IETF"/>
    <RuleAttribute displayValue="{1}" value="{1}" operator="EQUALS" name="NAS-IP-Address" type="Radius:IETF"/>
    <RuleAttribute displayValue="{2}" value="{2}" operator="EQUALS" name="NAS-Port-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue="{3}" value="{3}" operator="EQUALS_IGNORE_CASE" name="Calling-Station-Id" type="Radius:IETF"/>
    <RuleAttribute displayValue="TEIDOM\{0}" value="TEIDOM\{0}" operator="EQUALS_IGNORE_CASE" name="User-Name" type="Radius:IETF"/>
</AttributeList>

此外,您需要将模板作为常规文本加载,因此您可以使用格式运算符(-f)填充值。完成后,您可以将数据保存为文本:

$template = Get-Content 'C:\path\to\template.xml' | Out-String
Import-Csv 'C:\path\to\input.csv' | ForEach-Object {
    $filename = 'C:\path\to\{0}.xml' -f $_.UserName
    $template -f $_.UserName, $_.SwitchIP, $_.SwitchPort, $_.MACAddress |
        Set-Content $filename
}

或将填充的模板解析为XML对象并保存:

$template = Get-Content 'C:\path\to\template.xml' | Out-String
Import-Csv 'C:\path\to\input.csv' | ForEach-Object {
    $filename = 'C:\path\to\{0}.xml' -f $_.UserName
    [xml]$xml = $template -f $_.UserName, $_.SwitchIP, $_.SwitchPort, $_.MACAddress
    $xml.Save($filename)
}

后者的优点是解析器将验证XML数据并在出现错误时抛出错误。