3 Replies Latest reply on Sep 14, 2018 10:54 AM by Zsoldier

    How to capture powercli output as an object instead of text.

    shyamachilles Lurker

      Hi!

       

      We primarily use python for our automation framework.

       

      For testing involving vSphere interaction we are trying to leverage wrapping the PowerCLI commands with python.

       

      I am trying to automate creating a storage policy based on the steps without capturing the commands in the vaiables -

       

      1. $cap = Get-SpbmCapability -Name '<someCapability>'
      2. $rule = New-SpbmRule -Capability $cap -value 1
      3. $ruleset = New-SpbmRuleSet -AllOfRules $rule
      4. $policy = New-SpbmStoragePolicy -Name 'MyPolicy' -Description 'Description of MyPolicy' -AnyOfRuleSets $ruleset

       

      Output of #2 is text as-

       

      Name                           Port  User

      ----                           ----  ----

      c3-dl380pg8-88-vm02.cxo.sto... 443   VSPHERE.LOCAL\Administrator

       

       

      Capability : com.hp.3par.spbm.provisioningGroup

      Value      : 1

      AnyOfTags  :

       

      Could I please know how to obtain the output of "New-SpbmRule" as an object instead of plain text  in Step 2? Or convert the output to an  "VMware.VimAutomation.Storage.Types.V1.Spbm.SpbmRule" object as the subsequent command in Step3 requires the $rule as an object.

        • 1. Re: How to capture powercli output as an object instead of text.
          Zsoldier Expert
          vExpert

          This is how I do it for vSAN.

           

           

          $rules = @()

           

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.cacheReservation) -Value 0  

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.checksumDisabled) -Value $False

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.forceProvisioning) -Value $False

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.hostFailuresToTolerate) -Value 1

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.iopsLimit) -Value 0

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.proportionalCapacity) -Value 100

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.replicaPreference) -Value "RAID-1 (Mirroring) - Performance"

             $rules += New-SpbmRule -Capability (Get-SpbmCapability -name VSAN.stripeWidth) -Value 1

             $ruleset = New-SpbmRuleSet -AllOfRules $rules

             $storagepolicy = New-SpbmStoragePolicy -Name $policyname -AnyOfRuleSets $ruleset

          • 2. Re: How to capture powercli output as an object instead of text.
            LucD Guru
            vExpertCommunity WarriorsUser Moderators

            Not sure how you are capturing this in phyton.

            The output of the PowerCLI cmdlets is an object, that you can use in a subsequent cmdlet as a value to a parameter.

            If you are displaying such an object on the console, and capturing that in phyton, you will have the text ([string]) representation of the object.

            And that string will most of the time (unless OBN is supported for a parameter) not be accepted as a valid value for the parameter.

             

            I suggest to run the 4 lines with PowerCLI cmdlets in 1 block of code from phyton.

            And eventually use the pipeline to pass objects from one cmdlet to the next.

             

            Doing this line by line, and trying to capture the objects with a phyton wrapper will not work I'm afraid.

            • 3. Re: How to capture powercli output as an object instead of text.
              Zsoldier Expert
              vExpert

              To wrap it, I would suggest making a ps1 file as a function that you can pass parameters to as plain text/strings.  This can even be a json file.

              Example json:

              {

              "RuleSets":

              [

                   {

                        "Name": "NameofPolicy",

                        "Description": "My super duper awesome storage policy.",

                        "Rules":

                        [

                             {

                             "Name": "Name of Rule",

                             "Value": 1   

                             }

                        ]

                   },

                   {

                        "Name": "NameofAnotherPolicy",

                        "Description": "My other super duper awesome storage policy.",

                        "Rules":

                        [

                             {

                             "Name": "Name of Rule",

                             "Value": 1   

                             },

                             {

                             "Name": "Name of Rule",

                             "Value": 1   

                             }

                        ]

                   }

              ]

              }

               

              Save below as a .ps1 file.  Then you can use python subprocess to call pwsh, pass it the ps1 followed by a json formatted string.  For this example anyway.

              param

               

              (

                [Parameter(Mandatory = $true, HelpMessage = "JSON passed as a string to configure.")]

                [String]$ConfigString,

              )

              Begin{}

              Process

              {

              $v = $ConfigString | Convertfrom-json

               

              Foreach ($ruleset in $v.rulesets)

              {

              $Rules = @()

              Foreach ($rule in $Ruleset.Rules)

                   {

                   $rules += New-SpbmRule -Capability (Get-SpbmCapability -Name $v.rule.name) -Value $rule.value

                   }

                   $mynewruleset = New-SpbmRuleSet -AllofRules $rules

                   New-SpbmStoragePolicy -Name $policyname -AnyOfRuleSets $mynewruleset -Description $ruleset.description

              }

              }