VMware Cloud Community
WinStime
Enthusiast
Enthusiast
Jump to solution

How to load Powercli 6.x module in Powershell to work with vRO ?

Hi,

You are my last chance :smileycry:

My problem :

I had some workflow in vRO to deploy VM in a Private Cloud (vRA). One of the job is to change the default storage policy of a VM to another one.

With vRO, i didn't find an easy way to manage storage policy, so i use Powercli to deal with SP.

The script works fine, and i'm able to load the module from a basic powershell session

Here is the script :

Param(

  [string]$hostname

  )

# +------------------------------------------------------+

# |        Load VMware modules if not loaded             |

# +------------------------------------------------------+

if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {

    if (Test-Path -Path 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI' ) {

        $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\WOW6432Node\VMware, Inc.\VMware vSphere PowerCLI'

      

    } else {

        $Regkey = 'Registry::HKEY_LOCAL_MACHINE\SOFTWARE\VMware, Inc.\VMware vSphere PowerCLI'

    }

    . (join-path -path (Get-ItemProperty  $Regkey).InstallPath -childpath 'Scripts\Initialize-PowerCLIEnvironment.ps1')

}

if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {

    Write-Host "VMware modules not loaded/unable to load"

    Exit 99

}

$resultat=""

if ($hostname.Length -gt 0)

    {

    $vms=Get-VM $hostname

    }

    else

    {

    $vms=Get-VM

    }

foreach ($vm in $vms)

{

    $vm_name=$vm.name

    $vm = Get-VM -Name $vm_name

    $policy_name=($vm|Get-Datastore|Get-TagAssignment -Category storage).tag.name

    $policy = Get-SpbmStoragePolicy -Name $policy_name

    $hd = Get-HardDisk -VM $vm

    Set-SpbmEntityConfiguration -Configuration (Get-SpbmEntityConfiguration $vm) -StoragePolicy $policy

    Set-SpbmEntityConfiguration -Configuration (Get-SpbmEntityConfiguration $hd) -StoragePolicy $policy

}

Now, i would like to launch this script with a workflow like "invoke external script" from vRO, and when i do this, i had an error

com.vmware.library.powershell/invokeScript) Error in (Dynamic Script Module name : invokeScript#14) PowerShellInvocationError: Errors found while executing script

System.Management.Automation.CommandNotFoundException: The term 'Get-VM' 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.

   at System.Management.Automation.ExceptionHandlingOps.CheckActionPreference(FunctionContext funcContext, Exception exception)

   at System.Management.Automation.Interpreter.ActionCallInstruction`2.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)

   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)

   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(Boolean createLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)

   at System.Management.Automation.ScriptBlock.<>c__DisplayClass4.<InvokeWithPipe>b__2()

   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)

   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)

   at System.Management.Automation.ScriptBlock.InvokeUsingCmdlet(Cmdlet contextCmdlet, Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Object[] args)

   at Microsoft.PowerShell.Commands.InvokeExpressionCommand.ProcessRecord()

   at System.Management.Automation.CommandProcessor.ProcessRecord()

It says that a powercli function is not recognized. "get-vm", so i determined that Module has not been loaded.

How i could do this ?

The workflow is launch with a PSHost account that is admin of the server.

I think this error is linked with session/user, but don't know how to deal with

Help is appreciated, thanks Smiley Happy

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You didn't mention any versions, but if you're using PowerCLI 6.5R1, you could try to do

Get-Module -Name VMware* -ListAvailable | Import-Module


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

5 Replies
LucD
Leadership
Leadership
Jump to solution

You didn't mention any versions, but if you're using PowerCLI 6.5R1, you could try to do

Get-Module -Name VMware* -ListAvailable | Import-Module


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

WinStime
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

It's a 6.3 version. I am gonna try

0 Kudos
LucD
Leadership
Leadership
Jump to solution

It's only in 6.5R1 that it's all modules


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
WinStime
Enthusiast
Enthusiast
Jump to solution

yes, i see, i have updated the version and i'm checking if old WF still run and new one too

0 Kudos
WinStime
Enthusiast
Enthusiast
Jump to solution

Get-Module -Name VMware* -ListAvailable |import-module

Works great to load module in an external query from VRO,

great, thanks LucD:smileycheck:

0 Kudos