.net 使用PowerShell獲取電腦中的UUID


UUID含義是通用唯一識別碼 (Universally Unique Identifier),這 是一個軟件建構的標准,也是被開源軟件基金會 (Open Software Foundation, OSF) 的組織應用在分布式計算環境 (Distributed Computing Environment, DCE) 領域的一部分。

組成

UUID是指在一台機器上生成的數字,它保證對在同一時空中的所有機器都是唯一的。通常平台會提供生成的API。按照開放軟件基金會(OSF)制定的標准計算,用到了以太網卡地址、納秒級時間、芯片ID碼和許多可能的數字

UUID由以下幾部分的組合:

(1)當前日期和時間,UUID的第一個部分與時間有關,如果你在生成一個UUID之后,過幾秒又生成一個UUID,則第一個部分不同,其余相同。

(2)時鍾序列。

(3)全局唯一的IEEE機器識別號,如果有網卡,從網卡MAC地址獲得,沒有網卡以其他方式獲得。

UUID的唯一缺陷在於生成的結果串會比較長。關於UUID這個標准使用最普遍的是微軟的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函數很簡單地生成UUID,其格式為:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16),其中每個 x 是 0-9 或 a-f 范圍內的一個十六進制的數字。而標准的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),可以從cflib 下載CreateGUID() UDF進行轉換。

-------以上內容摘自《百度百科》

 

因為軟件產品中需要與硬件碼進行綁定,就想到了UUID,通過百度,網上搜索了一堆之后,發現大部分的代碼都是如下:

 

需要引用:System.Management;

string processor = "Win32_Processor";//類名
ManagementClass driveClass= new ManagementClass(processor);
Console.WriteLine(driveClass.GetQualifierValue("UUID"));

 

然后,讓我們部門所有同事在各自的電腦上運行了一次,發現結果如下:

image 

全部運行的結果都是相同的。(這是為什么呢??到現在我也不知道,但不甘心,繼續搜Google)

----------------------------------------------我是分隔線-----------------------------------------------

功夫不負有心人,后來查資料發現,Windows PowerShell也可以獲取UUID,雖然對於PowerShell我也不熟悉,但核心是能不能解決我的問題?

 

Windows PowerShell 是一種命令行外殼程序和腳本環境,使命令行用戶和腳本編寫者可以利用 .NET Framework 的強大功能。

它引入了許多非常有用的新概念,從而進一步擴展了您在 Windows 命令提示符和 Windows Script Host 環境中獲得的知識和創建的腳本。

 

 

首先,你必須保證操作系統上有PowerShell安裝在您的系統上,另外Vs開發工程中需要引用 System.Management.Automation.dll,
這個dll在我電腦以下路徑里:“ C:\windows\assembly\GAC_MSIL\System.Management.Automation\1.0.0.0__31bf3856ad364e35\”,
本機操作系統:Win7
 
核心的代碼如下:
private static string GetUUID()
        {

            try
            {
                string uuid = string.Empty;
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {

                    PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID");  //OK


                    Collection<PSObject> PSOutput = PowerShellInstance.Invoke();
                    foreach (PSObject outputItem in PSOutput)
                    {
                        if (outputItem != null)
                        {
                            uuid += outputItem.BaseObject.ToString();
                        }
                    }
                }
                return uuid;
            }
            catch
            {
                return string.Empty;
            }
        }
 
其調用其實就是使用PowerShell的Script進行獲取。因為在調用PowerShell時,可能會比較的慢,.net中也提供了異步調用的機制。核心代碼如下:
private static string GetAsyncUUID()
        {
            try
            {
                string uuid = string.Empty;
                using (PowerShell PowerShellInstance = PowerShell.Create())
                {

                    PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID");  //OK

                    PSDataCollection<PSObject> outputCollection = new PSDataCollection<PSObject>();
                    outputCollection.DataAdded += outputCollection_DataAdded;
                    PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;
                    IAsyncResult result = PowerShellInstance.BeginInvoke<PSObject, PSObject>(null, outputCollection);

                    while (result.IsCompleted == false)
                    {
                        Console.WriteLine("Waiting for pipeline to finish...");
                        Thread.Sleep(1000);

                        // While里面可以寫上執行等待中的一些事情
                    }

                    foreach (PSObject outputItem in outputCollection)
                    {
                        if (outputItem != null)
                        {
                            uuid += outputItem.BaseObject.ToString();
                        }
                    }

                }
                return uuid;
            }
            catch
            {
                return string.Empty;
            }

        }

 

static void Error_DataAdded(object sender, DataAddedEventArgs e)
        {
            Console.WriteLine("An error was written to the Error stream!");
        }

static void outputCollection_DataAdded(object sender, DataAddedEventArgs e)
        {
            Console.WriteLine("Object added to output.");
        }
 
以上代碼運行之后,經過測試之后,部門沒有重復的。結果如下:
image 
 
暫時,從以上測試結果分析來看,這個方法是可行的。但目前仍然有比較擔心的幾個問題:
 
             1、PowerShell在不同的版本里面,調用的方法會不會不一樣?因為做為B/s軟件需要考慮更多的Windows服務器?
                               比如: (get-wmiobject Win32_ComputerSystemProduct).UUID
 
            2、為了安全,PowerShell會不會被服務器給禁用?
 
            3、因為B/s軟件是需要IIS來運行的,會不會出現權限不足的情況??
 
希望有對Windows PowerShell比較熟悉的朋友能指教一下。
 
另:本文僅作記錄分享,臟水請勿亂吐!

===============================補充:==========================================
后期經過大量測試發現:windows Server 2003 使用PowerShell 獲取UUID,可能會報錯,所以建議還是使用如下方式:

var c = new ManagementClass("Win32_ComputerSystemProduct");
var instance = c.GetInstances();
 
foreach (var i in instance)
    Console.WriteLine(i["uuid"]);

 

 




免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM