Splat参数到cmdlet而不展开数组

时间:2018-08-03 12:53:22

标签: powershell

我有这个cmdlet:

$dnstype = ,@("Primary","Secondary","Tertiary")
$dnsserver = ,@("192.168.1.11","192.168.1.12","192.168.150.13")

Set-HPEiLOIPv4NetworkSetting -connection $connection -DNSServer $dnsserver -DNSServerType $dnstype

这很好。

但是,由于需要根据对象的已知状态添加参数(这是因为某些参数会导致重置,如果可能的话,我会尽量避免这样做),因此我需要对参数进行splat处理。

在执行以下操作时,不会修改DNS服务器,因为将它们发送到cmdlet时需要保持“展开”状态。

$dnstype = ,@("Primary","Secondary","Tertiary")
$dnsserver = ,@("192.168.1.11","192.168.1.12","192.168.150.13")

$NewDCHPv4Settings = @{
    Connection    = $connection
    InterfaceType = "Dedicated"
    OutVariable   = "IPv4Set"
    OutputType    = "Object"
    DNSName       = $ShortName.ToLower()
    ErrorAction   = "Stop"
    Verbose       = $true
}
# Check DNS and amend if required
if ($IPv4Settings.DNSServer -ne $dnsserver) {
    Write-Host "DNS server entries not correct, amending"
    $NewDCHPv4Settings.Add("DNSServer", $dnsserver)
    $NewDCHPv4Settings.Add("DNSServerType", $dnstype)
}

Set-HPEiLOIPv4NetworkSetting @NewDCHPv4Settings

问题是-如何将一元数组添加到splat数组中,以便保持完整性并正确处理?

根据要求-这是定义:

Name          : Set-HPEiLOIPv4NetworkSetting
ModuleName    : HPEiLOCmdlets
Module        : @{Name=HPEiLOCmdlets}
CommandType   : Cmdlet
Definition    : 
                Set-HPEiLOIPv4NetworkSetting [-Connection] <Connection[]> -InterfaceType <string[]> [-SharedNetworkPortType <string[]>] [-SNPPort <int[]>] 
                [-VLANEnabled <string[]>] [-VLANID <int[]>] [-DNSName <string[]>] [-DomainName <string[]>] [-NICEnabled <string[]>] [-FullDuplex <string[]>] 
                [-LinkSpeedMbps <string[]>] [-DHCPEnabled <string[]>] [-DHCPv4Gateway <string[]>] [-DHCPv4DomainName <string[]>] [-DHCPv4DNSServer <string[]>] 
                [-DHCPv4WINSServer <string[]>] [-DHCPv4StaticRoute <string[]>] [-DHCPv4NTPServer <string[]>] [-IPv4Address <string[]>] [-IPv4SubnetMask <string[]>] 
                [-IPv4Gateway <string[]>] [-IPv4StaticRouteIndex <int[][]>] [-IPv4StaticRouteDestination <string[][]>] [-IPv4StaticRouteMask <string[][]>] 
                [-IPv4StaticRouteGateway <string[][]>] [-DNSServerType <string[][]>] [-DNSServer <string[][]>] [-RegisterDDNSServer <string[]>] [-PingGateway 
                <string[]>] [-RegisterWINSServer <string[]>] [-WINSServerType <string[][]>] [-WINSServer <string[][]>] [-iLONICAutoDelay <int[]>] [-iLONICFailOver 
                <string[]>] [-iLONICFailOverDelay <int[]>] [-iLONICAutoSelect <string[]>] [-iLONICAutoSNPScan <int[]>] [-OutputType <string>] [-Force] 
                [<CommonParameters>]

ParameterSets : {@{Name=__AllParameterSets; IsDefault=False; Parameters=System.Management.Automation.PSObject[]}}

1 个答案:

答案 0 :(得分:0)

就像在评论中一样,我的问题是由iLO本身/逻辑引起的,而不是splat错误。我的原始代码确实可以按预期工作:

$dnstype = ,@("Primary","Secondary","Tertiary")
$dnsserver = ,@("192.168.1.11","192.168.1.12","192.168.150.13")

$NewDCHPv4Settings = @{
    Connection    = $connection
    InterfaceType = "Dedicated"
    OutVariable   = "IPv4Set"
    OutputType    = "Object"
    DNSName       = $ShortName.ToLower()
    ErrorAction   = "Stop"
    Verbose       = $true
}
# Check DNS and amend if required
if ($IPv4Settings.DNSServer -ne $dnsserver) {
    Write-Host "DNS server entries not correct, amending"
    $NewDCHPv4Settings.Add("DNSServer", $dnsserver)
    $NewDCHPv4Settings.Add("DNSServerType", $dnstype)
}

Set-HPEiLOIPv4NetworkSetting @NewDCHPv4Settings