2017-07-26 163 views
0

我试图将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文件中的变量,但我得到一个字符串格式错误。

编辑:好了,现在我已经发现了这个问题,也有与{}其他代码,我让他们双,但现在我有一个新的错误:

to type "System.Xml.XmlDocument". Error: "'ahmett' is an unexpected token. Expecting white space. Line 5, position 84.

+0

'XMLTemplate_v2.ps1'的内容是什么? '(&$ config)'执行该脚本,因此需要输出一个用于'-f'的模板字符串来操作。 –

+0

我加了一块它 – alhn34

回答

0

的一个您的模板中的ttribute值必须用引号引起来。更改此:

<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数据并抛出一个错误,以防其某些事情无效。

+0

谢谢兄弟,它现在工作的很好。 – alhn34

0

你应该做的格式一样即:

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

如果你有适当的形式配置文件时,它会通过类似的东西;)

+0

现在我得到这个错误:无法将值“System.Object []”转换为键入“System.Xml.XmlDocument”。错误: 作为此节点的有效子节点,因为指定节点的类型是错误的。“ – alhn34

+0

显然,您的配置文件格式不正确。 – KlapenHz

+0

这是问题,它不允许粘贴在我的公司,但我可以粘贴一块: – alhn34