主頁 > 知識(shí)庫(kù) > 使用PowerShell .Net獲取電腦中的UUID

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

熱門標(biāo)簽:硅谷的囚徒呼叫中心 蘋果 智能手機(jī) 地方門戶網(wǎng)站 解決方案 呼叫中心 電子圍欄 服務(wù)器配置

UUID含義是通用唯一識(shí)別碼 (Universally Unique Identifier),這 是一個(gè)軟件建構(gòu)的標(biāo)準(zhǔn),也是被開源軟件基金會(huì) (Open Software Foundation, OSF) 的組織應(yīng)用在分布式計(jì)算環(huán)境 (Distributed Computing Environment, DCE) 領(lǐng)域的一部分。

組成

UUID是指在一臺(tái)機(jī)器上生成的數(shù)字,它保證對(duì)在同一時(shí)空中的所有機(jī)器都是唯一的。通常平臺(tái)會(huì)提供生成的API。按照開放軟件基金會(huì)(OSF)制定的標(biāo)準(zhǔn)計(jì)算,用到了以太網(wǎng)卡地址、納秒級(jí)時(shí)間、芯片ID碼和許多可能的數(shù)字

UUID由以下幾部分的組合:

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

(2)時(shí)鐘序列。

(3)全局唯一的IEEE機(jī)器識(shí)別號(hào),如果有網(wǎng)卡,從網(wǎng)卡MAC地址獲得,沒有網(wǎng)卡以其他方式獲得。

UUID的唯一缺陷在于生成的結(jié)果串會(huì)比較長(zhǎng)。關(guān)于UUID這個(gè)標(biāo)準(zhǔn)使用最普遍的是微軟的GUID(Globals Unique Identifiers)。在ColdFusion中可以用CreateUUID()函數(shù)很簡(jiǎn)單地生成UUID,其格式為:xxxxxxxx-xxxx- xxxx-xxxxxxxxxxxxxxxx(8-4-4-16),其中每個(gè) x 是 0-9 或 a-f 范圍內(nèi)的一個(gè)十六進(jìn)制的數(shù)字。而標(biāo)準(zhǔn)的UUID格式為:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12),可以從cflib 下載CreateGUID() UDF進(jìn)行轉(zhuǎn)換。

-------以上內(nèi)容摘自《百度百科》

因?yàn)檐浖a(chǎn)品中需要與硬件碼進(jìn)行綁定,就想到了UUID,通過百度,網(wǎng)上搜索了一堆之后,發(fā)現(xiàn)大部分的代碼都是如下:

需要引用:System.Management;

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

然后,讓我們部門所有同事在各自的電腦上運(yùn)行了一次,發(fā)現(xiàn)結(jié)果如下:

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

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

功夫不負(fù)有心人,后來查資料發(fā)現(xiàn),Windows PowerShell也可以獲取UUID,雖然對(duì)于PowerShell我也不熟悉,但核心是能不能解決我的問題?

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

它引入了許多非常有用的新概念,從而進(jìn)一步擴(kuò)展了您在 Windows 命令提示符和 Windows Script Host 環(huán)境中獲得的知識(shí)和創(chuàng)建的腳本。

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

private static string GetUUID()
{
try
{
string uuid = string.Empty;
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID"); //OK
CollectionPSObject> PSOutput = PowerShellInstance.Invoke();
foreach (PSObject outputItem in PSOutput)
{
if (outputItem != null)
{
uuid += outputItem.BaseObject.ToString();
}
}
}
return uuid;
}
catch
{
return string.Empty;
}
}

其調(diào)用其實(shí)就是使用PowerShell的Script進(jìn)行獲取。因?yàn)樵谡{(diào)用PowerShell時(shí),可能會(huì)比較的慢,.net中也提供了異步調(diào)用的機(jī)制。核心代碼如下:

private static string GetAsyncUUID()
{
try
{
string uuid = string.Empty;
using (PowerShell PowerShellInstance = PowerShell.Create())
{
PowerShellInstance.AddScript("(get-wmiobject Win32_ComputerSystemProduct).UUID"); //OK
PSDataCollectionPSObject> outputCollection = new PSDataCollectionPSObject>();
outputCollection.DataAdded += outputCollection_DataAdded;
PowerShellInstance.Streams.Error.DataAdded += Error_DataAdded;
IAsyncResult result = PowerShellInstance.BeginInvokePSObject, PSObject>(null, outputCollection);
while (result.IsCompleted == false)
{
Console.WriteLine("Waiting for pipeline to finish...");
Thread.Sleep(1000);
// While里面可以寫上執(zhí)行等待中的一些事情
}
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.");
}

以上代碼運(yùn)行之后,經(jīng)過測(cè)試之后,部門沒有重復(fù)的。

結(jié)果如下:

 

暫時(shí),從以上測(cè)試結(jié)果分析來看,這個(gè)方法是可行的。但目前仍然有比較擔(dān)心的幾個(gè)問題:

1、PowerShell在不同的版本里面,調(diào)用的方法會(huì)不會(huì)不一樣?因?yàn)樽鰹锽/s軟件需要考慮更多的Windows服務(wù)器? 比如: (get-wmiobject Win32_ComputerSystemProduct).UUID

2、為了安全,PowerShell會(huì)不會(huì)被服務(wù)器給禁用?

3、因?yàn)锽/s軟件是需要IIS來運(yùn)行的,會(huì)不會(huì)出現(xiàn)權(quán)限不足的情況??

以上所述是小編給大家介紹的使用PowerShell .Net獲取電腦中的UUID的相關(guān)知識(shí),希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!

您可能感興趣的文章:
  • PowerShell中使用.NET將程序集加入全局程序集緩存
  • Powershell中可以使用的.Net實(shí)用靜態(tài)方法
  • PowerShell入門教程之訪問.Net程序集、COM和WMI實(shí)例
  • Powershell小技巧之查看安裝的.Net framework版本信息
  • PowerShell中調(diào)用.NET對(duì)象的靜態(tài)方法、靜態(tài)屬性和類方法、類屬性例子
  • PowerShell中查看當(dāng)前版本、Windows版本、.NET版本信息的代碼
  • 一個(gè)ASP.Net下的WebShell實(shí)例
  • PowerShell 定時(shí)執(zhí)行.Net(C#)程序的方法
  • ASP.NET下使用WScript.Shell執(zhí)行命令
  • 如何使用 Shell 腳本執(zhí)行 .NET Core 應(yīng)用

標(biāo)簽:喀什 玉林 泰安 呂梁 房產(chǎn) 德宏 佳木斯

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《使用PowerShell .Net獲取電腦中的UUID》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266