当前位置: 移动技术网 > IT编程>开发语言>.net > 使用PowerShell .Net获取电脑中的UUID

使用PowerShell .Net获取电脑中的UUID

2017年12月12日  | 移动技术网IT编程  | 我要评论

北干听风冷暖人间,雪龙号完成任务,冰帝校园行txt下载

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"); //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.");
}

以上代码运行之后,经过测试之后,部门没有重复的。

结果如下:

 

暂时,从以上测试结果分析来看,这个方法是可行的。但目前仍然有比较担心的几个问题:

1、powershell在不同的版本里面,调用的方法会不会不一样?因为做为b/s软件需要考虑更多的windows服务器? 比如: (get-wmiobject win32_computersystemproduct).uuid

2、为了安全,powershell会不会被服务器给禁用?

3、因为b/s软件是需要iis来运行的,会不会出现权限不足的情况??

以上所述是小编给大家介绍的使用powershell .net获取电脑中的uuid的相关知识,希望对大家有所帮助

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网