Search code examples
powershellvmwarepowercli

You have modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. Invoke-VMScript


I am trying to run Invoke-VMScript inside a PowerShell workflow. The below code is working:

function Test-Workflow {
    Connect-VIServer -Server 1.2.3.4 -Username admin -Password password123

    # Invoke VMScript..
    Invoke-VMScript -VM myVirtualMachine01 -GuestUser 'administrator' -GuestPassword password123 -ScriptText ls c:\
}

The above works correctly. However running as a workflow:

workflow Test-Workflow {
    Connect-VIServer -Server 1.2.3.4 -Username admin -Password password123

    # Invoke VMScript..
    Invoke-VMScript -VM myVirtualMachine01 -GuestUser 'administrator' -GuestPassword password123 -ScriptText ls c:\
}

I get the error:

You have modified the global:DefaultVIServer and global:DefaultVIServers system variables. This is not allowed. Please reset them to $null and reconnect to the vSphere server.

What am I doing wrong?


Solution

  • PowerCLI used in conjunction with PoSh Workflows does some very odd things.

    To work around some of the oddness, connect to the vCenter server prior to running the workflow and then pass the session secret through to the workflow.

    For more detailed information, check out this blog post: http://www.lucd.info/2015/03/17/powercli-and-powershell-workflows/