背景故事海拔在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和其他参数?