简体   繁体   中英

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

Changing Ethernet or Wi-Fi connections to Private/Public is something very easy to be done. I can do that either from PowerShell or Registries.

But, is there a way of changing Unidentified Networks or Identifying Networks to Private/Public in a programmatically way on Windows 10? I would like to include this step into one of my projects.

I found this answer on the sevenforums.com , but it applies only to Windows 7.

The General Command Is Using 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

To Run The Set-NetConnectionProfile Cmdlet Command On A Remote Computer

$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
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM