¿Cómo define una función de PowerShell que requiere elevación?

Tabla de contenido:

¿Cómo define una función de PowerShell que requiere elevación?
¿Cómo define una función de PowerShell que requiere elevación?
Anonim
PowerShell puede ser extremadamente útil para muchas tareas diarias, pero si necesita ajustar algunas funciones teniendo en cuenta la seguridad, ¿cómo define una función para que requiera elevación? La publicación de preguntas y respuestas de SuperUser de hoy tiene la respuesta a la pregunta de un lector curioso.
PowerShell puede ser extremadamente útil para muchas tareas diarias, pero si necesita ajustar algunas funciones teniendo en cuenta la seguridad, ¿cómo define una función para que requiera elevación? La publicación de preguntas y respuestas de SuperUser de hoy tiene la respuesta a la pregunta de un lector curioso.

La sesión de Preguntas y Respuestas de hoy nos llega por cortesía de SuperUser, una subdivisión de Stack Exchange, un grupo de sitios web de preguntas y respuestas impulsado por la comunidad.

La pregunta

El lector SuperUser Vlastimil quiere saber cómo definir una función de PowerShell que requiere elevación:

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

¿Cómo define una función de PowerShell que requiere elevación?

La respuesta

El contribuyente de Superusuario Ashton tiene la respuesta para nosotros:

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]

¿Tienes algo que añadir a la explicación? Apaga el sonido en los comentarios. ¿Quieres leer más respuestas de otros usuarios de Stack Exchange con experiencia en tecnología? Echa un vistazo a la discusión completa aquí.

Tema popular

Consejos de expertos