2014年7月4日 星期五

WMI First using sample:Method 2:To connect to WMI and retrieve an object using a moniker --- Creating a Registry Key Using VBScript

WMI monikers can consist of three parts: one mandatory component and two optional components. The mandatory component is the "winmgmts:" prefix.
All WMI monikers must begin with "winmgmts:" as shown here:
Set objSWbemServices = GetObject("winmgmts:")
After you have obtained a reference to SWbemServices, you can then invoke one of the object methods as shown here:
Set colSWbemObjectSet = objSWbemServices.InstancesOf("Win32_LogicalDisk")

Step 1write the vb script as following, and save it as "NEW_RegistryKey.vbs"


HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
strSubKey = "SOFTWARE\MyKey\MySubKey"
strValueName = "MyValueName"
strValue = "MyValue"
 
  '''''''1Retrieve a locator object with a call to CreateObject
  '''''''2Log on to the namespace using a call to the ConnectServer method
  '''''''3Set the impersonation level with a call to the SWbemServices.Security_ method
Set ObjRegistry = _
    GetObject("winmgmts:{impersonationLevel = impersonate}!\\" _
    & strComputer & "\root\default:StdRegProv")
 
''''''4Implement the purpose of your scriptThe StdRegProv class has CreateKey  methods
Return = objRegistry.CreateKey(HKEY_LOCAL_MACHINE, strSubKey)
 
'''''''In scripting or Visual Basic, the method returns an integer value that is 0 (zero) if successful. If the function fails, the return value is a nonzero error 
code that you can look up in WbemErrorEnum.
  If Return <> 0 Then
    WScript.Echo "The operation failed." & Err.Number
    WScript.Quit
Else
      wScript.Echo "New registry key created :" & VBCRLF _
        & strSubKey 
End If
 
objRegistry.SetStringValue HKEY_LOCAL_MACHINE, strSubKey,strValueName ,strValue
 
Step 2using cscript to run NEW_RegistryKey.vbs"on windows command prompt



Step 3To check the result on registry setting


沒有留言:

張貼留言