Win32Shutdown Generic Failure

963 Views Asked by At

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?

1

There are 1 best solutions below

0
On

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.