[英]How to expand environment variables remotely with .NET?
我需要一種在遠程計算機上擴展環境變量的方法。
假設我有一個文件夾%appdata%\\MyApp\\Plugins
或%ProgramFiles%\\MyCompany\\MyApp\\Plugins
的路徑,並且我想列出該文件夾中的文件以進行審核。 唯一的問題是我想在遠程計算機上執行此操作,但是我具有管理員訪問權限。
一個額外的問題(但不是必需的)是如何為遠程計算機上的給定用戶執行此操作?
您將使用GetFolderPath 。 您可以使用許多不同的SpecialFolder值,包括ProgramFiles
和ApplicationData
string path = Environment.GetFolderPath(Environment.SpecialFolder.ProgramFiles);
然后,您可以將其與其余路徑結合起來
string full_path = Path.Combine(path, "\MyApp\Plugins");
在遠程計算機上,您可以嘗試執行以下操作
ConnectionOptions co = new ConnectionOptions();
// user with sufficient privileges to connect to the cimv2 namespace
co.Username = "administrator";
// his password
co.Password = "adminPwd";
ManagementScope scope = new ManagementScope(@"\\BOBSMachine\root\cimv2", co);
SelectQuery query = new SelectQuery("Select windowsdirectory from Win32_OperatingSystem");
ManagementObjectSearcher searcher = new ManagementObjectSearcher(scope, query);
foreach (ManagementObject windir in searcher.Get())
Console.WriteLine("Value = {0}", windir["windowsdirectory"]);
或從此處獲取所有遠程環境變量及其值的列表
public 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);
}
}
OP編輯:還可以從注冊表中找到%AppData%
(可以遠程完成),位於HKCU\\Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\Shell Folders
和程序文件中,位於HKLM\\Software\\Microsoft\\Windows\\CurrentVersion
ProgramfilesDir
下。
這個問題沒有道理。 環境變量不是每台機器的變量。 例如,您可以期望%appdata%
指向C:\\users\\
目錄,但是確切的位置顯然取決於用戶。 以管理員身份登錄仍然無法幫助您; 那只會告訴您管理員的%appdata%
在哪里。
環境變量是“計算機范圍”設置和“每用戶設置”的合並。 一個正在運行的進程可能會修改其環境,並且當它產生另一個進程時,該進程會繼承創建它的進程的環境。
除非您有權訪問在遠程計算機上運行的進程(或可以啟動該進程),否則就沒有“環境”之類的東西:它的上下文根本不存在。 特定進程的環境取決於以下所有功能:
也就是說,Windows將其環境變量設置保留在注冊表中:
HKEY_CURRENT_USER\\Environment
HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Control\\Session Manager\\Environment
如果您具有對遠程計算機注冊表的適當訪問權限,則應該可以找到所需的內容。
請注意,可以根據其他環境變量來定義環境變量:我相信您可能會自己照顧適當的擴展。
據我所知,解決%ProgramFiles%的唯一方法是通過注冊表,因為Win32_Environment中沒有公開此方法(盡管文檔另有說明)。 所以這很好用:
$key = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey([Microsoft.Win32.RegistryHive]::LocalMachine,$serverName);
$versionKey = $key.OpenSubKey('SOFTWARE\Microsoft\Windows\CurrentVersion');
$versionKey.GetValue('ProgramFilesDir')
但是,我似乎無法使用這種方法來取回Program Files(x86)文件夾-使用注冊表API,我在注冊表中看到的鍵不會“顯示”。 奇怪。
當然,如果您在遠程計算機上運行Powershell Remoting,我想這會很容易...
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.