2

I am putting together a script which will log off VDI sessions which have been in a disconnected state for over 10 hours. I have managed to get everything together except for the last hurdle - actually forcing a logoff.

ForEach ($Desktop in $VDIlist) 
    {
        $win32OS = Get-wmiobject win32_operatingsystem -ComputerName $desktop.'DNS Name' -EnableAllPrivileges
        write-host "Shutting down host $Desktop."DNS Name""
        $win32OS.Win32Shutdown(4)
    }    

This results in the error below.

Exception calling "Win32Shutdown" : "Generic failure "
At line:1 char:1
+ $win32OS.win32shutdown(4)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : WMIMethodException

This does not appear to happen when no argument is used
($win32os.win32shutdown()), but this also does not force the log off like I require.

As far as I have read the -EnableAllPrivileges parameter should allow for the remote log off and it does work if I have a live PCoIP session to the VDI I am attempting to shutdown but not when in a disconnected state.
Could anyone point me in the right direction?

Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
  • Have you seen this [MSDN documentation](https://msdn.microsoft.com/en-us/library/aa394058(v=vs.85).aspx)? It mentiones that you need `SE_SHUTDOWN_NAME ` it also suggests to try `Win32ShutdownTracker` method on the same WMI class. So personally I would first try remote to the host using `Enter-PSSession` listed my privileges (`whoami /priv`) and then tried to invoke the same wmi command to see if that works that way. – nohwnd May 20 '15 at 14:22
  • @nohwnd Yeah that is the documentation I have been working from. I have domain admin privileges so that will not be the problem, I have just tried the `Win32ShutdownTracker` and had the same problem. I have resorted to using the forced shutdown argument instead as this appears to work and the desktop will be refreshed anyway so it is the same end result (albeit a little slower) – Charlie Sorrell May 20 '15 at 14:46
  • Have solved this with an alternative command using VMware View's inbuilt "Powercli". `get-remotesession -state "Disconnected" | Where-Object {($_.duration -match 'Day' -or $_.duration -match '\d\d hours')} | Send-SessionLogoff` – Charlie Sorrell May 26 '15 at 14:31

1 Answers1

0

Still not entirely sure why the first script is giving an error but I have instead switched to using VMWare View's built in PowerCLI snapin to produce the same result - just faster and more efficiently.

get-remotesession -state "Disconnected" | Where-Object {($_.duration -match 'Day' -or $_.duration -match '\d\d hours')} | Send-SessionLogoff

This will query the Horizon view server for any sessions with the "Disconnected state", it will then filter out any objects that have had a lifetime of less than 10 hours and log off anything that is left.

This requires VMware View PowerCLI PSSnippets to be loaded and connected to your view connection broken.