【问题标题】:How do I start a new powershell instance and run commands in it?如何启动一个新的 powershell 实例并在其中运行命令?
【发布时间】:2013-12-17 19:02:55
【问题描述】:

这就是我想要完成的:

我需要用户在登录计算机时自动运行 powershell 脚本,让脚本启动 Elevated Powershell 提示(就像用户可以单击以管理员身份运行 Powershell)然后让它运行一些命令在新的 Powershell 对象中,然后关闭新的 Powershell 对象。

此函数当前将在 Elevated 模式下创建并运行一个新的 Powershell 对象。

function Set-Elevation
{
   # Create a new process object that starts PowerShell
   $newProcess = New-Object System.Diagnostics.ProcessStartInfo "powershell";
   # Indicate that the process should be elevated
   $newProcess.Verb = "runas";
   # Start the new process
   [System.Diagnostics.Process]::Start($newProcess) | Out-Null
}

但是,我如何让它在那里运行新命令?之后我将如何关闭该对象?

任何有关语法的提示都将不胜感激。

【问题讨论】:

    标签: c# powershell


    【解决方案1】:

    您可以使用内置的 Start-Process 命令:

    function IsAdministrator
    {
        $Identity = [System.Security.Principal.WindowsIdentity]::GetCurrent()
        $Principal = New-Object System.Security.Principal.WindowsPrincipal($Identity)
        $Principal.IsInRole([System.Security.Principal.WindowsBuiltInRole]::Administrator)
    }
    
    function IsUacEnabled
    {
        (Get-ItemProperty HKLM:\Software\Microsoft\Windows\CurrentVersion\Policies\System).EnableLua -ne 0
    }
    
    #
    # Main script
    #
    if (!(IsAdministrator))
    {
        if (IsUacEnabled)
        {
            [string[]]$argList = @('-NoProfile', '-NoExit', '-File', $MyInvocation.MyCommand.Path)
            $argList += $MyInvocation.BoundParameters.GetEnumerator() | Foreach {"-$($_.Key)", "$($_.Value)"}
            $argList += $MyInvocation.UnboundArguments
            Start-Process PowerShell.exe -Verb Runas -WorkingDirectory $pwd -ArgumentList $argList 
            return
        }
        else
        {
            # Log an error, do nothing or Start-Process -Credentials <admin_creds>
        }
    }
    

    【讨论】:

      【解决方案2】:

      如果您使用这个自升式脚本,您可以直接在 PS1 中运行您的命令,没有任何麻烦。

      $WID=[System.Security.Principal.WindowsIdentity]::GetCurrent();
      $WIP=new-object System.Security.Principal.WindowsPrincipal($WID);
      $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator;
      If ($WIP.IsInRole($adminRole)){
      }else {
        $newProcess = new-object System.Diagnostics.ProcessStartInfo 'PowerShell';
        $newProcess.Arguments = $myInvocation.MyCommand.Definition
        $newProcess.Verb = 'runas'
        [System.Diagnostics.Process]::Start($newProcess);Write-Host 'Prompting for Elevation'
        exit
      }
      
      Write-Host 'ElevatedCodeRunsHere';
      Write-Host 'Press any key to continue...'
      $null = $Host.UI.RawUI.ReadKey('NoEcho,IncludeKeyDown')
      

      【讨论】:

      • 你能不能把你的行拆分一下,一行中的多个命令很难阅读。
      • 所有分号行终止和空的if 块是怎么回事?如果您将每一行放在自己的行上,这肯定会更具可读性(或至少减少水平滚动)。
      • Exit 似乎也不适合我。仅使用一个命令编写 ps1 脚本:“exit”在调用时不会关闭会话。它似乎只结束了脚本 - 但我将如何关闭整个前一个对象?
      • 实际上使用这个脚本你不需要以前的对象,把这个片段放在以前的对象中,它会提升自己。
      • 我尝试了代码,但它似乎失败了 - 我尝试使用 Get-ChildItem 的参数执行它,但它表明它是一个无效的参数列表。 $pwd 也是在哪里传入的?
      猜你喜欢
      • 1970-01-01
      • 2019-11-11
      • 2020-01-16
      • 2014-11-25
      • 2018-04-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-13
      相关资源
      最近更新 更多