VMware Cloud Community
MCioe
Enthusiast
Enthusiast
Jump to solution

$global:defaultVIServer variable gets overwritten with datastores in PowerCLI 6.0 Release 2

I recently noticed some very odd behavior with the $global:defaultVIServer(s) variables.

The content was changing in the middle of my script; in particular $global:defaultVIServer.Name changed from

     10.10.10.10  to 10.10.10.10@443

For some reason a port number was getting pasted to the end of the name field, which failed in the Test-Connection (ping) cmdlet.

After some digging, I tracked it down to my use of the VMware global variable,  vmstores

The line, below overwrites $global:defaultVIServer

     $dsList = (dir -path vmstores:)

Code snippet

connect-VIServer $vctIP

$global:defaultVIServer | fl

$dsList = (dir -path vmstores: )

$global:defaultVIServer | fl

$dsList | fl


You can see that $global:defaultVIServer gets overwritten with the $dsList data after the dir command

Screen shots are attached. 


Minimally, I have to disconnect from my servers and reconnect to fix this.


Is there a better way to get the list of datastores (and their paths) with PowerCLI 6.0?

Maureen

Reply
0 Kudos
1 Solution

Accepted Solutions
MCioe
Enthusiast
Enthusiast
Jump to solution

I only call that command in a single place in my scripts, so I tested for an "@" char in the $global:DefaultViServer variable after that call and if I find it then I disconnect and reconnect to my servers.  This fixes the global variable, which I use in other places in my scripts.

I didn't want to change my scripts to have to use the other variable, $global:DefaultVIServer.ServiceUri.Host,

because that would have been a little more difficult.

Also, when the bug fix is implemented, I won't have to change my scripts.

Snippet:

foreach ($server in $global:DefaultViServers)

{

   # check if @ char is in the name; i.e. @port_number

    if ($server.Name -match '@')

    {

        # split the name into IP address and port number

        $serverNameList = $server.Name.split('@')

        Disconnect-VIServer $serverNameList[0]

        sleep -seconds 1

        Connect-VIServer $serverNameList[0]

    }

}

View solution in original post

Reply
0 Kudos
4 Replies
alanrenouf
VMware Employee
VMware Employee
Jump to solution

We are looking into why this happens, thanks for logging it.

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
ccalvetTCC
Enthusiast
Enthusiast
Jump to solution

Is there a better way to get the list of datastores (and their paths) with PowerCLI 6.0?


Are you looking for something like this?


function Get-ChildEntityRecursion{

    param(

    $ChildEntity,

    $ParentPath

    )

        process{

            $ChildEntity | foreach-object{

            $ChildChildEntity = get-view ($_)

                If ($ChildChildEntity.ChildEntity){

                $FullPath = $ParentPath +"\"+ $($ChildChildEntity.Name)

                Get-ChildEntityRecursion $ChildChildEntity.ChildEntity -ParentPath $FullPath

                }

                Else{

                $FullPath = $ParentPath +"\"+ $($ChildChildEntity.Name)

                $FullPath

                }

            }

        }

}

$ServiceInstance = get-view serviceinstance

$RootFolder = get-view($ServiceInstance.content.rootfolder)

$RootFolder.ChildEntity | foreach-object{

    $DatacenterFolder = get-view ($_)

    $TopFolder = get-view($DatacenterFolder.DatastoreFolder)

    $FullPath = $($global:defaultVIServer.name) + "\" +  $($DatacenterFolder.Name)

    Get-ChildEntityRecursion -ChildEntity $TopFolder.ChildEntity -ParentPath $FullPath

}

Note:
The script works only with one connection to a VC.
I didn't test it so much, but it works in my lab.

It doesn't rely on "vmstores:", so if the information is in vCenter it is possible to extract it even if a datastore is unavailable.

Bonus:

It is possible to replace the line:

$TopFolder = get-view($DatacenterFolder.DatastoreFolder) #To get everything in the "Datastores and Datastore Clusters" view

by

$TopFolder = get-view($DatacenterFolder.HostFolder)  #To get everything in the "Hosts and Clusters" view

$TopFolder = get-view($DatacenterFolder.VMFolder) #To get everything in the "VMs and Template" view

For the "Networking" view the script will have to be modified to take into account properly dvSwith.
It doesn't work well so far.

$TopFolder = get-view($DatacenterFolder.NetworkFolder)

Blog: http://thecrazyconsultant.com/ | Twitter: @ccalvetTCC
Reply
0 Kudos
alanrenouf
VMware Employee
VMware Employee
Jump to solution

In the meantime as a workaround provided from the engineering team is that you can use the $global:DefaultVIServer.ServiceUri.Host variable. It contains the same value and it does not change after using the vmstores: provider.

Blog: http://virtu-al.net Twitter: http://twitter.com/alanrenouf Co-author of the PowerCLI Book: http://powerclibook.com
Reply
0 Kudos
MCioe
Enthusiast
Enthusiast
Jump to solution

I only call that command in a single place in my scripts, so I tested for an "@" char in the $global:DefaultViServer variable after that call and if I find it then I disconnect and reconnect to my servers.  This fixes the global variable, which I use in other places in my scripts.

I didn't want to change my scripts to have to use the other variable, $global:DefaultVIServer.ServiceUri.Host,

because that would have been a little more difficult.

Also, when the bug fix is implemented, I won't have to change my scripts.

Snippet:

foreach ($server in $global:DefaultViServers)

{

   # check if @ char is in the name; i.e. @port_number

    if ($server.Name -match '@')

    {

        # split the name into IP address and port number

        $serverNameList = $server.Name.split('@')

        Disconnect-VIServer $serverNameList[0]

        sleep -seconds 1

        Connect-VIServer $serverNameList[0]

    }

}

Reply
0 Kudos