简体   繁体   中英

Store and Re-use PowerShell Session with PowerCLI Module Loaded and Connected to vCenter Server

I have a requirement where, I need to execute 3 PowerShell Scripts and each script connects to PowerCLI and then connects to VCenter Server and executes the code.

Ouput of Script-1 is input of Script-2, Output of Script-2 is Input of Script-3.

The problem that I am facing is the time taken the to load PowerCLI module in each of these scripts and then again the time taken to Connect to vCenter Server in each script.

All the 3 Scripts execute in sequence, but sometimes they all may run on same server or different servers each.

Is there a way to save the PowerShell session (Where the PowerCLI module is loaded and as well the Connection to the vCenter remains intact) created by Script-1, so that the same can be used

by Script-2 and Script-3?

I know it may sound a bit crazy, but is there a solution or workaround to store the PowerShell session in some object or session variable or file etc?

PowerShell has the notion of profiles. You could setup the profile to import PowerCLI every time the PowerShell session is created.

The connection to the vCenter server is going to be the hard part. Those sessions time out after periods of inactivity (couple hours, generally) as well as after a total amount of time (several days).

If you do want to attempt to re-use sessions, PowerCLI does allow it, with the Session parameter available through Connect-VIServer . The session ID is accessible through the following Global variable: $global:DefaultVIServer.SessionId

Hopefully that helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM