VMware Cloud Community
WiliamMerlin
Enthusiast
Enthusiast

problems with Powercli/script

Guys, I'm not specialist in scripting, actually, I have no idea whatsoever about powershell and scripting. However, After trying out a few scripts in a test lab I felt confirtable enough to run it on my production environemnt. I've got the script below to show the users that are currently loged in my vcenter.

After running successfully the first time it stoped working! I ran it in another vcenter and it worked fine. But when I type ./script.ps1 in my vcenter 5.0 I get nothing on screen, actually it is just appears as if I had pressed enter without anything typed in. So it just jumps to another line...

I've rebooted the server, tried different scripts, installed an updated version of the powerCLI and nothing! When I tried to run another script that actually logs in the vcenter I asee the connection been made from the sessions in vcenter, however I don't get any output... does anybody know what it could be?

$svcRef = new-object VMware.Vim.ManagedObjectReference

$svcRef.Type = "ServiceInstance"

$svcRef.Value = "ServiceInstance"

$serviceInstance = get-view $svcRef

$sessMgr = get-view $serviceInstance.Content.sessionManager

foreach ($sess in $sessMgr.SessionList){if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){write "$($sess.UserName)"}

}

Tags (2)
0 Kudos
22 Replies
Troy_Clavell
Immortal
Immortal

0 Kudos
AndySimmons
Hot Shot
Hot Shot

Just confirmed this works on vCenter 5. I added a quick sanity check... change "your.vcenter.server" in line 1 to your vCenter server and give this a shot.

$VCServer = "your.vcenter.server"
$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
if ($global:defaultviserver -eq $null) { 
     Connect-VIServer $VCServer
}
foreach ($sess in $sessMgr.SessionList) {
     if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)) {
          write "$($sess.UserName)"
     }
}
-Andy VCAP5-DCA, VCP-DV 4/5, MCSE, space camp graduate.
0 Kudos
LucD
Leadership
Leadership

You can do this a bit shorter, like this

$serviceInstance = Get-View ServiceInstance 
$sessMgr
= Get-View $serviceInstance.Content.sessionManager
foreach
($sess in $sessMgr.SessionList){   if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){write "$($sess.UserName)"} }

Perhaps this one is interesting as well


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Hi Andy, I did two tests, the first one disconnecting with the command Disconnect-VIServer and the other one while loged in.

You can see in teh image attached that I ran twice to be sure and got nothing in return.

printscreen.png

0 Kudos
LucD
Leadership
Leadership

It looks as if your Connect-VIServer didn't work


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Hi Lucd, tried this one as well, same thing. nothing shown

0 Kudos
LucD
Leadership
Leadership

But where is your Connect-VIServer statement ?

That should produce some output as well


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

HereUntitled.png

0 Kudos
LucD
Leadership
Leadership

And is 127.0.0.1 a vCenter or an ESXi server ?


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Vcenter 5.0

0 Kudos
LucD
Leadership
Leadership

Are you sure that there are sessions that have been opened  more than 60 minutes ago ?
Did you try lowering the that interval in the script  ?
Would you mind attaching that script.ps1 file ?


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Hi LucD, I tried lowering down the time, and I actually got a few connections shown. (8 connections from different users) these connections were left idle over night. However... I have more than 155 idle connections between the 6 minutes to 2 hours (at this moment). Oddly enough, when I ran another script to kill the sessions that are iddle for more than  5 minutes, it killed all those 8 connections, but the other 155 connections are still there. The script that I used to see the connections is actually just the test, what I really needed is to get these 155 idle connections killed... I'm attaching the two scripts... I renamed the script.ps1 to show and I attached the one that I use to kill sessions.

0 Kudos
LucD
Leadership
Leadership

Your scripts are working fine for me.

Do you have more than 1 vCenter ? And do you use them in linked mode ?

In that case you will see the sessions from all the vCenters in your vSphere client


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

0 Kudos
LucD
Leadership
Leadership

If you have linked vCenters and your are configured for multiple mode, you can adapt your script as follows

Connect-VIServer 127.00.0.1 -AllLinked
Get-View ServiceInstance | %{
  Get-View $_.Content.sessionManager | %{
    foreach ($sess in $_.SessionList){
      $($sess.UserName)
    }
  }
}

This will list all sessions.


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

WiliamMerlin
Enthusiast
Enthusiast

Hi LucD, it worked fine in my other vcenter as well, I have a 4.1 in another location. I did not linked them together because of the discrepancy in the versions...

I'm attaching the Sessions tab, Sorry for covering the username area, security policy.

As you can see I have idle sessions for like 58 minutes that were not killed by the scriot

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

I've removed the connection with the connection to vcenter part and I got it listing all the sessions as it should... so the problem seems to be (($sess.LastActiveTime).addminutes(25 part of my script... just guessing

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Hi LucD! I figured out the problem!!! I changed the ($sess.UserName)  of your last script to $($sess.LastActiveTime) so I could see which time it was based on to get the idle connections. so it happens that the bloody thing is actualluy showing the time in the future! I confirmed with my other vcenter, which actually shows the dates correctly... so now I just need to figure it out  how to solve this date missmatch...

idle.png

0 Kudos
LucD
Leadership
Leadership

That could be the TimeZone defined on the server where you run vCenter


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

0 Kudos
WiliamMerlin
Enthusiast
Enthusiast

Time zone -5, it is correct, checked the time in the database server, and in the domain controllers, I will keep looking, once I find it I will post it here.

0 Kudos