1

나는 command1.ps1 스크립트를 사용하여 Azure Custom Script Extension을 대상 VM에 설치하고 command2.ps1을 실행합니다. command2.ps1은 도메인 관리자 (따라서 -Credential $Credentials)와 같은 스크립트 (ScriptBlock 내부)를 실행해야합니다. command2.ps1을 수동으로 실행하고 $ domainAdminName 및 $ domainAdminPassword를 입력하면 작동하지만 command1.ps1을 통해 실행할 때 작동하지 않습니다. 어쩌면 문제는 Azure Custom Script Extension이 command2.ps1을 시스템 계정으로 실행하는 이유입니까? 제발, 스크립트 작업을 도와주세요. command1.ps1 :Azure 사용자 정의 스크립트 확장. 다른 사용자로 스크립트 실행

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

어쩌면 문제가 원인 Azure 사용자 지정 스크립트 확장에 의해 command2.ps1을 시스템 계정으로 실행 하시겠습니까?

예, Azure Custom Script Extension은 시스템 계정으로 실행됩니다. 즉 Azure VM 사용자 정의 스크립트 확장을 사용하면 가장 높은 시스템 권한이 필요한 경우에도 모든 종류의 코드를 실행할 수 있습니다. 다음 그림과 같이 CustomScriptHandler.exe 프로세스가 시스템 계정으로 실행되는 것을 볼 수 있습니다. 푸른 사용자 정의 스크립트 확장을 이해에 대한

enter image description here

더, this article를 참조하십시오.

제발, 스크립트 작업을 도와주세요.

스크립트가 정상입니다. 이 문제는 시스템 권한에 관한 것입니다. 오류 로그에 따라 Azure 사용자 정의 확장 스크립트를 통해 스크립트를 실행하려는 경우 시스템 계정에 사용 권한을 할당하고 VM의 일부 구성을 변경하여 해결할 수 있습니다. 오류를 해결하는 방법에 대한 자세한 내용은 this link을 참조하십시오. 템플릿에

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

매개 변수 이름은에 매개 변수의 이름과 일치해야합니다

1

이 같은 매개 변수를 추가하는

"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 구성에서 작동하는 푸른 DSC 확장을 사용할 수 있습니다 dsc. powershell을 사용하면 비슷한 것을 알 수 있습니다. 나는 개인적으로 시도한 적이 없지만 가능해야합니다.

+0

감사합니다. 그러나 우리의 경우 PowerShell이 ​​더 좋습니다. – WinBoss

관련 문제