WMI kald til server
Hej alleEr der nogen der kan fortælle mig hvorfor nedenstående fungere ved kald til bruger-pc'ere, men ikke på mine servere (kalder med en admin-konto):
-----------------------------------------------------
public string FreeSpace(string HostOrIP, string username, string password)
{
ConnectionOptions myConnectionOptions = new ConnectionOptions();
myConnectionOptions.Impersonation = ImpersonationLevel.Impersonate;
if (username != null && password != null)
{
myConnectionOptions.Username = username;
myConnectionOptions.Password = password;
}
ManagementScope objwmiservice;
ManagementObjectSearcher myObjectSearcher;
ManagementObjectCollection myCollection;
try
{
objwmiservice = new ManagementScope((@"\\" + HostOrIP + "\\root\\cimv2"), myConnectionOptions);
objwmiservice.Connect();
myObjectSearcher = new ManagementObjectSearcher(objwmiservice.Path.ToString(), "SELECT FreeSpace, Size, Name FROM Win32_LogicalDisk WHERE DriveType = 3");
myObjectSearcher.Options.Timeout = new TimeSpan(0, 0, 0, 0, 7000);
myCollection = myObjectSearcher.Get();
foreach (ManagementObject myObject in myCollection)
{
if (myObject.GetPropertyValue("FreeSpace") != null)
{
string receiver = "[CurrentUser]";
if (myConnectionOptions.Username != null)
receiver = myConnectionOptions.Username;
long Userx = Convert.ToInt64(myObject.GetPropertyValue("FreeSpace"));
return "Free space on '" + HostOrIP + "': " + (Userx / (1024 * 1024 * 1024)) + " GB."
+ " (Read by '" + receiver + "'.)";
}
}
return "Info not found.";
}
catch (UnauthorizedAccessException e)
{
return e.ToString(); //e.Message.Substring(0, e.Message.IndexOf("("));
}
catch (Exception e)
{
return "UNKNOWN ERROR:<br />" + e.ToString();
}
}
-----------------------------------------------------
Får følgende access denied fejl:
-----------------------------------------------------
System.UnauthorizedAccessException: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)) at System.Runtime.InteropServices.Marshal.ThrowExceptionForHRInternal(Int32 errorCode, IntPtr errorInfo) at System.Management.ManagementScope.InitializeGuts(Object o) at System.Management.ManagementScope.Initialize() at System.Management.ManagementObjectSearcher.Initialize() at System.Management.ManagementObjectSearcher.Get() at WMI_call.FreeSpace(String HostOrIP, String username, String password)
-----------------------------------------------------
