public static void EnablePrivilege()

in src/PatchOrchestrationApplication/NodeAgentNTService/src/Utility/ShutdownUtility.cs [20:91]


        public static void EnablePrivilege()
        {                      
            try
            {
                var locallyUniqueIdentifier = new LUID();

                if (LookupPrivilegeValue(null, "SeShutdownPrivilege", ref locallyUniqueIdentifier))
                {
                    var TOKEN_PRIVILEGES = new TOKEN_PRIVILEGES();
                    TOKEN_PRIVILEGES.PrivilegeCount = 1;
                    TOKEN_PRIVILEGES.Attributes = SE_PRIVILEGE_ENABLED;
                    TOKEN_PRIVILEGES.Luid = locallyUniqueIdentifier;

                    var tokenHandle = IntPtr.Zero;
                    try
                    {
                        var currentProcess = GetCurrentProcess();
                        if (OpenProcessToken(currentProcess,
                            TOKEN_ADJUST_PRIVILEGES | TOKEN_QUERY, out tokenHandle))
                        {
                            if (AdjustTokenPrivileges(tokenHandle, false,
                                ref TOKEN_PRIVILEGES,
                                1024, IntPtr.Zero, IntPtr.Zero))
                            {
                                var lastError = Marshal.GetLastWin32Error();
                                if (lastError == ERROR_NOT_ALL_ASSIGNED)
                                {
                                    var win32Exception = new Win32Exception();
                                    throw new InvalidOperationException("AdjustTokenPrivileges failed.", win32Exception);
                                }
                            }
                            else
                            {
                                var win32Exception = new Win32Exception();
                                throw new InvalidOperationException("AdjustTokenPrivileges failed.", win32Exception);
                            }
                        }
                        else
                        {
                            var win32Exception = new Win32Exception();

                            var exceptionMessage = string.Format(CultureInfo.InvariantCulture,
                                "OpenProcessToken failed. CurrentProcess: {0}",
                                currentProcess.ToInt32());

                            throw new InvalidOperationException(exceptionMessage, win32Exception);
                        }
                    }
                    finally
                    {
                        if (tokenHandle != IntPtr.Zero)
                            CloseHandle(tokenHandle);
                    }
                }
                else
                {
                    var win32Exception = new Win32Exception();

                    var exceptionMessage = string.Format(CultureInfo.InvariantCulture,
                        "LookupPrivilegeValue failed. SecurityEntityValue: SeShutdownPrivilege");

                    throw new InvalidOperationException(exceptionMessage, win32Exception);
                }
            }
            catch (Exception e)
            {
                var exceptionMessage = string.Format(CultureInfo.InvariantCulture,
                    "GrandPrivilege failed. SE_SHUTDOWN_NAME ");

                throw new InvalidOperationException(exceptionMessage, e);
            }
        }