高度を必要とするPowerShell関数をどのように定義しますか?

目次:

高度を必要とするPowerShell関数をどのように定義しますか? - ブログ 2023
高度を必要とするPowerShell関数をどのように定義しますか? - ブログ 2023
Anonim
PowerShellは、多くの日常的な作業のために非常に便利ですが、いくつかの機能を少しでも調整しなければならない場合は、高度を必要とするように関数をどのように定義しますか?今日のスーパーユーザのQ&A投稿には、興味深い読者の質問に対する答えがあります。
PowerShellは、多くの日常的な作業のために非常に便利ですが、いくつかの機能を少しでも調整しなければならない場合は、高度を必要とするように関数をどのように定義しますか?今日のスーパーユーザのQ&A投稿には、興味深い読者の質問に対する答えがあります。

今日の質疑応答セッションは、Q&Aウェブサイトのコミュニティ主導型のグループであるStack Exchangeの下位部門であるSuperUserの協力のもとに行われています。

質問

スーパーユーザーリーダーVlastimilは、高度が必要なPowerShell関数を定義する方法を知りたいと考えています。

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

高度を必要とするPowerShell関数をどのように定義しますか?

答え

SuperUserの投稿者Ashtonは答えてくれます:

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]

説明に追加するものがありますか?コメントで鳴り響く。他の技術に精通したStack Exchangeユーザーからの回答をもっとたくさん読んでみたいですか?ディスカッションスレッド全体をチェックしてください。

人気のあるトピック

専門家の助言