簡體   English   中英

如何以管理員權限運行腳本以更改執行策略

[英]How to Run script with admin rights to change execution policy

參見以下腳本:我需要啟動此腳本,並在腳本內部嵌入管理員權限,以將執行策略設置為不受限制,然后在腳本結尾處將其重新設置。 根據到目前為止的發現,這是不可能的或很難做到的。 我希望有一種更簡單的方法可以做到這一點。 將要運行此腳本的用戶在其PC上沒有管理員權限,因此他們將無法提升權限並無法從Powershell內部手動運行。

Stop-process -Name OUTLOOK -ErrorAction SilentlyContinue -Force
Stop-process -Name communicator -ErrorAction SilentlyContinue -Force
Stop-process -Name lync -ErrorAction SilentlyContinue -Force
Stop-Process -Name UcMapi -ErrorAction SilentlyContinue -Force
Stop-Process -Name skypehost -ErrorAction SilentlyContinue -Force
Stop-Process -Name searchprotocolhost -ErrorAction SilentlyContinue -Force

$OstPath = "c:\users\$([environment]::username)"+ "\AppData" + "\local" + "\Microsoft" + "\Outlook" 
$ost = get-ChildItem $OstPath | where { $_.Extension -eq ".ost"}  
$ost | remove-Item -force

Start-Process Outlook


if (Test-Path 'C:\Program Files (x86)\Microsoft Office\office15\lync.exe')
{
    Start-Process 'C:\Program Files (x86)\Microsoft Office\office15\lync.exe'
}
Else
{
   write-host "Lync is not installed"
   if (Test-Path 'C:\Program Files (x86)\Microsoft Office Communicator')
   {
        Start-Process 'C:\Program Files (x86)\Microsoft Office Communicator\communicator.exe'
   }
   Else 
   {
           write-host "Communicator is not installed"
   }
}

您可以使用:

$GBL_Username = "Here type your username"
$GBL_Password = ConvertTo-SecureString –String "Here type your password in plain text" –AsPlainText -Force
$GBL_Credential = New-Object –TypeName System.Management.Automation.PSCredential –ArgumentList $GBL_Username, $GBL_Password

Start-Process 'C:\Program Files (x86)\Microsoft Office\office15\lync.exe' -Credential $GBL_Credential

並在第二部分(Office Comunicator的執行)中使用變量$GBL_Credential

這樣做有一個問題:憑據將以純文本顯示,並且如果有人嘗試使用記事本,PowerShell ISE或其他程序編輯腳本,他們將看到密碼。

祝你有美好的一天。

根據我在腳本中看到的內容,不需要提升。 如果這僅僅是為了克服ExecutionPolicy,那么您的方法是錯誤的。 ExecutionPolicy可以防止用戶運行不受信任的腳本。 到目前為止,您的腳本就是其中之一。

正確的做法是使用證書對腳本進行簽名,然后在所有計算機上將ExecutionPolicy設置為Allsigned。 從那時起,用戶將只能運行已簽名的腳本。

如果這不可能,我會看到2個選項:

  1. 用戶復制腳本的內容並將其粘貼到Powershell窗口中
  2. 您將ExecutionPolicy設置為不受限制。 請記住,如果用戶嘗試做嚴肅的事情,他們仍然需要抬高,但是對於此腳本,抬高不是必需的。

因此,總而言之,ExecutionPolicy可以完全阻止您嘗試執行的操作,因此不要期望它會很容易克服。 它也不是您關閉然后再打開的東西。 您應該考慮可以接受的條件,並將其設置為您環境中的適當水平。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM