2016-06-11 47 views
0

請參閱下面的腳本: 我需要啓動此腳本,並在腳本中嵌入管理權限以將執行策略設置爲不受限制,然後在腳本結束時將其設置回。從我迄今發現的情況來看,這要麼不可能,要麼很難做到。我希望有一個更簡單的方法來做到這一點。將運行此腳本的用戶在其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" 
    } 
} 

回答

0

您可以使用:

$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 

,並與第二部分(辦公Comunicator的執行)

跟這個有問題,請使用變量$GBL_Credential:憑證將顯示在純文本如果有人試圖用記事本,PowerShell ISE或其他程序編輯腳本,他們可以看到密碼。

祝您有美好的一天。

+1

我會開始這個答案:'不管你做什麼,不要這樣做:'' –

0

從我在腳本中看到的,沒有必要提升。如果這只是爲了克服ExecutionPolicy而不是你的方法是錯誤的。 ExecutionPolicy用於防止用戶運行不受信任的腳本。到目前爲止,你的腳本就是其中之一。

正確的做法是使用證書爲腳本簽名,並在所有計算機上將ExecutionPolicy設置爲Allsigned。用戶將只能夠從現在開始運行簽名的腳本。

如果這是不可能的,我看到兩個選項:

  1. 用戶複製腳本的內容,並將其粘貼到PowerShell窗口
  2. 您設置ExecutionPolicy不受限制。請記住,如果用戶嘗試嚴肅處理某些內容,用戶仍然需要提升,但對於此腳本,不需要提升。

所以總而言之,ExecutionPolicy是爲了防止你正在嘗試做什麼,所以不要指望它很容易克服。這也不是你關閉和打開的東西。你應該考慮什麼是你可以接受的,並將其設置在你的環境中適當的水平。

相關問題