New-NetIPAddress参数“ -IPAddress”导致“在语句块或类型定义中缺少关闭'}'”错误

我正在尝试通过如下所示的config.json文件配置本地系统的虚拟交换机:

{
    "system_name":"demo_system","version":"0.0","network_setting":{
        "virtual_switch":{
            "internal":{
                "NATSwitch":{
                    "ip_address":"123.456.789.2","interface_alias":"vEthernet (NATSwitch)","internal_ip_interface_address_prefix":"123.456.789.0/24"
                },"Robot Switch":{
                    "ip_address":"987.654.321.2","interface_alias":"vEthernet (Robot Switch)","internal_ip_interface_address_prefix":"987.654.321.0/24"
                }
            }
        }
    }
}

读取该文件并设置虚拟交换机的ps1文件testing.ps1是这样的:

Param
(
    [string]$system_config = "C:\workspace\config.json"   
)


function assert-virtual-switch
{
    # $args[0] is the $networking_ht
    Write-Output "Checking the virtual switches"
    $ht = $args[0]
    $internal_switch_psobject = $ht["virtual_switch"].internal
    $internal_switch_ht = @{}
    $internal_switch_psobject.psobject.properties | Foreach-Object { $internal_switch_ht[$_.Name] = $_.Value }
    foreach ($internal_switch_key in $internal_switch_ht.Keys) {
        if (Get-VMSwitch | Where-Object {$_.Name -eq $internal_switch_key}) {
            # The switch already exist
            Write-Output "Internal Switch $internal_switch_key exists"
        }else{
            # Create the virtual switch
            Write-Output "Internal Switch $internal_switch_key doesn't exist,creating it now"
            $internal_switch_ip = $internal_switch_ht[$internal_switch_key].ip_address
            $interface_alias = $internal_switch_ht[$internal_switch_key].interface_alias
            $internal_ip_interface_address_prefix = $internal_switch_ht[$internal_switch_key].internal_ip_interface_address_prefix
            [byte]$prefix_length = 24
            New-VMSwitch –SwitchName $internal_switch_key –SwitchType Internal
            New-NetIPAddress -PrefixLength $prefix_length –IPAddress "$internal_switch_ip" -InterfaceAlias "$interface_alias"
            New-NetNat –Name $internal_switch_key –InternalIPInterfaceAddressprefix $internal_ip_interface_address_prefix
        }
    }
}

$sys_ps_object = Get-Content $system_config | Out-String | ConvertFrom-Json
$sys_ht = @{}
$sys_ps_object.psobject.properties | Foreach-Object { $sys_ht[$_.Name] = $_.Value }
# At this point,the system config is converted to a hash table,we can read from it like such:
foreach($sys_config_key in $sys_ht.Keys){
    if ($sys_config_key -eq "network_setting"){
        # $networking_ht is a hashtabe of the networking setting
        $networking_ps_object = $sys_ht[$sys_config_key]
        $networking_ht = @{}
        $networking_ps_object.psobject.properties | Foreach-Object { $networking_ht[$_.Name] = $_.Value }
    }
}

assert-virtual-switch $networking_ht

如您所见,我创建了一个函数assert-virtual-switch,该函数确保虚拟交换机在本地存在,如果不存在则将创建一个。该函数的输入是从config.json中提取的内部交换机信息的哈希表。

但是,我一直遇到此错误:

错误

At C:\workspace\test\test_new_ipaddress\testing.ps1:39 char:69
+ ... "External switch $external_switch_key doesn't exist,creating it now"
+                                                ~~~~~~~~~~~~~~~~~~~~~~~~~~
The string is missing the terminator: '.
At C:\workspace\test\test_new_ipaddress\testing.ps1:20 char:14
+         }else{
+              ~
Missing closing '}' in statement block or type definition.
At C:\workspace\test\test_new_ipaddress\testing.ps1:16 char:64
+     foreach ($internal_switch_key in $internal_switch_ht.Keys) {
+                                                                ~
Missing closing '}' in statement block or type definition.
At C:\workspace\test\test_new_ipaddress\testing.ps1:6 char:1
+ {
+ ~
Missing closing '}' in statement block or type definition.
    + CategoryInfo          : ParserError: (:) [],ParseException
    + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString

我已经将问题明确指出了创建和配置ip地址的这一行。

New-NetIPAddress -PrefixLength $prefix_length –IPAddress "$internal_switch_ip" -InterfaceAlias "$interface_alias"

如果我不包括这样的-IPAddress参数:

New-NetIPAddress -PrefixLength $prefix_length -InterfaceAlias "$interface_alias"

该错误不再显示:

PS C:\workspace> .\testing.ps1
Checking the virtual switches
Internal Switch Robot Switch exists
Internal Switch NATSwitch exists

所以这一定来自cmdlet解释IP地址的方式,因为我尝试过使用$internal_switch_ip前后是否带引号。

另一方面,如果我直接在Powershell控制台中逐行运行代码,则它可以工作。

有人对为什么会这样有任何想法吗?

编辑

根据@Lee_Daily提供的解决方案,该问题是由破折号和破折号混合引起的。将所有的“ –”更改为“-”后,代码即可工作。

luoyanzheng 回答:New-NetIPAddress参数“ -IPAddress”导致“在语句块或类型定义中缺少关闭'}'”错误

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3164936.html

大家都在问