VMware Cloud Community
SitrucHtims
Contributor
Contributor
Jump to solution

Getting connect-viserver to stay persistent through different functions.

For some reason, I cannot get my Virtual Center connection to stay persistent so I can use in different function when the connection is established in a function. If I perform other tasking the in the same function as the "Connect-VIServer" then they work with no problems. I know I've got to be missing something. Below are two basic code example of what I am trying to do.

Code

function connectVC

{

Connect-VIServer $vcserver

}

function showdatacenters

{

Get-Datacenter

}

Add-PSsnapin VMware.VimAutomation.Core

Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"

connectVC

showdatacenters

Error

5/7/2009 3:51:22 PM Get-Datacenter You are not currently connected to any servers. Please connect first using Connect-VIServer or one of its aliases.

At :line:7 char:15

+ Get-Datacenter <<<<

Code

function connectVC

{

$objVC = Connect-VIServer $vcserver

}

function showdatacenters

{

$objVC \

Get-Datacenter

}

Add-PSsnapin VMware.VimAutomation.Core

Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"

connectVC

showdatacenters

Error

The argument cannot be null or empty.

At :line:7 char:24

+ $objVC \

Get-Datacenter <<<<

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

The reason you are seeing is is due to the "scope" in which you connect to the VC server.

While in a function (connectVC in your first example) the local variables you create there are lost when you exit the function.

There are 2 solutions

1) Connect to the VC server outside a function, the so-called script scope


function showdatacenters
{
Get-Datacenter
}

Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"
Connect-VIServer $vcserver
showdatacenters

2) Store the ViServerImpl object (that is returned by the Connect-VIServer cmdlet) outside the function and pass it on all the other VITK cmdlets.


function connectVC
{
$global:vc = Connect-VIServer $vcserver
}
function showdatacenters
{
Get-Datacenter -Server $global:vc
}

Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"
connectVC
showdatacenters

If you want to learn more on the scope modifiers (like the global I used in 2)) have a look at this.


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

View solution in original post

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership
Jump to solution

The reason you are seeing is is due to the "scope" in which you connect to the VC server.

While in a function (connectVC in your first example) the local variables you create there are lost when you exit the function.

There are 2 solutions

1) Connect to the VC server outside a function, the so-called script scope


function showdatacenters
{
Get-Datacenter
}

Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"
Connect-VIServer $vcserver
showdatacenters

2) Store the ViServerImpl object (that is returned by the Connect-VIServer cmdlet) outside the function and pass it on all the other VITK cmdlets.


function connectVC
{
$global:vc = Connect-VIServer $vcserver
}
function showdatacenters
{
Get-Datacenter -Server $global:vc
}

Add-PSsnapin VMware.VimAutomation.Core
Initialize-VIToolkitEnvironment.ps1

$vcserver = read-host "Target VirtualCenter Server"
connectVC
showdatacenters

If you want to learn more on the scope modifiers (like the global I used in 2)) have a look at this.


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

Reply
0 Kudos