使用PowerShell .Net擷取電腦中的UUID_實用技巧

來源:互聯網
上載者:User

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")); 

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

全部啟動並執行結果都是相同的。(這是為什麼呢??到現在我也不知道,但不甘心,繼續搜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"); //OKCollection<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"); //OKPSDataCollection<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.");}

以上代碼運行之後,經過測試之後,部門沒有重複的。

結果如下:

 

暫時,從以上測試結果分析來看,這個方法是可行的。但目前仍然有比較擔心的幾個問題:

1、PowerShell在不同的版本裡面,調用的方法會不會不一樣?因為做為B/s軟體需要考慮更多的Windows伺服器? 比如: (get-wmiobject Win32_ComputerSystemProduct).UUID

2、為了安全,PowerShell會不會被伺服器給禁用?

3、因為B/s軟體是需要IIS來啟動並執行,會不會出現許可權不足的情況??

以上所述是小編給大家介紹的使用PowerShell .Net擷取電腦中的UUID的相關知識,希望對大家有所協助,如果大家有任何疑問請給我留言,小編會及時回複大家的。在此也非常感謝大家對雲棲社區網站的支援!

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.