PC Review


Reply
Thread Tools Rate Thread

Get environment from remote machine

 
 
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
Guest
Posts: n/a
 
      21st Feb 2005
Hi;

Is there any way to get the value of an environment variable from a remote
system - in the context of a given user?

--
thanks - dave
 
Reply With Quote
 
 
 
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      22nd Feb 2005
Hi Dave,

Welcome to MSDN newsgroup. As for the retreveing sys environment
variables , I think the only means is using WMI, and in .NET the WMI apis
are under the
System.Management namespace, here are some good articles on WMI in .net:

http://www.c-sharpcorner.com/Code/20...t/WMIPart1.asp
http://www.c-sharpcorner.com/Code/20...t/WMIPart2.asp

#Remoting and Connection Options
http://msdn.microsoft.com/library/en...tingandconnect
ionoptions.asp?frame=true

Also, I've paste a piece of test code below for your reference, if you
still need any further info on WMI, I suggest that you also have a look in
the WMI newsgroup, you can find many resources or former threads discussing
such problem there:

==========================
static void GetSysInfo(string domain, string machine, string username,
string password)
{

ManagementObjectSearcher query = null;
ManagementObjectCollection queryCollection = null;

ConnectionOptions opt = new ConnectionOptions();

opt.Impersonation = ImpersonationLevel.Impersonate;
opt.EnablePrivileges = true;
opt.Username = username;
opt.Password = password;
try
{
ManagementPath p = new ManagementPath
("\\\\" +machine+ "\\root\\cimv2");

ManagementScope msc = new ManagementScope(p, opt);

SelectQuery q= new SelectQuery("Win32_Environment");

query = new ManagementObjectSearcher(msc, q, null);
queryCollection = query.Get();

Console.WriteLine(queryCollection.Count);

foreach (ManagementBaseObject envVar in queryCollection)
{
Console.WriteLine("System environment variable {0} = {1}",
envVar["Name"], envVar["VariableValue"]);
}
}
catch(ManagementException e)
{
Console.WriteLine(e.Message);

Environment.Exit(1);
}
catch(System.UnauthorizedAccessException e)
{
Console.WriteLine(e.Message);

Environment.Exit(1);
}



}

}

========================================

HTH. Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)




 
Reply With Quote
 
=?Utf-8?B?RGF2aWQgVGhpZWxlbg==?=
Guest
Posts: n/a
 
      22nd Feb 2005
This is great - thanks - dave

"Steven Cheng[MSFT]" wrote:

> Hi Dave,
>
> Welcome to MSDN newsgroup. As for the retreveing sys environment
> variables , I think the only means is using WMI, and in .NET the WMI apis
> are under the
> System.Management namespace, here are some good articles on WMI in .net:
>
> http://www.c-sharpcorner.com/Code/20...t/WMIPart1.asp
> http://www.c-sharpcorner.com/Code/20...t/WMIPart2.asp
>
> #Remoting and Connection Options
> http://msdn.microsoft.com/library/en...tingandconnect
> ionoptions.asp?frame=true
>
> Also, I've paste a piece of test code below for your reference, if you
> still need any further info on WMI, I suggest that you also have a look in
> the WMI newsgroup, you can find many resources or former threads discussing
> such problem there:
>
> ==========================
> static void GetSysInfo(string domain, string machine, string username,
> string password)
> {
>
> ManagementObjectSearcher query = null;
> ManagementObjectCollection queryCollection = null;
>
> ConnectionOptions opt = new ConnectionOptions();
>
> opt.Impersonation = ImpersonationLevel.Impersonate;
> opt.EnablePrivileges = true;
> opt.Username = username;
> opt.Password = password;
> try
> {
> ManagementPath p = new ManagementPath
> ("\\\\" +machine+ "\\root\\cimv2");
>
> ManagementScope msc = new ManagementScope(p, opt);
>
> SelectQuery q= new SelectQuery("Win32_Environment");
>
> query = new ManagementObjectSearcher(msc, q, null);
> queryCollection = query.Get();
>
> Console.WriteLine(queryCollection.Count);
>
> foreach (ManagementBaseObject envVar in queryCollection)
> {
> Console.WriteLine("System environment variable {0} = {1}",
> envVar["Name"], envVar["VariableValue"]);
> }
> }
> catch(ManagementException e)
> {
> Console.WriteLine(e.Message);
>
> Environment.Exit(1);
> }
> catch(System.UnauthorizedAccessException e)
> {
> Console.WriteLine(e.Message);
>
> Environment.Exit(1);
> }
>
>
>
> }
>
> }
>
> ========================================
>
> HTH. Thanks,
>
> Steven Cheng
> Microsoft Online Support
>
> Get Secure! www.microsoft.com/security
> (This posting is provided "AS IS", with no warranties, and confers no
> rights.)
>
>
>
>
>

 
Reply With Quote
 
Steven Cheng[MSFT]
Guest
Posts: n/a
 
      22nd Feb 2005
You're welcome. :-)

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to debug sql 2005 on remote machine. I am administrator on both my dev machine and remote sql server machine. DR Microsoft C# .NET 1 3rd Nov 2007 11:23 AM
How to debug sql 2005 on remote machine. I am administrator on both my dev machine and remote sql server machine. DR Microsoft Dot NET Framework 0 2nd Nov 2007 10:05 PM
How to debug sql 2005 on remote machine. I am administrator on both my dev machine and remote sql server machine. DR Microsoft ADO .NET 0 2nd Nov 2007 10:05 PM
How to debug sql 2005 on remote machine. I am administrator on both my dev machine and remote sql server machine. DR Microsoft VB .NET 0 2nd Nov 2007 10:05 PM
How to debug sql 2005 on remote machine. I am administrator on both my dev machine and remote sql server machine. DR Microsoft Dot NET 0 2nd Nov 2007 10:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:46 PM.