允许Powershell脚本以编程方式在设备上进行更改

时间:2018-09-26 16:12:49

标签: windows powershell

我正在开发一个简单的软件,该软件旨在通过启用/禁用以太网和wi-fi驱动程序来启用/禁用互联网连接。

我的问题是通过Java执行脚本时出现的。我设法自动提升Powershell会话以正确执行Disable-NetAdapter -Name Wi-fi -Confirm:$falseEnable-NetAdapter -Name Wi-fi -Confirm:$false,但是在执行时仍会弹出以下窗口: Allow app to make changes on device (请注意,这不是我获得的完全相同的窗口,这是来自互联网的示例,因为当提到的窗口弹出时,我无法截图屏幕)

我进行了一些研究,发现了三个可能的命令来解决此问题(我相信)。 Set-AppLockerPolicySet-ServiceGrant-SmbShareAccess

我的问题是:以编程方式启用Powershell在设备上进行更改是否正确?如果可以,如何进行更改。

这是我所有的ps1脚本。非常简单,类似于here中发现的自提升脚本。

# 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

0 个答案:

没有答案