Start-Process -Credential 在 windows-startup-script-ps1 中默默失败

问题描述 投票:0回答:1

我正在尝试在 GCP 上自动设置 ADFS。为此,我尝试在使用

New-ADuser
安装 ADFS 之前创建一个服务用户。我想在
windows-startup-script-ps1
中做到这一点。但是,由于脚本运行的帐户没有执行此操作的权限,因此我需要切换到委派管理员帐户。当我尝试使用
Start-Process
执行此操作时,它不会提供任何输出,并且实际上不会运行该命令。这是一个简化的示例,其中使用
echo
Out-File
而不是
New-ADuser

# Copyright 2024 Google LLC.
# SPDX-License-Identifier: Apache-2.0
$Password = ConvertTo-SecureString '[redacted]' -AsPlainText -Force
$Credential = New-Object System.Management.Automation.PSCredential 'adfs\setupadmin', $Password
Start-Process powershell '-NoProfile', '-Command', {echo 'it works' | Out-File test.txt} -Credential $Credential -Wait

Write-Output 'test.txt:'
Get-Content test.txt

这会产生错误:

Get-Content : Cannot find path 'C:\Windows\system32\test.txt' because it does not exist

如果我删除

-Credential $Credential
,它会运行,但如果我尝试执行
New-ADuser
,它将失败并出现权限错误。另一方面,如果我在创建实例后通过 RDP 进入实例并以管理员身份运行 Powershell,则
Start-Process
-Credential
可以正常工作。为什么它在
windows-startup-script-ps1
中不起作用?有解决办法吗?

我还尝试提供

powershell.exe
的完整路径,但结果相同。

更新:我尝试使用

gcloud compute ssh
而不是启动脚本(这本来是一个可以接受的解决方法),发现它也在那里失败(相同的行为)。根据我的研究,看起来
Start-Process -Credential
可能在会话 0 中不起作用。

windows powershell google-cloud-platform active-directory google-compute-engine
1个回答
0
投票

注意:这篇文章不提供解决方案,而只是根据您自己的观察指出问题

  • 看起来,您的代码在

    services
    会话(窗口站)中运行,即在 ID 为 0
    invisible
    会话中运行 Windows 服务。

  • 事实上,尝试将此会话中的

    Start-Process
    -Credential
    一起使用,即以不同的用户身份运行 ,失败,即使调用者以海拔 运行 (这在该会话中是典型的) ).

    • 从调用者的

      特权来看问题是什么并不明显:whoami /all

      both提升的交互式会话中显示以下内容 - 命令成功 - 以及在会话中0
      进程 - 命令
      失败的地方:

      • SeImpersonatePrivilege
        Enabled
        
        
      • SeDelegateSessionUserImpersonatePrivilege
        Disabled
        
        
  • 在会话

    0

     的失败调用中,
    Start-Process -Credential
     创建的进程报告的进程退出代码为(通过 
    (Start-Process ... -Wait -PassThru).ExitCode
     获取):

© www.soinside.com 2019 - 2024. All rights reserved.