[玩了幾個小時后,我確實找到了解決方案]
我撰寫了 2 個不同的函式來添加網路組態檔。第一種是使用 2 個引數 $SSID、$PW 添加帶有密碼的網路組態檔
第二個功能只是添加不需要密碼的網路組態檔。這個只有一個引數 $SSID
我想將這 2 個功能組合成 1 個,只有第三個引數來區分添加安全和開放網路
我的職能:
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)"
}
uj5u.com熱心網友回復:
這是另一種方法,直接修改 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)"
}
uj5u.com熱心網友回復:
在玩了一會兒之后,我設法使它作業。仍然愿意就如何更好地做到這一點提出批評或建議,謝謝
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)"
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/476848.html
下一篇:新手powershell引數問題
