Azure 自定义脚本扩展。以另一个用户身份执行脚本
Azure Custom Script Extension. Execute script as another user
我正在使用 command1.ps1 脚本在目标 VM 上安装 Azure 自定义脚本扩展并执行 command2.ps1。 command2.ps1 应该 运行 作为域管理员的脚本(在 ScriptBlock 中)(因此 -Credential $Credentials
)。当我手动 运行 command2.ps1 并输入 $domainAdminName 和 $domainAdminPassword 时,它工作,但是当 运行 通过 command1.ps1 连接它时,它不起作用。也许问题是由 Azure 自定义脚本扩展 运行ning command2.ps1 作为系统帐户引起的?请帮助我使脚本正常工作。
命令 1.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
命令 2.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:1.txt
Stop-Transcript
} -Credential $DomainCredentials
事件日志中还有一些错误:
https://i.stack.imgur.com/RKlZo.png
https://i.stack.imgur.com/XL28M.png
Maybe the problem is cause by Azure Custom Script Extension running
command2.ps1 as System account?
是,Azure 自定义脚本扩展 运行s 作为系统帐户。这意味着使用 Azure VM 自定义脚本扩展,我们可以 运行 任何类型的代码,即使它需要最高系统权限。如下图,我们可以看到 CustomScriptHandler.exe 进程 运行s 作为系统帐户。
有关了解 Azure 自定义脚本扩展的更多信息,请参阅 this article。
Please, help me make the script work.
你的脚本没问题。这个问题是关于系统权限的。根据您的错误日志,如果您想通过 Azure 自定义扩展脚本 运行 您的脚本,您可以尝试通过向 系统帐户 分配权限并更改一些配置来解决它你的虚拟机。更多错误解决方法,可参考this link.
您可以使用 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 找出类似的东西。我个人没试过,但应该是可以的。
我正在使用 command1.ps1 脚本在目标 VM 上安装 Azure 自定义脚本扩展并执行 command2.ps1。 command2.ps1 应该 运行 作为域管理员的脚本(在 ScriptBlock 中)(因此 -Credential $Credentials
)。当我手动 运行 command2.ps1 并输入 $domainAdminName 和 $domainAdminPassword 时,它工作,但是当 运行 通过 command1.ps1 连接它时,它不起作用。也许问题是由 Azure 自定义脚本扩展 运行ning command2.ps1 作为系统帐户引起的?请帮助我使脚本正常工作。
命令 1.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
命令 2.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:1.txt
Stop-Transcript
} -Credential $DomainCredentials
事件日志中还有一些错误: https://i.stack.imgur.com/RKlZo.png https://i.stack.imgur.com/XL28M.png
Maybe the problem is cause by Azure Custom Script Extension running command2.ps1 as System account?
是,Azure 自定义脚本扩展 运行s 作为系统帐户。这意味着使用 Azure VM 自定义脚本扩展,我们可以 运行 任何类型的代码,即使它需要最高系统权限。如下图,我们可以看到 CustomScriptHandler.exe 进程 运行s 作为系统帐户。
有关了解 Azure 自定义脚本扩展的更多信息,请参阅 this article。
Please, help me make the script work.
你的脚本没问题。这个问题是关于系统权限的。根据您的错误日志,如果您想通过 Azure 自定义扩展脚本 运行 您的脚本,您可以尝试通过向 系统帐户 分配权限并更改一些配置来解决它你的虚拟机。更多错误解决方法,可参考this link.
您可以使用 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 找出类似的东西。我个人没试过,但应该是可以的。