2016-06-11 53 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是为了防止你正在尝试做什么,所以不要指望它很容易克服。这也不是你关闭和打开的东西。你应该考虑什么是你可以接受的,并将其设置在你的环境中适当的水平。

相关问题