Comment définir une fonction PowerShell nécessitant une élévation?

Table des matières:

Comment définir une fonction PowerShell nécessitant une élévation?
Comment définir une fonction PowerShell nécessitant une élévation?

Vidéo: Comment définir une fonction PowerShell nécessitant une élévation?

Vidéo: Comment définir une fonction PowerShell nécessitant une élévation?
Vidéo: Têtes au foot et lésions cérébrales - 36.9° - YouTube 2024, Avril
Anonim
PowerShell peut être extrêmement utile pour de nombreuses tâches quotidiennes, mais si vous devez modifier certaines fonctions avec un peu de sécurité en tête, comment définissez-vous une fonction de sorte qu'elle nécessite une élévation? L’article d’aujourd’hui sur le SuperUser Q & R répond aux questions d’un lecteur curieux.
PowerShell peut être extrêmement utile pour de nombreuses tâches quotidiennes, mais si vous devez modifier certaines fonctions avec un peu de sécurité en tête, comment définissez-vous une fonction de sorte qu'elle nécessite une élévation? L’article d’aujourd’hui sur le SuperUser Q & R répond aux questions d’un lecteur curieux.

La séance de questions et réponses d’aujourd’hui nous est offerte par SuperUser, une sous-division de Stack Exchange, un groupe de sites Web de questions-réponses dirigé par la communauté.

La question

Lecteur SuperUser Vlastimil veut savoir comment définir une fonction PowerShell qui nécessite une élévation:

Since I cannot find any alternatives to Linux’s sudo elevation command, I have the following question. How do I define a PowerShell function that requires elevation, as in activating a UAC prompt on my Windows 8.1 Pro, 64-bit system? For example, say I run the following function:

With the following results:
With the following results:
Image
Image

To be completely clear, if I run PowerShell as “user”, then run the aforementioned function system-check, I want the function to elevate in order to be able to execute the command (I want the UAC prompt to appear).

Comment définissez-vous une fonction PowerShell qui nécessite une élévation?

La réponse

Ashton, contributeur à SuperUser, a la solution pour nous:

To run a specific command from an elevated window:

For example:
For example:
To run a specific script from an elevated window:
To run a specific script from an elevated window:
To run an entire PowerShell session that prompts the UAC:
To run an entire PowerShell session that prompts the UAC:
Image
Image

A function to return $True or $False if the current window is running with elevated permissions:

To ensure a script is only run As Admin, add this to the beginning:
To ensure a script is only run As Admin, add this to the beginning:
Image
Image

In PowerShell v4.0, the above can be simplified by using a #Requires statement:

Image
Image

Source: Run with Elevated Permissions [SS64.com]

Avez-vous quelque chose à ajouter à l'explication? Sound off dans les commentaires. Voulez-vous lire plus de réponses d'autres utilisateurs de Stack Exchange doués en technologie? Découvrez le fil de discussion complet ici.

Conseillé: