PowerShell尝试转换为整数而不是字符串

时间:2019-09-25 13:03:40

标签: excel powershell ini

我的问题是,当尝试使用“ =”转换为整数时,PowerShell希望将其转换为整数。我确保我得到的变量也将是一个字符串。这是我的任务:

我有一个像这样的Excel表格:

___________|VLANID1|VLANID2|VLANID3|
SwitchName1|    1-7|  12-16|   8-11| ← Ports 1-7 of Switch1 are configure for VLAN1...
SwitchName2|    1-7|  12-16|   8-11|    

在此表之外,我想将每个端口都保存在一个.ini文件中,该端口也已配置了VLAN,也就是这样:

[SwitchName1]
1=VLANID1
2=VLANID3
...

这是我的代码,一直循环到没有端口,然后没有开关为止。

$split = $global:Ports -split("-") #$Ports is the Value with the saved excel cell (7-11)
$split[0]..$split[1] | ForEach-Object {
    $vlan = $global:Switches.Cells.Item(1, $global:SSpallte).Text
    [string]$inistring = "`n" + $_ + "=" + $vlan #<- Here it tries to convert "=" into integer
    Add-Content -Path $global:portsini -Value $inistring
}

我什至尝试过将用来制作$inistring的每个变量转换成字符串,但仍然无法正常工作。另外,$_$vlan都具有值,而不是$null

该错误指出无法将“ =”转换为类型“ System.Int32”。

1 个答案:

答案 0 :(得分:0)

因此,我不太清楚问题出在哪里,但是我解决问题的方法是重新启动环境。我正在PowerShell ISE中编程,有时我会看到“缓存”变量。 ISE的重新启动使程序再次工作。 不,我没有事先更改任何内容。 最后,我很高兴我的问题不再存在,但是我仍然想知道为什么错误是由“ =”触发的。