Use PowerShell. NET get the Uuid_ practical skill of the computer

Source: Internet
Author: User
Tags foreach uuid

The UUID meaning is the universal Unique identifier (universally unique Identifier), which is a standard for software construction and is also used by the Open Source Software Foundation (open Software Foundation, OSF) in a distributed computing environment ( Distributed Computing Environment, DCE) is part of the field.

Composition

A UUID is a number generated on a single machine that guarantees that all machines in the same space-time are unique. Typically, the platform provides the generated APIs. According to the standard calculations developed by the Open Software Foundation (OSF), Ethernet card address, nanosecond time, chip ID code, and many possible numbers are used.

The UUID consists of a combination of the following parts:

(1) The current date and time, the first part of the UUID is related to time, and if you generate an UUID after a few seconds after generating a UUID, the first part is different and the rest is the same.

(2) clock sequence.

(3) Globally unique IEEE machine identification number, if there is a network card from the network card MAC address, no NIC in other ways to obtain.

The only drawback to the UUID is that the resulting string will be longer. The most common use of the standard for UUID is the Microsoft GUID (Globals Unique Identifiers). In ColdFusion, the Createuuid () function can be used to easily generate a UUID in the form of: Xxxxxxxx-xxxx-xxxx-xxxxxxxxxxxxxxxx (8-4-4-16), where each x is 0-9 or a-f A hexadecimal number within the range. The standard UUID format is: xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx (8-4-4-4-12), which can be converted from the Cflib Download CreateGUID () UDF.

-------above content is excerpted from "Baidu Encyclopedia"

Because the SOFTWARE product needs to bind with the hardware code, think of the UUID, through Baidu, search the Internet after a heap, found that most of the code are as follows:

Need to quote: System.Management;

String processor = "Win32_Processor";//class name
managementclass driveclass= new ManagementClass (processor);

Then, all of our colleagues in our department ran one on their computers and found the results as follows:

The results of all runs are the same. (What is this for??) Now I do not know, but not reconciled, continue to search Google)

----------------------------------------------I'm the divider-----------------------------------------------

Kung Fu not negative, and then check the data found that Windows PowerShell can also get uuid, although for PowerShell I am not familiar with, but the core is can solve my problem?

Windows PowerShell is a command-line shell and scripting environment that enables command-line users and script writers to take advantage of the powerful capabilities of the. NET Framework.

It introduces a number of very useful new concepts that further expands the knowledge you have gained and the scripts you create at the Windows command prompt and Windows Script Host environment.

First, you have to make sure that the operating system has PowerShell installed on your system, in addition to the VS development project need to refer to System.Management.Automation.dll, this DLL in my computer in the following path: "C:\windows\ Assembly\gac_msil\system.management.automation\1.0.0.0__31bf3856ad364e35\ ", native operating system: The Win7 core code is as follows:

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
}
}

The call is actually obtained using the PowerShell script. Because it may be slower to invoke PowerShell, the mechanism for asynchronous invocation is also available in. Net. The core code is as follows:

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 inside can write some things in the execution wait} foreach (PsObject outputitem in outputcollection) {if (Outputitem!= null) {uuid = Outputite
M.baseobject.tostring ();
}} return UUID; catch {return string.
Empty; } static void Error_dataadded (object sender, Dataaddedeventargs e) {Console.WriteLine ("An Error is written to" ERR
or stream! ");} static void Outputcollection_dataadded (object sender, Dataaddedeventargs e) {Console.WriteLine ("object added to output.");} 

After the above code has been run, the department has not duplicated it after testing.

The results are as follows:

For the time being, this method is feasible from the analysis of the above test results. But there are still a few issues that are worrying:

1, PowerShell in different versions, call the method will not be the same? Because as a B/s software need to consider more Windows Server? For example: (Get-wmiobject win32_computersystemproduct). Uuid

2. For security, will PowerShell be disabled by the server?

3, because the B/s software is required to run the IIS, will not appear the lack of access to the situation??

The above mentioned is a small series to introduce the use of PowerShell. NET access to the computer in the UUID related knowledge, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.