2016-10-23 10 views
1

J'essaie d'utiliser le code suivant de la question "PowerShell: Running a command as Administrator" non seulement pour élever automatiquement mon script pour qu'il s'exécute automatiquement dans un PowerShell de niveau administrateur, mais aussi pour que la session PowerShell au niveau de l'administrateur soit s'exécuter avec un niveau ExecutionPolicy de RemoteSigned. Je suppose que je dois utiliser quelque chose comme -ExecutionPolicy RemoteSigned dans $newProcess.Arguments mais je suis complètement perdu quant à savoir si c'est le cas, et si c'est alors quelle syntaxe puis-je utiliser pour créer les arguments multiples?Script d'auto-élévation + politique d'exécution

# 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 an administrator 
if ($myWindowsPrincipal.IsInRole($adminRole)) { 
    # We are running as an administrator, so change the title and background colour to indicate this 
    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"; 
    $Host.UI.RawUI.BackgroundColor = "DarkBlue"; 
    Clear-Host; 
} else { 
    # We are not running as an administrator, so relaunch as administrator 

    # Create a new process object that starts PowerShell 
    $newProcess = New-Object System.Diagnostics.ProcessStartInfo "PowerShell"; 

    # Specify the current script path and name as a parameter with added scope and support for scripts with spaces in it's path 
    $newProcess.Arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'" 

    # 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... 

Write-Host -NoNewLine "Press any key to continue..."; 
$null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown"); 
+0

[auto script élévatrices sans failles de sécurité] (https://blogs.msdn.microsoft.com/virtual_pc_guy/2010/09/23/a-self-elevating-powershell-script/) – lloyd

+0

Eh bien, ce n'est pas mien. Peut-être devrais-je expliquer mon objectif global. Ce que j'essaie finalement de faire est d'exécuter un script à partir d'une session Powershell non administrateur qui limitera la bande passante Internet pour un seul exécutable. Ma compréhension était que ceci ne peut être réalisé qu'à partir d'un script Powershell de niveau Administrateur. J'ai également besoin de cela pour pouvoir être exécuté à partir d'une machine Windows 10 avec ExecutionPolicy par défaut défini sur Restreint. Par conséquent, mon désir de réduire le niveau de l'ExecutionPolicy pour la durée du script. – Ikrananka

Répondre

1

$newProcess.Arguments est en effet où vous ajoutez les paramètres pertinents. Cependant, vous pouvez exécuter le script via le paramètre -File au lieu d'utiliser l'opérateur d'appel (&) dans un paramètre implicite -Command.

$newProcess = New-Object Diagnostics.ProcessStartInfo 'powershell.exe' 
$newProcess.Arguments = '-ExecutionPolicy RemoteSigned -File "' + 
         $script:MyInvocation.MyCommand.Path + '"' 
$newProcess.Verb = 'runas' 
[Diagnostics.Process]::Start($newProcess) 
+0

C'est parfait - fonctionne exactement comme j'espérais. Après avoir passé des heures ce matin à m'arracher les cheveux, vous l'avez résolu pour moi dans quelques instants. Je vous remercie :) – Ikrananka