2014年7月16日 星期三

Powershell與WMI Provider的簡介和使用(新增和設定rigistry key)

目的:了解如何使用Powershell使用WMI Provider
目標:1、經由Powershell Console呼叫WMI Provider來修改rigistry key和設定其default value
      2、經由Powershell Script呼叫WMI Provider來修改rigistry key和設定其default value

WMI Provider簡介

WMIWindows system的資訊database,經由 WMI Service連結到databasequery database的資訊。
經由以下的步驟可以經由開啟WMI Control Properties來看到 WMI的詳細資訊(Name space)
1、經由Administrative Tools打開 "Computer Management"
2、展開 "Services and Applications."
3Right-click "WMI Control" 和選擇Properties.



WMI Namespace 底下包含許多classesclass則由propertiesmethods所組成。我們可以經由PowerShell 來使用WMI class做一些系統環境的設定。
以下顯示如何使用Get-WmiObject cmdletlistNamespaceclasses


Example:使用Get-WmiObject cmdletlistroot\ cimv2classes
Get-WmiObject -List -Namespace "root\ cimv2 "




EExampleListWMI ClassPropertiesMethods

using the "Get-Member" cmdletlistWin32_Processor classPropertiesMethods
Get-WmiObject -Class "Win32_Processor" -Namespace "root\CIMV2" | Get-Member



Note
或使用以下的方式



Note
Win32 classes,例如 Win32_NetworkAdapter Win32_Process是在monitormanage 系統的hardwarefeatures。 一般而言,這些classes是屬於root\cimv2 WMI namespace
Win32_Processor WMI class 顯示一個device能夠interpret一系列在電腦執行的windows OS指令,在multiprocessor computer, 每一個processor都存在一個Win32_Processor instance
以下的syntax包含Win32_ Processor WMI class的所有inherited propertiesMEMBER

Syntax    
[Provider("CIMWin32")]class Win32_Processor : CIM_Processor
{
  uint16   AddressWidth;
  uint16   Architecture;
  uint16   Availability;
  string   Caption;
  uint32   ConfigManagerErrorCode;
  boolean  ConfigManagerUserConfig;
  uint16   CpuStatus;
  string   CreationClassName;
  uint32   CurrentClockSpeed;
  
    
  
  string   SystemCreationClassName;
  string   SystemName;
  string   UniqueId;
  uint16   UpgradeMethod;
  string   Version;
  uint32   VoltageCaps;
};
 
Members
The Win32_Processor class has these types of members:
Methods
The Win32_Processor class has these methods.

Method
Description
Reset
Not implemented. For more information about how to implement this method, see the Reset method in CIM_Processor.
SetPowerState
Not implemented. For more information about how to implement this method, see the SetPowerState method in CIM_Processor.



Rigistry keyWMI Provider為何

System Registry Provider

The System Registry provider enables management applications to retrieve and modify data in the system registry, and receive notifications when changes occur. The registry can be located on a local or remote computer. The _Win32Provider instance name is RegProv; the property provider name is RegPropProv; and the event provider name is RegistryEventProvider. For more information, see Modifying the System Registry.
The provider has the following __Win32Provider instances:
l   "RegProv"
l   "RegistryEventProvider"
l   "RegPropProv"

The System Registry provider supports the following classes located in the \root\defaultnamespace:

Event class
Description
Abstract class from which the other registry event classes are derived.
Represents changes to a specific registry key.
Represents changes to a specific key or its subkeys.
Represents changes to a single value of a specific key.
Contains methods that manipulate system registry keys and values.


StdRegProv class
The StdRegProv class contains methods that manipulate system registry keys and values. StdRegProv is preinstalled in the WMI namespaces root\default and root\cimv2.

Syntax
[Provider("RegProv"), Dynamic]class StdRegProv
  {
};
Members
The StdRegProv class has these types of members:
Methods
The StdRegProv class has these methods.

Method
Description
Verifies that the user has the specified access permissions.
Creates a subkey.
Deletes a subkey.
Deletes a named value.
Enumerates subkeys.
Enumerates the named values of a key.
Gets the binary data value of a named value.
Gets the DWORD data value of a named value.
Gets the expanded string data value of a named value.
Gets the multiple string data values of a named value.
Gets the QWORD data values of a named value.
Windows Server 2003 and Windows XP:  This method is not available.
Gets the security descriptor for a key.
Windows Server 2003 and Windows XP:  This method is not available.
Gets the string data value of a named value.
Sets the binary data value of a named value.
Sets the DWORD data value of a named value.
Sets the expanded string data value of a named value.
Sets the multiple string values of a named value.
Sets the QWORD data values of a named value.
Windows Server 2003 and Windows XP:  This method is not available.
Sets the security descriptor for a key.
Windows Server 2003 and Windows XP:  This method is not available.
Sets the string value of a named value.


Remarks
StdRegProv supplies only methods


Example:使用Get-WmiObject cmdletlistroot\defaultclasses
Get-WmiObject -List -Namespace "root\default"




Note
StdRegProv 必須下以下的command(額外加-list),才能使用GET-MEMBER
Along with creating instance of a WMI class, the Get-WMIObject cmdlet can also used for listing the WMI classes using the -list parameter. Once we have that list, we can filter any of the specific classes we want through this list.




Powershell如何使用WMI Provider

使用以下的sample來示範,主要為呼叫WMI ProviderStdRegProv class來新增registry key和設定defaultvalue

l   Method1、使用Powershell console
1、設定變數


Note
變數也可以使用以下的方式宣告


看變數的宣告值為何

讀取單一的變數值


2connectWMIStdRegProv class
使用$objService = [WMIClass] "root\default: StdRegProv"



或則
$objService = Get-WmiObject -class StdRegProv -Namespace "root\default″ -list




或則
$objService = Get-WmiObject –Namespace "root\default" –List | where-object { $_.Name –eq "StdRegProv" }



或則
$objService =new-object System.Management.ManagementClass "Root\default:StdRegProv"




Note
連結到StdRegProv class,可以使用get-member來查看method


3、新增一個registry sub Key




Note
return value0,才算成功。
如果不適使用administraor權限來執行,會得到以下的return value


4、新增一個registry value Key和設定default value



Note
如果下以下的語法,則會新增一個registry key (MyValueName)






5、結果如下:




l   Method2、使用power shell script
###$regKey = @{ "HKEY_CLASSES_ROOT" = 2147483648; "HKEY_CURRENT_USER" = 2147483649;
###"HKEY_LOCAL_MACHINE" = 2147483650; "HKEY_USERS" = 2147483651; "HKEY_CURRENT_CONFIG" = ###2147483653 }
###$strComputer = "."
HKEY_LOCAL_MACHINE" = 2147483650
$strSubkeyname = "SOFTWARE\MyKey"
$strValue=" MyValue"
###$objService = Get-WmiObject –Namespace "root\default" –List | where-object { $_.Name –eq ###"StdRegProv" }
###$objService = [WMIClass] "root\default: StdRegProv"
###$objService = Get-WmiObject –class StdRegProv -Namespace "root\CIMV2″ -ComputerName $strComputer
$objService = [WMIClass] "root\default: StdRegProv"
$objService.CreateKey($HKEY_LOCAL_MACHINE, $strSubkeyname)
$objService.SetStringValue($HKEY_LOCAL_MACHINE, $strSubkeyname, "", $strValue)

1、使用notepad或其他編輯工具編輯script,將其存成.ps1檔案。



2、使用powershell console執行script檔。



沒有留言:

張貼留言