我如何结合这两个功能来使用第三个参数添加网络配置文件?

How can I combine these 2 functions to add a network profile using a 3rd parameter?

[玩了几个小时后我确实找到了解决方案]

我编写了 2 个不同的函数来添加网络配置文件。 第一种是使用 2 个参数 $SSID, $PW

添加带有密码的网络配置文件

第二个功能只是添加不需要密码的网络配置文件。 这个只有一个参数 $SSID

我想将这 2 个函数合并为 1 个,只有第 3 个参数来区分添加安全和开放网络

我的职能:

function Add-SecureNetWork {

[CmdletBinding()]
param ( 
[Parameter (Mandatory = $True, ValueFromPipeline = $True)]
[string]$SSID,

[Parameter (Mandatory = $True)]
[string]$PW
)

$profilefile="ACprofile.xml"
$SSIDHEX=($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile="<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>$PW</keyMaterial>
            </sharedKey>
        </security>
    </MSM>
</WLANProfile>
"

$XMLFILE > ($profilefile)
netsh wlan add profile filename="$($profilefile)"
}
function Add-OpenNetWork {

[CmdletBinding()]
param ( 
[Parameter (Mandatory = $True, ValueFromPipeline = $True)]
[string]$SSID
)

$profilefile="ACprofile.xml"
$SSIDHEX=($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile="<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
        <security>
            <authEncryption>
                <authentication>open</authentication>
                <encryption>none</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
        </security>
    </MSM>
</WLANProfile>
"

$XMLFILE > ($profilefile)
netsh wlan add profile filename="$($profilefile)"
}

经过一番尝试后,我设法让它工作了。 仍然愿意接受批评或建议如何做得更好,谢谢

function Add-NetWork {

[CmdletBinding()]
param ( 
[Parameter (Mandatory = $True)]
[string]$SSID,

[Parameter (Mandatory = $True)]
[string]$s,

[Parameter (Mandatory = $False)]
[string]$PW

)

# -------------------------------------------------------------------------------------------------

$sec = switch ( $s )
{
    "secure"  { 
"
        <security>
            <authEncryption>
                <authentication>WPA2PSK</authentication>
                <encryption>AES</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
            <sharedKey>
                <keyType>passPhrase</keyType>
                <protected>false</protected>
                <keyMaterial>$PW</keyMaterial>
            </sharedKey>
        </security>
"
}
    "open" { 

"
        <security>
            <authEncryption>
                <authentication>open</authentication>
                <encryption>none</encryption>
                <useOneX>false</useOneX>
            </authEncryption>
        </security>
" 

}
}

# -------------------------------------------------------------------------------------------------

$profilefile="ACprofile.xml"
$SSIDHEX=($SSID.ToCharArray() |foreach-object {'{0:X}' -f ([int]$_)}) -join''
$xmlfile="<?xml version=""1.0""?>
<WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
    <name>$SSID</name>
    <SSIDConfig>
        <SSID>
            <hex>$SSIDHEX</hex>
            <name>$SSID</name>
        </SSID>
    </SSIDConfig>
    <connectionType>ESS</connectionType>
    <connectionMode>auto</connectionMode>
    <MSM>
$sec
    </MSM>
</WLANProfile>
"

$XMLFILE > ($profilefile)
netsh wlan add profile filename="$($profilefile)"
}

这是另一种方法,直接修改 XML 对象

function Add-NetWork {
    [CmdletBinding()]
    param ( 
        [Parameter (Mandatory = $True, ValueFromPipeline = $True)]
        [string] $SSID,

        [Parameter (Mandatory = $True)]
        [string] $PW,

        [Parameter (Mandatory = $True)]
        [ValidateSet("Secure", "Open")]
        [string] $NetworkType
    )

    switch ($NetworkType)
    {
        "Secure"
        {
            $authentication = "WPA2PSK"
            $encryption     = "AES"
        }

        "Open"
        {
            $authentication = "open"
            $encryption     = "none"
        }
    }

    $profilefile = "ACprofile.xml"
    $SSIDHEX = ($SSID.ToCharArray() | foreach-object {'{0:X}' -f ([int]$_)}) -join''
    [xml]$xmlFile = "<?xml version=""1.0""?>
    <WLANProfile xmlns=""http://www.microsoft.com/networking/WLAN/profile/v1"">
        <name>$SSID</name>
        <SSIDConfig>
            <SSID>
                <hex>$SSIDHEX</hex>
                <name>$SSID</name>
            </SSID>
        </SSIDConfig>
        <connectionType>ESS</connectionType>
        <connectionMode>auto</connectionMode>
        <MSM>
            <security>
                <authEncryption>
                    <authentication>$authentication</authentication>
                    <encryption>$encryption</encryption>
                    <useOneX>false</useOneX>
                </authEncryption>
            </security>
        </MSM>
    </WLANProfile>
    "

    $xmlNamespace = "http://www.microsoft.com/networking/WLAN/profile/v1"
    if ($NetworkType -eq "Secure")
    {
        $childNode = $xmlFile.CreateElement("sharedKey", $xmlNamespace)
        
        $childElement = $xmlFile.CreateElement("keyType", $xmlNamespace)
        $childText = $xmlFile.CreateTextNode("passPhrase")
        $childElement.AppendChild($childText)
        $childNode.AppendChild($childElement)

        $childElement = $xmlFile.CreateElement("protected", $xmlNamespace)
        $childText = $xmlFile.CreateTextNode("false")
        $childElement.AppendChild($childText)
        $childNode.AppendChild($childElement)

        $childElement = $xmlFile.CreateElement("keyMaterial", $xmlNamespace)
        $childText = $xmlFile.CreateTextNode($PW)
        $childElement.AppendChild($childText)
        $childNode.AppendChild($childElement)
        
        $xmlFile.WLANProfile.MSM.security.AppendChild($childNode)
    }

    $XMLFILE > ($profilefile)
    netsh wlan add profile filename = "$($profilefile)"
}