|
重发一下 之前的网路不好 格式又有问题
我想我下面的代码会是你想要的 里面有代码,可以的话就帮我点赞 可惜我的csdn博客里有讲代码,可惜不能发送uri, 如果你想看 可以 在csdn中搜索“windows powershell 切换wifi开关“关键词来搜索
如何使用:保存为test.ps1, 以管理员身份打开powershell,运行 powershell -ExecutionPolicy bypass -File "存放路径\test.ps1" -WlanStatus On/Off,最后的On就是打开,Off就是关闭
代码如下
[CmdletBinding()]
Param (
[Parameter(Mandatory=$true)][ValidateSet('Off', 'On')][string]$WlanStatus
)
# 确保 WLAN 服务正在运行
If ((Get-Service wlansvc).Status -eq 'Stopped') { Start-Service wlansvc }
# 加载 Windows Runtime 组件
Add-Type -AssemblyName System.Runtime.WindowsRuntime
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() |
? { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]
Function Await($WinRtTask, $ResultType) {
$asTask = $asTaskGeneric.MakeGenericMethod($ResultType)
$netTask = $asTask.Invoke($null, @($WinRtTask))
$netTask.Wait(-1) | Out-Null
$netTask.Result
}
# 加载必要的 WinRT 类型
[Windows.Devices.Radios.Radio,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
[Windows.Devices.Radios.RadioAccessStatus,Windows.System.Devices,ContentType=WindowsRuntime] | Out-Null
Await ([Windows.Devices.Radios.Radio]::RequestAccessAsync()) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null
$radios = Await ([Windows.Devices.Radios.Radio]::GetRadiosAsync()) ([System.Collections.Generic.IReadOnlyList[Windows.Devices.Radios.Radio]])
$wlan = $radios | ? { $_.Kind -eq 'Wifi' }
Await ($wlan.SetStateAsync($WlanStatus)) ([Windows.Devices.Radios.RadioAccessStatus]) | Out-Null |
|