Azure自定义脚本扩展。以另一个用户身份执行脚本

时间:2017-09-26 18:08:47

标签: powershell azure azure-virtual-machine azure-powershell powershell-remoting

我使用command1.ps1脚本在目标VM上安装Azure自定义脚本扩展并执行command2.ps1。 command2.ps1应该作为域管理员运行一个脚本(在ScriptBlock中)(因此-Credential $Credentials)。当我手动运行command2.ps1并输入$ domainAdminName和$ domainAdminPassword时,它可以工作,但是当通过command1.ps1运行它时它不起作用。也许问题是由运行command2.ps1作为系统帐户的Azure自定义脚本扩展引起的?请帮我把脚本搞定。 command1.ps1:

    param
(
    [Parameter(Mandatory)]
    [String]$resourceGroupName,

    [Parameter(Mandatory)]
    [String]$targetVMname,

    [Parameter(Mandatory)]
    [String]$vmLocation,

    [Parameter(Mandatory)]
    [String]$FileUri,

    [Parameter(Mandatory)]
    [String]$nameOfTheScriptToRun,

    [Parameter(Mandatory)]
    [String]$customScriptExtensionName,

    [Parameter(Mandatory)]
    [String]$domainAdminName,

    [Parameter(Mandatory)]
    [String]$domainAdminPassword

)

Set-AzureRmVMCustomScriptExtension -Argument "-domainAdminName $domainAdminName -domainAdminPassword $domainAdminPassword" `
    -ResourceGroupName $resourceGroupName `
    -VMName $targetVMname `
    -Location $vmLocation `
    -FileUri $FileUri `
    -Run $nameOfTheScriptToRun `
    -Name $customScriptExtensionName

Remove-AzureRmVMCustomScriptExtension -Force `
    -ResourceGroupName $resourceGroupName `
    -VMName $targetVMname `
    -Name $customScriptExtensionName

command2.ps1:

    param
(
    [Parameter(Mandatory)]
    [String]$domainAdminName,

    [Parameter(Mandatory)]
    [String]$domainAdminPassword

)

$domainAdminPasswordSecureString = ConvertTo-SecureString -String $domainAdminPassword -AsPlainText -Force
$DomainCredentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $domainAdminName, $domainAdminPasswordSecureString

Invoke-Command -ComputerName localhost -ScriptBlock {
Start-Transcript C:\transcript1.txt
New-Item C:\111.txt 
Stop-Transcript
} -Credential $DomainCredentials

事件日志中还有一些错误: https://i.stack.imgur.com/RKlZo.png https://i.stack.imgur.com/XL28M.png

2 个答案:

答案 0 :(得分:3)

  

问题可能是由Azure自定义脚本扩展程序运行引起的   command2.ps1作为系统帐户?

是的,Azure自定义脚本扩展作为系统帐户运行。这意味着使用Azure VM自定义脚本扩展,即使它需要最高的系统权限,我们也可以运行任何类型的代码。如下图所示,我们可以看到CustomScriptHandler.exe进程作为系统帐户运行。

enter image description here

有关了解Azure自定义脚本扩展的详细信息,请参阅this article

  

请帮我制作剧本。

你的脚本没问题。此问题与系统特权有关。根据您的错误日志,如果您想通过Azure自定义扩展脚本运行脚本,可以尝试通过为系统帐户分配权限并更改VM上的某些配置来解决此问题。有关如何解决错误的更多信息,请参阅this link

答案 1 :(得分:1)

您可以使用Azure DSC扩展来解决该问题

"properties": {
    "publisher": "Microsoft.Powershell",
    "type": "DSC",
    "typeHandlerVersion": "2.20",
    "autoUpgradeMinorVersion": true,
    "settings": {
        "configuration": {
            "url": "url",
            "script": "script.ps1",
            "function": "function"
        },
        "configurationArguments": {
            "regular": "arguments"
        }
    },
    "protectedSettings": {
        "configurationArguments": {
            "DomainCredentials": {
                "userName": "user",
                "password": "password"
            }
        }
    }

在您的DSC配置中添加如下参数:

[Parameter(Mandatory)] # doesn't have to be mandatory, just copy pasting
[System.Management.Automation.PSCredential]$DomainCredentials,

模板中的参数名称必须与dsc中的参数名称匹配。您可以使用PowerShell找出类似的东西。我个人从未尝试过,但它应该是可能的。

相关问题