0
votes

Basically I am just trying a very basic thing of accessing the VM details using Azure DSC. 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
  • 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. 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. 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!!!!!!

    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 = {
        }            
    }   
} 

}

1

1 Answers

0
votes

Command Get-AutomationPSCredential works in Azure Automation.

For DSC, pass credentials using Get-Credential. Add parameter

[Parameter()]
[pscredential]
$Credential

And replace Get-AutomationPSCredential with Get-Credential -Credential $Credential.