使用 Azure Runbook 在远程 VM 上的特定用户下调用 AzVMRunCommand 和 Start-Pro



我需要使用 Azure Powershell Runbook 在具有特定用户帐户的远程 VM 上运行启动进程

function Install-Postgres {
$username = "aact-import-vm1aact-importer"
$password = "ChangeMe!"

$cred = New-Object System.Management.Automation.PSCredential -ArgumentList `
@($username,(ConvertTo-SecureString -String $password -AsPlainText -Force))
write-output $cred
# run pg installer
Start-Process "C:Program FilesWindowsPowerShellModulesInstall-Postgrespostgresql.exe" -ArgumentList `
"--mode unattended", "--unattendedmodeui none",`
"--prefix `"C:Program FilesPostgreSQL10`"", "--datadir `"C:Program FilesPostgreSQL10data`"", 
"--superpassword `"ChangeMe!`"",`
"--servicename `"postgres`"", "--serviceaccount `"postgres`"", "--servicepassword `"ChangeMe!`""`
-Wait -Credential $cred;
}
$script = Get-Content Function:Install-Postgres
Out-File -FilePath Install.ps1 -InputObject $script
#Note that the -ScriptPath should not point to the remote path(in remote vm), it should point to the local path where you execute the command Invoke-AzureRmVMRunCommand
$output = Invoke-AzVMRunCommand -ResourceGroupName $resourceGroupName -Name $vmName -CommandId 'RunPowerShellScript' -ScriptPath Install.ps1
write-output $output.Value
#after execution, you can remove the file
Remove-Item -Path Install.ps1

上面的脚本生成以下错误:

启动进程:由于错误:访问被拒绝,此命令无法运行。

如果我在没有特定凭据的情况下运行上面的脚本,postgres 安装程序会在日志中产生此错误:

Executing icacls "C:WindowsTemp/postgresql_installer_1ef9b3f2c6" /T /Q /grant "WORKGROUPaact-import-vm1$:(OI)(CI)F"
Script exit code: 1332
Script output:
Successfully processed 0 files; Failed processing 1 files
Script stderr:
WORKGROUPaact-import-vm1**$**: No mapping between account names and security IDs was done.

请注意,有符号$而不是用户名。

但是,如果我在 VM 上运行它,它工作正常并在日志中生成以下行:

Executing icacls "C:Usersaact-importerAppDataLocalTemp2/postgresql_installer_2662c862ff" /T /Q /grant "aact-import-vm1aact-importer:(OI)(CI)F"
Script exit code: 0

据我所知,如果我在没有凭据的情况下远程运行 runbook 脚本,它会在 NTAUTHORITY\SYSTEM 下运行,这就是为什么 postgres 安装程序日志中有符号 $ 而不是用户名的原因。如果我在本地运行它,它会使用正确的用户,并且一切正常。

问题是:如何指定用户帐户以在远程 VM 上运行启动进程?

关于 msdn https://social.msdn.microsoft.com/Forums/en-US/a7fa0ca8-5cba-42bb-8076-9a8d4a654beb/invokeazvmruncommand-and-startprocess-under-specific-user-on-remote-vm-using-azure-runbook?forum=azureautomation#a7fa0ca8-5cba-42bb-8076-9a8d4a654beb 的相同问题

对于那些感兴趣的人:

经过MS支持的调查,他们确认运行手册(非混合(始终在NTAUTHORITY\SYSTEM下运行

最新更新