2013-07-10 37 views
1

背景故事海拔在PowerShell中

我有一个调用创建一个新的用户帐户后立即启用邮箱的脚本,但问题是我需要的,以便通过与命令一起管理凭证为它工作。该脚本是用GUI来完成的,它可以简化日常管理任务的过程,并且会被那些对PowerShell一无所知的人甚至是如何将目录切换到脚本所在的位置。正因为如此,我希望它在浏览器中双击时运行,并提示用户一次输入管理员凭据。一旦凭证被输入他们将使用下面的代码被存储在同一目录下的文件:

Try { 
    $credsFile = Import-Clixml "credentials" 
    $credsFile.Password = $credsFile.Password | ConvertTo-SecureString 
    $adminCreds = New-Object System.Management.Automation.PsCredential($credsFile.Username, $credsFile.Password) 
} 
Catch { 
    # This means the credentials file does not exist 
    $adminCreds = Get-Credential 
    $adminCreds = $adminCreds | Select-Object * 
    $adminCreds.password = $adminCreds.Password | ConvertFrom-SecureString 
    $adminCreds | Export-Clixml "credentials" 
    $adminCreds.password = $adminCreds.Password | ConvertTo-SecureString 
} 

尝试

由于启用邮箱不具有-Credential参数我传递给它我一直在试图找到一种方法来从一开始就用存储的凭证以管理员身份运行脚本。我创建了一个脚本,试图在提升状态下调用主脚本。

我想第一个问题是:

Start-Process -File "$PSHOME\powershell.exe" -ArgumentList "-NoExit","-Command Scripts\MainScript.ps1" -Credential $adminCreds -Wait 

,但它只是提示输入凭据(每次),从来没有加载脚本。

接下来我想:

start-process powershell -verb runas -argument "scripts\MainConsole.ps1" 

这将加载脚本只是从ISE内找到,但在点击Explorer时,翻了一番不会加载它。

我也研究过Invoke-Command和Invoke-Expression,试图以管理员身份运行Enable-Mailbox命令,但没有成功。有谁知道如何从一开始就以管理员身份运行脚本,或者至少将凭据传递给Enable-Mailbox和其他参数?

回答

1

假设您没有在邮件服务器上运行此脚本,可以在调用此连接时使用Exchange管理员凭据进行身份验证? 喜欢的东西:

$server = "exchangeserver.domain.local" 

Import-PSSession (New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://$server/PowerShell -Authentication Kerberos -Credential $Credential) 
Enable-Mailbox -Identity $cn -Alias $user 

我个人建议不要保存在一个文件vim的信任状,但你应该能够运行这是任何用户,并通过凭据传递到$凭证变量以上(或只是让他们每次登录)。

如果他们需要记住正确的用户名,等等,我很喜欢,像这样的东西帮助更多的帮助(取决于您帐户的命名):

$credential = Get-Credential "exchange_admin" # or 
$credential = Get-Credential $env:Username"_admin" 

在从图标运行的主题,我知道的一个可怕的解决方法是创建一个运行的批处理文件

@powershell -FilePath \\fullpath\to\script.ps1 -params