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簡介

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



WMI Namespace 底下包含許多classes, class則由properties和methods所組成。我們可以經由PowerShell 來使用WMI 的class做一些系統環境的設定。
以下顯示如何使用Get-WmiObject cmdlet來list出 Namespace的classes

2014年7月13日 星期日

PowerShell Provider的簡介和使用(新增和設定rigistry key)

PowerShell Providers 是 .NET programs允許mount "data stores"成 drives來工作,簡單的存取PowerShell environment的外部資料,如如同file system 般的存取registry。這意味著能如同使用存取files和 folders的cmdlets,可以參考以下的 table 。

簡短的敘述
Windows PowerShell providers提供簡單的command line來存取data和components不須另外的command,data類似一個file system drive般的呈現。

完整描述
Windows PowerShell providers 是NET programs ,使Windows PowerShell 可以簡單的view和manage在特殊data store的data。
Provider將data呈現在drive,如同一個hard drive,可以使用provider支援的built-in cmdlets來管理資料,另外也可特別針對一些data來製作custom cmdlets。
Providers 也可以搭配 "dynamic parameters"來使用 built-in cmdlets,當cmdlet被使用來存取provider data。
Provider 也被稱為 "snap-in" ,如同PowerShell 的dynamic link library (.dll), library當執行command時,指示PowerShell來做action的code。也就是說如果使用VBScript 來讀取registry,不需要再寫code。 可以寫新的和 Custom 的Providers,請參考 The PowerShell Software Developers Kit provides documents should you wish to build your own Providers.

列出PowerShell 的Providers
使用 "Get-PSProvider" cmdlet


2014年7月11日 星期五

PowerShell Fist programming(1):如何使用cmdlets來新增和設定rigistry key

我們試著執行一個example,在HKEY_LOCAL_MACHINE\SOFTWARE底下建立一個新的"MyKey"的registry key,和將該registry key的default value設定為"MyValue"。
·         使用PowerShell console
使用New-Item cmdlet新增"MyKey" 的registry key和寫入"MyValue"的default value的command如下:
New-Item -Path HKLM:\Software\MyKey -Value "MyValue"


新增的registry key和寫入該key的default value,結果如下:

Powershell Function call

Powershell 既然是一個script language,就有所謂的function call。

·         directly executable code
以下是直接在script file裡面來呼叫sum function
1、將寫好的script存成.ps1檔案。



PowerShell Simply induction

Windows PowerShell 是一個新的任務基礎(task-based)的command-line shell和 scripting language,是特別為系統的管理(administration)而設計。
Windows PowerShell 是以.NET Framework為基礎來建立,, 可以幫助 IT professionals 和power users控制和自動化windows OS和應用程式的管理。
此外, Windows PowerShell有豐富的expression parser和完整先進的scripting language。
Windows PowerShell is very different.
  • Windows PowerShell不處理text,他處理以.NET Framework platform為基礎的 objects。
  • Windows PowerShell 有自己的 built-in commands,所有的shell commands使用相同的command parser和一致的介面。
Windows PowerShell 使用自己的語言,而非使用既有已經存在的語言,因為:
  • Windows PowerShell 需要管理Microsoft .NET Framework objects.的語言。
  • 這個語言需要一致的環境來使用cmdlets。
  • 這個語言需要支援complex tasks,而不是使簡單的tasks更複雜。
  •  這個語言需要與如C#般在.NET Framework中使用的higher-level languages一致。

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 1、write the vb script as following, and save it as "NEW_RegistryKey.vbs"

WMI First using sample:Method 1: To connect to WMI using SWbemLocator--- Creating a Registry Key Using VBScript

You can use the methods of the SWbemLocator object to obtain an SWbemServices object that represents a connection to a namespace on either a local computer or a remote host computer. You can then use the methods of the SWbemServices object to access WMI.
You can use the methods of an SWbemServices object to perform operations against a namespace on either a local host or a remote host. This object cannot be created by the VBScript CreateObject call.

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