[英]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.我有一个要求,我需要执行 3 个 PowerShell 脚本,每个脚本连接到 PowerCLI,然后连接到 VCenter Server 并执行代码。
Ouput of Script-1 is input of Script-2, Output of Script-2 is Input of Script-3. Script-1 的输出是 Script-2 的输入,Script-2 的 Output 是 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.我面临的问题是在每个脚本中加载 PowerCLI 模块所花费的时间,然后是在每个脚本中连接到 vCenter Server 所花费的时间。
All the 3 Scripts execute in sequence, but sometimes they all may run on same server or different servers each.所有 3 个脚本都按顺序执行,但有时它们可能都运行在同一台服务器或不同的服务器上。
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有没有办法保存由Script-1创建的PowerShell session(PowerCLI模块加载的地方,以及与vCenter的连接保持不变),以便可以使用相同的
by Script-2 and Script-3?通过 Script-2 和 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? 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. PowerShell 具有配置文件的概念。 You could setup the profile to import PowerCLI every time the PowerShell session is created.
您可以设置配置文件以在每次创建 PowerShell session 时导入 PowerCLI。
The connection to the vCenter server is going to be the hard part.与 vCenter 服务器的连接将是困难的部分。 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
.如果您确实想尝试重用会话,PowerCLI 确实允许这样做,
Session
参数可通过Connect-VIServer
。 The session ID is accessible through the following Global variable: $global:DefaultVIServer.SessionId
session ID 可通过以下全局变量访问:
$global:DefaultVIServer.SessionId
Hopefully that helps.希望这会有所帮助。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.