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

Time:05-15

[After playing around with it for a few hours I did find a solution]

I have 2 different functions I have written to add a network profile. The first is to add a network profile with a password using 2 parameters $SSID, $PW

The second function is just adding a network profile that does not need a password. This one has just the one parameter $SSID

I'd like to combine these 2 functions into 1 that would just have a 3rd parameter to distinguish bewteen adding a Secure and Open network

My functions:

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)"
}

CodePudding user response:

Here is another way of doing it, by directly modifying the XML object

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)"
}

CodePudding user response:

After a little playing around I managed to make it work. Still willing to take criticism or advice on how to do this better please and thank you

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)"
}
  • Related