简体   繁体   English

Azure DSC配置问题

[英]Azure DSC Configuration Issue

Basically I am just trying a very basic thing of accessing the VM details using Azure DSC. 基本上,我只是尝试使用Azure DSC访问VM详细信息的非常基本的事情。 I have done the following 我做了以下

  • Added a new Credential(which holds the username and password) and Variable(which holds the subscriptionId) under Shared Resources of my automation account 在我的自动化帐户的“共享资源”下添加了一个新的凭据(包含用户名和密码)和变量(其中包含subscriptionId)
  • Have implemented the following DSC code for retrieving the VM details: I am able to complie this file in the portal it generates the .MOF file as well. 已实现以下DSC代码以检索VM详细信息:我能够在也生成.MOF文件的门户中编译该文件。 But when I try to apply this to a node in the portal I get the following error: 但是,当我尝试将其应用于门户中的节点时,出现以下错误:

PowerShell DSC resource MSFT_ScriptResource failed to execute Set-TargetResource functionality with error message: The term 'Get-AutomationPSCredential' is not recognized as the name of a cmdlet, function, script file, or operable program. PowerShell DSC资源MSFT_ScriptResource无法执行Set-TargetResource功能,并显示错误消息:术语“ Get-AutomationPSCredential”未被识别为cmdlet,函数,脚本文件或可运行程序的名称。 Check the spelling of the name, or if a path was included, verify that the path is correct and try again 检查名称的拼写,或者是否包含路径,请验证路径是否正确,然后重试

Please note that the code written inside the SetScript executes successfully in a runbook!!!!!! 请注意,在SetScript中编写的代码可以在Runbook中成功执行!

    Configuration VMAzureDSCTasks
    {
param
(
    [Parameter()]
    [System.String]
    $NodeName = "rajeshserver",

    [Parameter()]
    [System.String]
    $ResourceGroupName = "rajeshresourcegroup",

    [Parameter()]
    [System.String]
    $VMSize = "Standard_D2s_v3",

    [Parameter()]
    [System.String]
    $CredentialAssetName = "cred"
)

Import-DscResource -ModuleName 'PSDesiredStateConfiguration'        

Node $NodeName
{        
    Script resizevm
    {                 
        SetScript = {

                    # Credentials and Subscription ID declaration
                    $Cred = Get-AutomationPSCredential -Name $using:CredentialAssetName   
                    $null = Add-AzureRmAccount -Credential $Cred -ErrorAction Stop
                $SubId = Get-AutomationVariable -Name 'SubscriptionId'
                $null = Set-AzureRmContext -SubscriptionId $SubId -ErrorAction Stop      

                try {
                $vm = Get-AzureRmVm -ResourceGroupName $using:ResourceGroupName -VMName $using:NodeName -ErrorAction Stop
                } catch {
                throw "Virtual Machine not found!!!!!!" 
                exit
                }

                # Output current VM Size
                $currentVMSize = $vm.HardwareProfile.vmSize

                Write-Verbose -Message "`nFound the specified Virtual Machine: $using:NodeName"
                Write-Verbose -Message "Current size: $using:currentVMSize"

        }
        TestScript = {
         return $false                
        }
        GetScript = {
        }            
    }   
} 

} }

Command Get-AutomationPSCredential works in Azure Automation. 命令Get-AutomationPSCredential在Azure自动化中运行。

For DSC, pass credentials using Get-Credential. 对于DSC,请使用Get-Credential传递凭据。 Add parameter 添加参数

[Parameter()]
[pscredential]
$Credential

And replace Get-AutomationPSCredential with Get-Credential -Credential $Credential. 并将Get-AutomationPSCredential替换为Get-Credential -Credential $ Credential。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM