VMware Cloud Community
mythrandir52
Contributor
Contributor

disconnect-VIServer Issue

I have a script that reports on all of the powered on vm's in each of my virtual center servers.  It connects to each virtual center servers walks the floder structure and reports on the vm's in each folder.  I am running the script in powergui.  The problem i am having is it doesnt seem to disconnect unless i add the -force switch to the disconnect.  The problem comes that once it disconnects with the -force it can not connect again unless i stop and restart the powershell session.  by ot disconnecting it is have to go through the list on vm's for the first vc server twice.  I have temporarly add a unique filter to the report to dump the duplicates but it doubles the processing time.

code:

cls

# **************************************************************************************

# Load Libraries

# **************************************************************************************

if(-not (Get-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue)) { Add-PSSnapin VMware.VimAutomation.Core }

# **************************************************************************************

# Functions

# **************************************************************************************

$locations= @{"S1"="Site1";"S2"="Site2"; "S3"="Site3"; "S4"="Site4"; "S5"="Site5"}

$vcenters= @("vcenter01","vcenter02")

$rpt= @()

Foreach

($vc in $vcenters) {

Connect-VIServer -Server $vc #| Out-Null

foreach ($site in (Get-Folder datacenters | Get-Folder -NoRecursion | Sort-Object $_.name)) {

Foreach ($VM in ($site | Get-VM | Where-Object {$_.powerstate -eq "PoweredOn"} | Sort-Object $_.name)){

$tmp = "" | select Asset_Name, Model, Serial_Number, Asset_Tag, Description, Manufacturer, Asset_Type, Class, location, In_DMZ, Portfolio_Status, Primary_Contact_empid, Primary_Contact_LANID, Primary_Support_Contact_LANID, Record_Status_Inactive, Usage_Status, Deployment_Status, Service_Status, Received_Date, Warranty_Type, Warranty_Start_Date, Warranty_End_Date, Server_Type, Floor_Location, Room_Location, Cabinet_Location, Shelf_Location, Slot_Location, Operating_System, Product_Version, Maintenance_Org_Code, Service_Type;

Write-Host $site.name $locations.get_item($site.name) $VM.Name

$tmp.Asset_Name = $VM.Name

$tmp.Serial_Number = $VM.Name

$tmp.Asset_Type = "Hardware.Server"

$tmp.Model = "Virtual"

$tmp.Class = "Virtual"

$tmp.Portfolio_Status = "Active"

$tmp.Operating_System = (get-view $vm.id).guest.guestfullname

$tmp.location = $locations.get_item($site.name)

$tmp.Maintenance_Org_Code = "CCG872"

$rpt += $tmp

}

}

Disconnect-VIServer -Confirm:$false -server $vc

}

$rpt | Select-Object * -Unique | Sort-Object Asset_Name | Export-Csv -NoTypeInformation VirtualServerList.csv

Truth is a three edged sword
0 Kudos
1 Reply
LucD
Leadership
Leadership

Does the same happen when you change the connect and disconnect lines as follows ?

$svConnected = Connect-VIServer -Server $vc
#
#
#
Disconnect-VIServer -Server $svConnected -Confirm:$false


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

0 Kudos