VMware Cloud Community
Jaren_Haywood
Contributor
Contributor
Jump to solution

ABX Action using PowerShell to change Storage Policy on VM and Disks fails

Hello all,

 

We have created an ABX Action which runs PowerShell/PowerCLI code to update the Storage Policy of a given newly deployed VM during initial power on. Although the code appears to accomplish the objective (the intended Storage Policy is applied to the VM and Hard Disk objects), the action ultimately declares a failure and therefore the entire deployment fails. We can run a modified version of the code from our local workstations without difficulty, so we question whether the issue is due to the PowerShell/PowerCLI version installed on the Cloud Extensibility Proxy appliance, assuming that is where the code is actually executed.

 

Cloud Extensibility Proxy version: 8.11.1.29524

Local PowerShell Version: 5.1.19041.2673

Local PowerCLI Version: 13.0.0.20829139

 

Below is the code we are attempting to run via the ABX Action:

 

 

function handler($context, $inputs) {
    #_______________________Debug and Verbose Mode Off____________________________________
    Set-PSDebug -Off
    $DebugPreference="SilentlyContinue"
    $VerbosePreference="SilentlyContinue"
    $ErrorActionPreference="SilentlyContinue"
    #_______________________Debug and Verbose Mode Off____________________________________
    Get-Module -Name VMware.PowerCLI | Select-Object -Property Name,Version

    #_______________________Get Secrets and Inputs____________________________________
    # vCenter Server
    $vc = $context.getSecret($inputs.vc_name)
    # vCenter User Credentials
    $username = $context.getSecret($inputs.vc_user)
    $password = $context.getSecret($inputs.vc_password)
    $data = $inputs['deployment_details']
    
    #_______________________Get Secrets and Inputs____________________________________
    # Connect to vCenter Server
    $connection = Connect-VIServer -Server vcenter.company.com -User $username -Password $password  -force
    
    #_______________________Set Storage Policy____________________________________
    #foreach($item in $data){
        # Set Storage Policy to 'Datastore Default' which is a null value
        $vm_name = $data.resourceName
        Write-Host "Target VM is $vm_name."
        # Line below can be commented out without impact since we can specify the particular VM in scope
        #$VMsToUpdate = Get-VM -name $vm_name | Get-SpbmEntityConfiguration
        $NewStoragePolicy = $null
        # Commented out foreach loop below as we can specify the particular VM in scope instead
        #foreach ($VM in $VMsToUpdate) {
            
            #$VMHDDs = Get-VM -Name $vm_name | Get-HardDisk
            $VMHDDs = Get-HardDisk -VM $vm_name
            Set-SpbmEntityConfiguration -Configuration $vm_name -StoragePolicy $NewStoragePolicy
            foreach ($HDD in $VMHDDs) { 
                Write-Host "Current Disk $HDD"
                Set-SpbmEntityConfiguration -Configuration $HDD -StoragePolicy $NewStoragePolicy
                Write-Host "Done configuring $HDD"
            }
        #}
    #}
}

 

 

Below is the "log" output from the action run:

 

Target VM is vratest4001.

Current Disk Hard disk 1

Done configuring Hard disk 1

Current Disk Hard disk 2

Done configuring Hard disk 2

WARNING: The 'Version' property of VirtualMachine type is deprecated. Use the 'HardwareVersion' property instead.

WARNING: Resulting JSON is truncated as serialization has exceeded the set depth of 100.

WARNING: GuestId property is deprecated. Please use ConfiguredGuestId and RuntimeGuestId properties instead.

WARNING: The 'State' property of VMHost type is deprecated. Use the 'ConnectionState' property instead.

WARNING: The 'DrsMode' property of Cluster type is deprecated. Use the 'DrsAutomationLevel' property instead.

WARNING:  The 'ScsiLun' property of VMHostStorageInfo type is deprecated. Use 'Get-ScsiLun' cmdlet instead.

WARNING: The 'VMKernelGatewayDevice' property of VMHostNetworkInfo type is deprecated and will be removed in a future release.

WARNING: The 'VirtualSwitch' property of VMHostNetworkInfo type is deprecated. Use 'Get-VirtualSwitch' cmdlet instead.

WARNING: The 'PhysicalNic' property of VMHostNetworkInfo type is deprecated. Use 'Get-VMHostNetworkAdapter' cmdlet instead.

WARNING: The 'ConsoleNic' property of VMHostNetworkInfo type is deprecated. Use 'Get-VMHostNetworkAdapter' cmdlet instead.

