我已经写了一个PowerShell脚本,生成用户不得不同意的“EULA”types的popup窗口。
它通过作为用户(非pipe理员)帐户的计划任务运行,在login时执行此操作。 它需要运行提升,所以我使用以下脚本来提升它:
$pw= convertto-securestring "myPassw0rd" -asplaintext –force $credential = new-object -typename system.management.automation.pscredential -argumentlist "-default-",$pw $localArgs = "/c Powershell c:\scripts\myScript.ps1" [System.Diagnostics.Process]::Start("cmd.exe", $localArgs, "Administrator", $credential.Password, $computer)
(我将encryption密码以使其更安全一些,但这与这个问题无关。)
无论如何 – 我的问题是,当脚本被调用时,它显示在我的“漂亮”EULApopup窗口后面的命令提示符窗口。
有没有办法隐藏/最小化命令窗口?
谢谢,
本
这应该是你所需要的:
$Process = new-Object System.Diagnostics.Process $Process.StartInfo.UserName="Administrator" $Process.StartInfo.Password=$Credential.Password $Process.StartInfo.Domain="$Computer" $Process.StartInfo.WindowStyle="Hidden" $Process.StartInfo.FileName="cmd.exe" $Process.StartInfo.Arguments="$localArgs" $Process.Start()
您可以使用Start-Process cmdlet(PowerShell 2.0):
启动过程cmd.exe -Credential $凭证-WindowStyle隐藏-WorkingDirectory … -ArgumentList …
您不能使用-Credential和-WindowStyle参数以及PowerShell v2,您需要使用PowerShell v3或者同时使用-NoNewWindow和-Credential参数
您可以使用PowerShell v2的以下代码:
$user = "{user}" $pass = ConvertTo-SecureString -String "{password}" -AsPlainText -Force $cred = new-object -typename System.Management.Automation.PSCredential ` -argumentlist $user, $pass start-process -Credential $cred -NoNewWindow powershell "-command & '{path and script}'"