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

PowerShell trying to convert to an integer instead of a string

我的问题是 PowerShell 在尝试使用它来生成新字符串时想要将“=”转换为整数。我确保我的结果变量也是一个字符串。这是我的任务:

我有一点Exceltable像这样:

___________|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|    

除此之外 table 我想将每个端口保存在一个 .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"。

所以,我不太确定问题出在哪里,但我的解决方法是重新启动我的环境。我在 PowerShell ISE 中编程,我有时会看到 "caching" 变量。 ISE 的这次重启使程序再次运行。 不,我事先没有改变任何东西。 最后我很高兴我的问题不再存在,但我仍然想知道为什么错误是由“=”触发的。