WARNING: The 'VirtualNic' property of VMHostNetworkInfo type is deprecated. Use 'Get-VMHostNetworkAdapter' cmdlet instead.

WARNING: The value of 'ExtensionData' property of VMHostNetworkInfo type is deprecated and will be changed to the value of 'ExtensionData2' property in a future release. Use 'ExtensionData2' property instead.

An item with the same key has already been added. Key: LinkedView

Finished running action code.

Exiting powershell process.

OperationStopped: /polyglot/main.ps1:264

Line |

 264 |          Throw $_.Exception

     |          ~~~~~~~~~~~~~~~~~~

     | An item with the same key has already been added. Key: LinkedView



Powershell process exited.

Max Memory Used: 552 MB

 

 

What peaked our interest the most is the line stating "An item with the same key has already been added. Key: LinkedView". Reviewing online search results indicates this may be somewhat of a known issue, but the solution appears elusive. I would like to stress once again that a modified version of the code (removes the secret variables, etc.) is functional on our local workstations.

 

Has anyone seen this before? Please share any thoughts you may have! Thank you!

Labels (3)
0 Kudos
1 Solution

Accepted Solutions
Ankush11s
VMware Employee
VMware Employee
Jump to solution

you should put these into variable as well , as it produce the output back to terminal .
Since Powercli runs inside the container , 

$storage_policy=Set-SpbmEntityConfiguration -Configuration $vm_name -StoragePolicy $NewStoragePolicy
$set_hdd = Set-SpbmEntityConfiguration -Configuration $HDD -StoragePolicy $NewStoragePolicy


thumb rule is always use variable for ABX code ,
Also always return some value in hash table format for powercli function for abx. 



You can run this  removing exit and it should work

View solution in original post

3 Replies
Jaren_Haywood
Contributor
Contributor
Jump to solution

Hi everyone,

 

We were able to successfully resolve the issue by instructing the action script to Exit following the foreach loop for hard disks. Although I cannot explain why there is a difference in behavior between running the script locally vs ABX Action on the Cloud Extensibility Proxy, I am pleased to see a positive result. The updated (working) script is below:

 

function handler($context, $inputs) {
    #_______________________Debug and Verbose Mode Off____________________________________
    Set-PSDebug -Off
    $DebugPreference="SilentlyContinue"
    $VerbosePreference="SilentlyContinue"
    $ErrorActionPreference="SilentlyContinue"
    #_______________________Debug and Verbose Mode Off____________________________________

    #_______________________Get Secrets and Inputs____________________________________
    # vCenter Server
    $vc = $context.getSecret($inputs.vc_name)
    # vCenter User Credentials
    $username = $context.getSecret($inputs.vc_user)
    $password = $context.getSecret($inputs.vc_password)
    $data = $inputs['deployment_details']
    
    #_______________________Get Secrets and Inputs____________________________________
    # Connect to vCenter Server
    $connection = Connect-VIServer -Server vcenter.company.com -User $username -Password $password  -force
    
    #_______________________Set Storage Policy____________________________________
  
    # Set Storage Policy to 'Datastore Default' which is a null value
    $vm_name = $data.resourceName
    Write-Host "Target VM is $vm_name."
    # Line below can be commented out without impact since we can specify the particular VM in scope
    $NewStoragePolicy = $null

    $VMHDDs = Get-HardDisk -VM $vm_name
    Set-SpbmEntityConfiguration -Configuration $vm_name -StoragePolicy $NewStoragePolicy
    foreach ($HDD in $VMHDDs) { 
       Write-Host "Current Disk $HDD"
       Set-SpbmEntityConfiguration -Configuration $HDD -StoragePolicy $NewStoragePolicy
       Write-Host "Done configuring $HDD"
    }
    exit
}

 

0 Kudos
Ankush11s
VMware Employee
VMware Employee
Jump to solution

you should put these into variable as well , as it produce the output back to terminal .
Since Powercli runs inside the container , 

$storage_policy=Set-SpbmEntityConfiguration -Configuration $vm_name -StoragePolicy $NewStoragePolicy
$set_hdd = Set-SpbmEntityConfiguration -Configuration $HDD -StoragePolicy $NewStoragePolicy


thumb rule is always use variable for ABX code ,
Also always return some value in hash table format for powercli function for abx. 



You can run this  removing exit and it should work

Jaren_Haywood
Contributor
Contributor
Jump to solution

Hi @Ankush11s and thank you for your response!

 

Yes, you are correct. We removed the "Exit" command and inserted the variables in front of each Set-SpbmEntityConfiguration command, as you indicated. The outcome was successful, which is excellent. Thank you for your guidance!

0 Kudos