PowerShell對很多日常任務都非常有用,但是如果您需要在考慮安全性的情況下調整某些函式,那麼如何定義函式以使其需要提升呢?今天的超級使用者問答帖子回答了一位好奇的讀者的問題。
今天的問答環節是由SuperUser提供的,SuperUser是Stack Exchange的一個分支,是一個由社群驅動的問答網站分組。
超級使用者讀者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:
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函式?
超級使用者貢獻者Ashton為我們提供了答案:
To run a specific command from an elevated window:
For example:
To run a specific script from an elevated window:
To run an entire PowerShell session that prompts the UAC:
A function to return $True or $False if the current window is running with elevated permissi***:
To ensure a script is only run As Admin, add this to the beginning:
In PowerShell v4.0, the above can be simplified by using a #Requires statement:
Source: Run with Elevated Permissi*** [SS64.com]
有什麼要補充的解釋嗎?在評論中發出聲音。想從其他精通技術的Stack Exchange使用者那裡瞭解更多答案嗎?在這裡檢視完整的討論主題。
學習程式設計的關鍵第一步是使用迴圈。謝天謝地,PowerShell將隨著您的技能不斷增長。 ...
檔案加密和解密可以是一個工作位。但是,使用PowerShell擴充套件,可以將程序精簡為一行命令。為此,我們需要安裝Gpg4win和Powershell模組。使用指令碼,我們可以自動化檔案加密和解密過程。 ...
大多數使用者從未聽說過PowerShell,更不用說觸控它並試用了。這可能是因為這個名字,聽起來像是隻有鐵桿IT極客才會使用的東西。或者是因為PowerShell的好處還不清楚。可能是因為最初的學習曲線。 ...
...示檔案資源管理器的所有技巧,以及如何在命令提示符和PowerShell中使用它們。您甚至可以將“複製到”和“移動到”新增到檔案資源管理器的上下文選單中。 在Windows 10中複製檔案或資料夾時,所選專案將被複制並儲存在所選的...
...您使用的是Windows 10 Home,則必須編輯Windows登錄檔或使用PowerShell,稍後我們將介紹如何進行此更改。如果您使用的是windows10professional或Enterprise,但在登錄檔中工作比在組策略編輯器中工作更舒服,您也可以這樣做。(但是,如果...
PowerShell是一種比命令提示符更強大的命令列shell和指令碼語言。自從Windows10釋出以來,它就成了預設的選擇,而且有很多方法可以開啟它。 PowerShell的使用更為複雜,但它比命令提示符更加健壯和強大。這就是為什麼它成為高階...
...;單擊“是”,登錄檔編輯器開啟。 透過命令提示符或powershell開啟登錄檔編輯器 您還可以從命令提示符或PowerShell開啟登錄檔編輯器。兩個應用程式的命令相同,但我們使用的是PowerShell。 開啟PowerShell,鍵入“regedit”,然後按...
使用Windows10的PowerShellcmdlet,您可以輕鬆地新增應用程式,以便在啟動時自動觸發VPN連線。自動化這個任務意味著當你使用某些應用程式時,你甚至不必考慮連線到VPN。 要利用PowerShell中的此功能,如果您還沒有VPN服務,則必須...