如何以编程方式将未识别网络设置为 Windows 10 上的私有或 Public 网络位置?

How to programmatically set Unidentified Networks to be a Private or Public Network Location on Windows 10?

将以太网或 Wi-Fi 连接更改为 Private/Public 是一件非常容易完成的事情。我可以从 PowerShell 或注册表中执行此操作。

但是,有没有一种方法可以在 Windows 10 上以编程方式将未识别网络或识别网络更改为 Private/Public?我想将此步骤纳入我的项目之一。

我在 sevenforums.com 上找到了这个答案,但它只适用于 Windows 7.

通用命令正在使用 Set-NetConnectionProfile Cmdlet

https://docs.microsoft.com/en-us/powershell/module/netconnection/set-netconnectionprofile?view=windowsserver2019-ps

#Update Windows Firewall from Public to Private
Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private

到 运行 远程计算机上的 Set-NetConnectionProfile Cmdlet 命令

$server = "servername"
$RequestingServer = $env:COMPUTERNAME

[STRING] $LocalUser = "Administrator"
[STRING] $LocalPassword = "Password01"
$LocalSecurePassword = $LocalPassword | ConvertTo-SecureString -AsPlainText -Force
$LocalCredentials = New-Object System.Management.Automation.PSCredential -ArgumentList $LocalUser, $LocalSecurePassword

#Update Windows Firewall Remotely
$LocalSession = New-PSSession -Computername $Server -Credential $LocalCredentials
Invoke-Command -Session $LocalSession -ScriptBlock {

$AddServer = $Using:RequestingServer

    #Update Windows Firewall from Public to Private
    Get-NetConnectionProfile | Set-NetConnectionProfile -NetworkCategory Private
    
    #Update Windows Firewall to allow remote WMI Access
    netsh advfirewall firewall set rule group="Windows Management Instrumentation (WMI)" new enable=yes
    
    #Update Trusted Hosts is not domain-joined and therefore must be added to the TrustedHosts list 
    Set-Item wsman:\localhost\Client\TrustedHosts -Value $AddServer -Force
    
    #Update Windows Firewall to allow RDP
    Enable-NetFirewallRule -DisplayGroup "Remote Desktop"
    
    #Enable RDP : 1 = Disable ; 0 = Enable
    Set-ItemProperty -Path "HKLM:\System\CurrentControlSet\Control\Terminal Server" -Name "fDenyTSConnections" -Value 0
}