Kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio?

Turinys:

Kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio?
Kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio?

Video: Kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio?

Video: Kaip apibrėžti
Video: How to Watch March Madness 2021 Without Cable | Best & Cheapest Streaming TV Options! - YouTube 2024, Balandis
Anonim
"PowerShell" gali būti labai naudinga daugeliui kasdieninių užduočių, bet yra, bet jei reikia šiek tiek pagerinti kai kurias funkcijas, tada kaip apibrėžiate funkciją, kad ji turėtų pakilimą? Šiandien "SuperUser" Q & A įrašas turi atsakymą į įdomų skaitytojo klausimą.
"PowerShell" gali būti labai naudinga daugeliui kasdieninių užduočių, bet yra, bet jei reikia šiek tiek pagerinti kai kurias funkcijas, tada kaip apibrėžiate funkciją, kad ji turėtų pakilimą? Šiandien "SuperUser" Q & A įrašas turi atsakymą į įdomų skaitytojo klausimą.

Šiandieninė klausimų ir atsakymų sesija pateikiama su "SuperUser" - "Stack Exchange", bendruomenės valdoma Q & A grupių asociacija.

Klausimas

"SuperUser" skaitytuvas "Vlastimil" nori žinoti, kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio:

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

Kaip apibrėžti "PowerShell" funkciją, kuriai reikia aukščio?

Atsakymas

"SuperUser" autorius Ashton atsakė mums:

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]

Ar turite ką nors įtraukti į paaiškinimą? Garsas išjungtas komentaruose. Norite skaityti daugiau atsakymų iš kitų "Tech-savvy Stack Exchange" vartotojų? Patikrinkite visą diskusijų temą čia.

Rekomenduojamas: