如何使用 powershell 连接到新的 Windows VM,但不使用 gcloud 命令或项目仪表板?

How to connect to a new Windows VM with powershell, but without the usage of the gcloud command or the project dashboard?

我的问题有点复杂,所以我将从 上下文 的简短解释开始。 目标是:


问题

没有API创建新的windows帐号和密码,就像gcloud命令行存在的一样:

gcloud compute reset-windows-password

但是当然需要用户连接到虚拟机。


我的想法

我尝试使用创建新用户的启动脚本。 powershell 脚本已经在 运行 机器(作为管理员)上按预期工作:

$ADSIComp =[adsi]"WinNT://test-remote"
$NewUser = $ADSIComp.Create('User','test')
$NewUser.SetPassword(('S€cur3P@ssword'))
$NewUser.SetInfo()
$Group = [ADSI]"WinNT://test-remote/Administrators,group"
$Group.Add("WinNT://test,user")

Google云提供了不同类型的启动脚本(see here)。我尝试使用 sysprep-specialize-script-ps1,所以它只运行一次。 (这个对吗?)。为了测试这是否有效,我在 google 云仪表板上创建了一个新的 windows 实例。在元数据部分,我插入了 sysprep-specialize-script-ps1 作为元键,括号中的脚本行作为值。

结果

我在仪表板上创建了一个新用户来登录 windows 机器。但是上面的脚本没有创建用户。所以我的方法失败了。


未决问题

你能看到我的过程中的(概念上的或技术上的)错误吗?

还有其他方法可以达到指定的目标吗?

谢谢!

您可以查看 Python/Java example provided here,它允许您使用 Google 云客户端库为您的 Windows VM 生成新密码并在本地实际打印它。

Automating Windows Password Generation

The gcloud compute reset-windows-password command allows a user with write access to the Compute Engine project to securely retrieve passwords for accounts on Windows instances.

The command does this by sending a username and an RSA public key to the instance. The agent running on the instance then either:

  • Creates an account on the instance for that username and generates a random password.
  • Resets the password to a random value if the account already exists.

The agent running on the instance encrypts the password with the provided public key and sends it back to the client to be decrypted by the corresponding private key.

流程是这样的:

  1. 您可以选择您传递的用户名。
  2. 您传入用于加密的RSA密钥对的public密钥。你把RSA密钥对的私钥保存在本地。
  3. 使用步骤 1 和 2 中提到的信息覆盖 VM 上的 windows-keys 元数据。
  4. 加密后的密码打印到COM4串口
  5. 然后您提取加密密码并在您的机器上本地解密。

Compute Engine APIs for updating an instance's metadata as well as reading an instance's Serial port output个。我之前指出的使用客户端库的示例使用了这些 API。

因此您可以使用客户端库构建 script/application 或直接调用我上面提到的 API。

重新发明轮子

由于我不知道您要避免使用 gcloud 的确切原因,我想指出的是,使用 REST API 的客户端库的手动方式执行的步骤基本完全相同通过方便的 gcloud compute reset-windows-password 命令完成。

换句话说,您可能在这里重新发明了轮子。这样做有合法的情况,比如您想将此逻辑添加到您的应用程序并且不想调用外部程序来执行此操作。如果您只打算将它用作脚本的一部分,我会建议重用 gcloud 除非有强烈的反对使用它的理由。