Я занимаюсь разработкой простого программного обеспечения, предназначенного для включения / отключения подключения к Интернету путем включения / отключения драйверов Ethernet и Wi-Fi.
Моя проблема возникает при выполнении моего скрипта через Java.Мне удалось самостоятельно поднять свой сеанс PowerShell, чтобы правильно выполнить Disable-NetAdapter -Name Wi-fi -Confirm:$false
и Enable-NetAdapter -Name Wi-fi -Confirm:$false
, но по-прежнему появляется следующее окно при выполнении: Разрешить приложению вносить изменения на устройстве (обратите внимание, этоне то же самое окно, которое я получаю, это пример из интернета, так как я не смог сделать снимок экрана с моим экраном, когда всплыло упомянутое окно)
Я провел некоторое исследование и нашел три возможные команды для решения этой проблемы (я полагаю), Set-AppLockerPolicy , Set-Service и Grant-SmbShareAccess .
Мой вопрос: Правильно липрограммно включить PowerShell для внесения изменений на устройстве, и если да, то как это сделать.
Вот весь мой скрипт ps1.Очень простой и похожий на самоподъемный скрипт, найденный здесь .
# Get the ID and security principal of the current user account
$myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
$myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
# Get the security principal for the Administrator role
$adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
# Check to see if we are currently running "as Administrator"
if ($myWindowsPrincipal.IsInRole($adminRole))
{
# We are running "as Administrator" - so change the title and background color to indicate this
$Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
$Host.UI.RawUI.BackgroundColor = "Gray"
clear-host
}
else
{
# We are not running "as Administrator" - so relaunch as administrator
# Create a new process object that starts PowerShell
$newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
# Hide window
$newProcess.WindowStyle = "Hidden"
# Specify the current script path and name as a parameter
$newProcess.Arguments = $myInvocation.MyCommand.Definition;
# Indicate that the process should be elevated
$newProcess.Verb = "runas";
# Start the new process
[System.Diagnostics.Process]::Start($newProcess);
# Exit from the current, unelevated, process
exit
}
# Run your code that needs to be elevated here
function Test-InternetAccess {
<#
.SYNOPSIS
Tests connectivity by pinging Google DNS servers once
.DESCRIPTION
Uses Test-Connection to ping a host, with -Quiet for returning a boolean. The default is a highly available Google DNS server (8.8.4.4)
.EXAMPLE
Test-InternetAccess
.EXAMPLE
Test-InternetAccess example.com
.INPUTS
None.
.OUTPUTS
Boolean
#>
param (
[String]
$RemoteHost = "google-public-dns-b.google.com"
)
Test-Connection -Computer $RemoteHost -BufferSize 16 -Count 1 -Quiet
}
function Go-Offline {
[CmdletBinding(SupportsShouldProcess=$True)]
param()
Write-Output "Disabling Internet connection"
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
Disable-NetAdapter -Name Wi-fi -Confirm:$false
}
function Go-Online {
[CmdletBinding(SupportsShouldProcess=$True)]
param()
Write-Output "Enabling Internet connection"
$XMLLocation = "$env:TEMP\Disabled-NICs.xml"
Enable-NetAdapter -Name Wi-fi -Confirm:$false
}
function ShiftConnection(){
if (Test-InternetAccess) {
. Go-Offline
}else{
. Go-Online
}
}
. ShiftConnection