wmi call on win2k3 server : DOTNET
This is a discussion on wmi call on win2k3 server within the DOTNET forums in Framework and Interface Programming category; I have a c# application which, amongst other things, requires the shutdown/Reboot of the computer on which it runs. I have found the very stadnard code for this which uses a wmi call from the System.Management .net library. This works perfectly on Windows 2000 machines but when I try to run it on Windows Server 2003 it returns an Exception from the Management object of 'Privilege not held'. I have checked all the Local Security Policy settings for the logged-on user and they all seem ok (e.g. Shutdown m/c privilege etc...). I have scoured long and hard around newsgroups to ...
| DOTNET Discussion forums related to Microsoft Dot net technologies, CSharp and other related items |
![]() |
| | LinkBack | Thread Tools |
|
#1
| |||
| |||
| shutdown/Reboot of the computer on which it runs. I have found the very stadnard code for this which uses a wmi call from the System.Management .net library. This works perfectly on Windows 2000 machines but when I try to run it on Windows Server 2003 it returns an Exception from the Management object of 'Privilege not held'. I have checked all the Local Security Policy settings for the logged-on user and they all seem ok (e.g. Shutdown m/c privilege etc...). I have scoured long and hard around newsgroups to no evail. I am now totally stumped! btw, all machines are .net Framework 1.1 (I thought maybe it was a Framework v1 bug but alas no fix when they were all set to run v1.1) Any advice hugely appreciated. |
|
#2
| |||
| |||
| Dan, Not sure if this applies to you but, there's a problem using System.Management on .NET v1.1 SP1. Seems like required privileges are not set when requesting privileged WMI actions. Willy. "dan m" <bbposts@excelnet.co.uk> wrote in message news:29db3f3d.0409140222.2f6cfc73@posting.google.c om... >I have a c# application which, amongst other things, requires the > shutdown/Reboot of the computer on which it runs. > > I have found the very stadnard code for this which uses a wmi call > from the System.Management .net library. > > This works perfectly on Windows 2000 machines but when I try to run it > on Windows Server 2003 it returns an Exception from the Management > object of 'Privilege not held'. I have checked all the Local Security > Policy settings for the logged-on user and they all seem ok (e.g. > Shutdown m/c privilege etc...). I have scoured long and hard around > newsgroups to no evail. I am now totally stumped! > > btw, all machines are .net Framework 1.1 (I thought maybe it was a > Framework v1 bug but alas no fix when they were all set to run v1.1) > > Any advice hugely appreciated. |
|
#3
| |||
| |||
| Dan, System.Management v1.1 SP1 fails to enable privileges required to execute methods on WMI classes. To solve this (temporarily), I wrote a simple class [1], you should call TokenAdjuster.SetPrivilege just before (or directly after) you create an instance of the ManagementObject for the "Win32_OperatingSystem" class. Usage... ManagementObject mo = null; if(TokenAdjuster.SetPrivilege("SeShutdownPrivilege ", true)) // Privilege to be enabled (here SeShutdownPrivilege) { mo = new ManagementObject(path); //use mo and call shutdown } else // SetPrivilege failed.... [1] using System; using System.Runtime.InteropServices; using System.Security; using System.Diagnostics; public class TokenAdjuster { // PInvoke stuff required to set/enable security privileges [DllImport("advapi32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute] static extern int OpenProcessToken( System.IntPtr ProcessHandle, // handle to process int DesiredAccess, // desired access to process ref IntPtr TokenHandle // handle to open access token ); [DllImport("kernel32", SetLastError=true), SuppressUnmanagedCodeSecurityAttribute] static extern bool CloseHandle(IntPtr handle); [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true), SuppressUnmanagedCodeSecurityAttribute] static extern int AdjustTokenPrivileges( IntPtr TokenHandle, int DisableAllPrivileges, IntPtr NewState, int BufferLength, IntPtr PreviousState, ref int ReturnLength); [DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true), SuppressUnmanagedCodeSecurityAttribute] static extern bool LookupPrivilegeValue( string lpSystemName, string lpName, ref LUID lpLuid); [StructLayout(LayoutKind.Sequential)] internal struct LUID { internal int LowPart; internal int HighPart; } [StructLayout(LayoutKind.Sequential)] struct LUID_AND_ATTRIBUTES { LUID Luid; int Attributes; } [StructLayout(LayoutKind.Sequential)] struct _PRIVILEGE_SET { int PrivilegeCount; int Control; [MarshalAs(UnmanagedType.ByValArray, SizeConst=1)] // ANYSIZE_ARRAY = 1 LUID_AND_ATTRIBUTES [] Privileges; } [StructLayout(LayoutKind.Sequential)] internal struct TOKEN_PRIVILEGES { internal int PrivilegeCount; [MarshalAs(UnmanagedType.ByValArray, SizeConst=3)] internal int[] Privileges; } const int SE_PRIVILEGE_ENABLED = 0x00000002; const int TOKEN_ADJUST_PRIVILEGES = 0X00000020; const int TOKEN_QUERY = 0X00000008; const int TOKEN_ALL_ACCESS = 0X001f01ff; const int PROCESS_QUERY_INFORMATION = 0X00000400; public static bool SetPrivilege (string lpszPrivilege, bool bEnablePrivilege ) { bool retval = false; int ltkpOld = 0; IntPtr hToken = IntPtr.Zero; TOKEN_PRIVILEGES tkp = new TOKEN_PRIVILEGES(); tkp.Privileges = new int[3]; TOKEN_PRIVILEGES tkpOld = new TOKEN_PRIVILEGES(); tkpOld.Privileges = new int[3]; LUID tLUID = new LUID(); tkp.PrivilegeCount = 1; if (bEnablePrivilege) tkp.Privileges[2] = SE_PRIVILEGE_ENABLED; else tkp.Privileges[2] = 0; if(LookupPrivilegeValue(null , lpszPrivilege , ref tLUID)) { Process proc = Process.GetCurrentProcess(); if(proc.Handle != IntPtr.Zero) { if (OpenProcessToken(proc.Handle, TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, ref hToken) != 0) { tkp.PrivilegeCount = 1; tkp.Privileges[2] = SE_PRIVILEGE_ENABLED; tkp.Privileges[1] = tLUID.HighPart; tkp.Privileges[0] = tLUID.LowPart; const int bufLength = 256; IntPtr tu = Marshal.AllocHGlobal( bufLength ); Marshal.StructureToPtr(tkp, tu, true); if(AdjustTokenPrivileges(hToken, 0, tu, bufLength, IntPtr.Zero, ref ltkpOld) != 0) { // successful AdjustTokenPrivileges doesn't mean privilege could be changed if (Marshal.GetLastWin32Error() == 0) { retval = true; // Token changed } } TOKEN_PRIVILEGES tokp = (TOKEN_PRIVILEGES) Marshal.PtrToStructure(tu, typeof(TOKEN_PRIVILEGES) ); Marshal.FreeHGlobal( tu ); } } } if (hToken != IntPtr.Zero) { CloseHandle(hToken); } return retval; } // End class Cheers, Willy. "dan m" <bbposts@excelnet.co.uk> wrote in message news:29db3f3d.0409140222.2f6cfc73@posting.google.c om... >I have a c# application which, amongst other things, requires the > shutdown/Reboot of the computer on which it runs. > > I have found the very stadnard code for this which uses a wmi call > from the System.Management .net library. > > This works perfectly on Windows 2000 machines but when I try to run it > on Windows Server 2003 it returns an Exception from the Management > object of 'Privilege not held'. I have checked all the Local Security > Policy settings for the logged-on user and they all seem ok (e.g. > Shutdown m/c privilege etc...). I have scoured long and hard around > newsgroups to no evail. I am now totally stumped! > > btw, all machines are .net Framework 1.1 (I thought maybe it was a > Framework v1 bug but alas no fix when they were all set to run v1.1) > > Any advice hugely appreciated. |
![]() |
« Previous Thread
|
Next Thread »
| Thread Tools | |
| |
| ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Why am I getting System.DllNotFoundException err on win2K3 server? | usenet | CSharp | 7 | 08-22-2007 03:40 PM |
| Stop sending mails from Win2K3 Server | usenet | Inetserver | 0 | 07-28-2006 04:22 AM |
| wmi call on win2k3 server | usenet | DOTNET | 1 | 09-14-2004 01:09 PM |
| exc2k on win2k3 server | usenet | Microsoft Exchange | 2 | 06-14-2004 06:07 AM |
| Problem with transactions from Win2k app server to Win2k3 SQL Server | usenet | DOTNET | 7 | 12-10-2003 03:19 PM |
All times are GMT -5. The time now is 05:21 PM.




