VMware Cloud Community
JLogan2016
Enthusiast
Enthusiast
Jump to solution

Keeping a VIServer Session Open or Inter-script Communication

I have been working on wrapping a working PowerCLI script with a GUI so the work of bulk VM creation can be handed off to another group of people. The script and what I have run into thus far can be seen here: Datastore and VMHost vs Cluster

Both the GUI and original script are working, I am just trying to marry the two with the least amount of overhead. The issue I am currently working through is the GUI script returns the information on the VMs to be created in an array table, like so:

pastedImage_1.png

My concern is around the best means to pass this array to the PowerCLI script, so that I am not recreating the Connect-VIServer connection (to multiple servers) multiple times. I am just curious if exporting the table to a csv is my best/only option. Can an entire array be passed to the script on calling? Is any level of inter-script communication supported as in other languages, or is there a supported method for opening the connection in one script and then running another script within the scope of that connection?

If I am overthinking it, please feel free to say so Smiley Happy

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

A connection made with Connect-VIServer receives a SessionId.

You can use this SessionId to do a new Connect-VIServer (with the Session parameter).

And yes, you can pass an array as an argument to a function,

As an example

testarray.ps1

param(

    [int[]]$array

)

$array.Count

test

$t = 1,2,3,4

.\testarray.ps1 $t


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

View solution in original post

0 Kudos
5 Replies
LucD
Leadership
Leadership
Jump to solution

A connection made with Connect-VIServer receives a SessionId.

You can use this SessionId to do a new Connect-VIServer (with the Session parameter).

And yes, you can pass an array as an argument to a function,

As an example

testarray.ps1

param(

    [int[]]$array

)

$array.Count

test

$t = 1,2,3,4

.\testarray.ps1 $t


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast
Jump to solution

Thanks, I will take a look at these. I confirmed I can pass the SessionID from script to script; will play around with passing multiple IDs from multiple connections.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just take note that there is a session timeout.


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

0 Kudos
JLogan2016
Enthusiast
Enthusiast
Jump to solution

Thanks, I was thinking about this as well. As I am making a connection to multiple vCenter Servers, the prospect of grabbing all of the Session IDs and passing those to the second is making less and less sense. Also, while your example of passing the array worked great (will store that for future use!) as this array is going to be a large 2D - 15 or 16 column - array, I think I am over-complicating it. Best just to export to csv and then import, after all. Nevertheless, thanks for all the advice.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you are not starting a new PowerShell process, but instead would be using jobs, you could use a reference variable.


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

0 Kudos