This series of articles from the beginning of initial knowledge, basically can complete some simple system management, in order to more convenient management system, at the same time in order to better explore the performance of the system, you need to use the system to provide
Some of the advanced features of the Windows Server family of OS, if you can take advantage of the latest features to manage the system, it will be a very good thing, although the current winserver used relatively little
But in some places it is useful, especially when non-computer professionals are required to operate on the server, Winserver is more suitable for applications than unix/linux simplicity. Today
Here is a description of some of the advanced features in PS.
One, the WMI object
We know that under the Windows-series operating system, WMI has been a powerful tool for management systems and the core technology of win system management. There are many functions that can be implemented with WMI, and in PS, this function
Can not only not weaken, but has been strengthened. WMI provides a consistent way to expose various types of information, so let's talk about this topic slowly.
1, 1 Get WMI class
We know that WMI can provide powerful functionality, but we need to know the interface of the application when we apply it, and what objects the app provides to operate, hundreds of classes in WMI, and
There are dozens of properties for some classes, so WMI is a complex class library and object library.
The Get-wmiobject command allows you to obtain the summary information for the WMI type library. (The following command uses ...) Represents the omitted output)
EXP: Obtaining WMI information with the Get-wmiobject-list command
PS c:\users\vol_20120330> get-wmiobject-list namespace:root\cimv2name Methods Properties--------------------- __systemclass {} {} __thisnamespace {} {Security_descriptor} __namespace {} {Name} __provider {} {Name} __win32provider {} {Clientloadableclsid, clsid...__providerregistration {} {provider} __eventproviderregistration {} {eventquerylist, provider} __objectproviderregistration {} {interactiontype, provider,... __classproviderregistration {}{cacherefreshinterval, Inte...__instanceproviderregistration {} {interactiontype, provider,... __met Hodproviderregistration {} {provider} __propertyproviderregistration {} {provider, Supportsget, sup...__eventconsumerproviderregistration {} {Consumerclassname S, provider}__indicationrelated {} {}
.............
We need to look at the head of this output: namespace:root\cimv2; This output indicates that the currently acquired WMI type is subordinate to the root\cimv2 namespace (some people prefer to translate it into namespaces, but I personally
Think the translation into a namespace than the name of the space to listen to some, Hee hee ... Namespace can be used as a parameter to the Get-wmiobject command.
The above command is equivalent to: get-wmiobject-list-namespace root\cimv2
Get-wmiobject has a number of parameters, one of which is the ComputerName parameter, specifying the computer name or IP address that can be used to obtain relevant information from the remote computer. The command is:
Get-wmiobject-list-computername 192.168.0.1
When you obtain information from a remote computer through this command, the remote computer must be running WMI, and the account used must be a member of the Administrators group on the remote computer, and in this way remote computer information is
The computer does not need to run PS, so you can use PS to manage a remote computer that does not have PS installed.
The ComputerName parameter can also be used to obtain information and resources for this machine.
Exp:
PS c:\users\vol_20120330> get-wmiobject-list -computername localhost namespace:root\cimv2name Methods Properties ---- ------- ---------- __systemclass {} {} __thisnamespace {} {security_descriptor} __namespace {} { name} __provider {} {name}
The above localhost represents the native.
So you can have a few commands to get the information for this machine: get-wmiobject-list
Get-wmiobject-list-computername localhost
Get-wmiobject-list-computername the native computer name
Get-wmiobject-list-computername Native IP Address
1, 2 displays the WMI class details
If you know the name of a WMI, how do you show the related system information for that class? We can use the-class parameter to display information about a WMI class.
EXP: Displaying information about the Win32_OperatingSystem class with the-class parameter
PS c:\users\vol_20120330> get-wmiobject -class win32_operatingsystem-namespace root\cimv2 Systemdirectory:c:\windows\system32organization: buildnumber : 7601RegisteredUser : Vol_ 20120330SerialNumber : 00426-oem-8992662-00015version : 6.1.7601
In the previous narration, we said that cmdlets have their own default properties, and when no parameters or attributes are specified, the cmdlet will operate with the default properties or parameters, and Get-wmiobject has several default parameters:-class;
The default namespace is root\cimv2; The computername default is native (localhost).
So the above command: get-wmiobject-class win32_operatingsystem-namespace root\cimv2
Equivalent: get-wmiobject win32_operatingsystem
1, 3 display non-default properties using the Format command
As I said earlier, you can use the Format command to display non-default display properties or information for objects, as well as to display non-default information. For example, using the format-table command to display Win32_OperatingSystem total* and free
Property.
Exp:
PS c:\users\vol_20120330> get-wmiobject -class win32_operatingsystem-namespace root\cimv2 | Format-table-property Total*,free*totalswapspa totalvirtual totalvisible freephysical FreeSpaceIn FreeVirtual Name cesize Memorysize memorysize memory pagingfiles memory ----------------------------------------------- --------------------------- 4039784 2019892 496464 1496860 2006200 microsof...________ ____________________________________________________________________________
Here's a point: in the Format command, when you use the-property parameter, you can use wildcards for output.
Ii. Creating a WMI Object
Since there are so many classes in the WMI model, we can use these classes to instantiate some objects based on object-oriented thinking. Instantiate an object in PS using the New-object command.
2, 1 creating WMI objects
Under Windows, some software components have NET Framework and COM interfaces, so you can perform many of the system's administrative tasks, and PS can use these components. Early versions of PS Most cmdlets do not support remote computers.
However, this limitation can be eliminated when using the NET Framework's System.Diagnostics.EventLog class to manage event logs in PS.
Exp:new-object command to create a System.Diagnostics.EventLog object
PS c:\users\vol_20120330> new-object-typename system.diagnostics.eventlog Max (K) Retain overflowaction Entries Log -------------------------- ----------
As shown above, we have created an System.Diagnostics.EventLog object, but this object instance does not contain any data because it does not have a specific event log assigned to it.
2, 2 constructors
Consistent with object-oriented, you need to initialize the object with the constructor of the class. Here we refer to the specific event log by specifying the log name and pass the log name to the constructor of the class. Use the-argumentlist parameter to specify a specific event log
The journal name of the object.
EXP: Initializing an object with the ArgumentList parameter
PS c:\users\vol_20120330> new-object-typename system.diagnostics.eventlog -argumentlist application Max (K) Retain overflowaction Entries Log -------------------------- ---------- 20,480 0 overwriteasneeded 5,900 Application
Because most of the NET Framework core classes in PS are defined in the system clear space, if PS cannot find the specified type name entry, the class is found in the System namespace, which means that we can not specify the system
Namespaces to refer to System.Diagnostics.EventLog, instead using Diagnostics.eventlog.
Exp:
PS c:\users\vol_20120330> new-object-typename diagnostics.eventlog -argumentlist application Max (K) Retain overflowaction Entries Log -------------------------- ---------- 20,480 0 overwriteasneeded 5,900 Application
As shown above, the execution results are the same.
2, 3 use variables to store objects
As we created the object above, we have to enter this creation command every time for other operations, which is too cumbersome; in PS you can use variables to store objects.
Exp:
____________________________________________________________________________________ps C:\Users\vol_20120330 > $var =new-object-typename diagnostics.eventlog -argumentlist application _________________________________ ___________________________________________________ps c:\users\vol_20120330> $var Max (K) Retain OverflowAction Entries Log -------------------------- ---------- 20,480 0 overwriteasneeded 5,900 Application ____________________________________________________________________________________
Any valid PS command output can exist in the variable, the variable name starts with $, to reference the variable, the variable name can be used. As we set up the $var variable, we can use it to get the relevant system event log information.
Exp:
PS c:\users\vol_20120330> $var |get-member TypeName:System.Diagnostics.EventLogName membertype Definition------------------------ Disposed Event System.EventHandler disposed (System.Object, Sys ... EntryWritten Event System.Diagnostics.EntryWrittenEventHandler Ent ... BeginInit method System.Void BeginInit () Clear Method System.Void Clear () Close Method system.void Close () Createobjref Method System.Runtime.Remoting.ObjRef createobjref (Typ ... Dispose method System.Void Dispose () EndInit method System.Void EndInit () Equals Method bool EquALS (System.Object obj) GetHashCode Method int GetHashCode () Getlifetimeservice Method System.Object Getlifetimeservice () GetType Me Thod type GetType () InitializeLifetimeService Method System.Object Initializeli Fetimeservice () modifyoverflowpolicy Method system.void modifyoverflowpolicy (System.diagnos ... Registerdisplayname Method system.void registerdisplayname (string resource ... ToString Method String ToString () WriteEntry method System.Void WriteEntry (String message), System .... WriteEvent Method system.void WriteEvent (System.Diagnostics.Event ... Container Property System.ComponentModel.IContainer Container {get;} EnableRaisingEvents Property System.Boolean enableraisingevents {get;set;} ENtries Property System.Diagnostics.EventLogEntryCollection Entr ... Log Property System.String log {get;set;} Logdisplayname Property System.String logdisplayname {get;} MachineName Property System.String machinename {Get;set;} Maximumkilobytes Property System.Int64 maximumkilobytes {get;set;} Minimumretentiondays Property System.Int32 minimumretentiondays {get;} OverflowAction Property System.Diagnostics.OverflowAction Overflowactio ... Site Property System.ComponentModel.ISite site {get;set;} Source Property System.String Source {get;set;} SynchronizingObject Property System.ComponentModel.ISynchronizeInvoke synchr...__________________________________ __________________________________________________
You can also use variables to reference object properties and methods.
Exp:
PS c:\users\vol_20120330> $var. MachineName . ____________________________________________________________________________________ps C: \ Users\vol_20120330> $var. logapplication
In PS, if a method is called without parentheses (), the Help information for this method is displayed. A method must be called with parentheses to invoke it.
EXP: Call $var's Clear method, but without ()
PS c:\users\vol_20120330> $var. Clearmembertype : methodoverloaddefinitions: {system.void Clear ()} Typenameofvalue : System.Management.Automation.PSMethodValue : System.Void Clear () Name : Clearisinstance : True
Summary: Create a variable that stores objects through $variablename=new-object; After a variable stores an object, the object information can be displayed through $variablename; The method by which the object is called by $variablename.method; VariableName
The properties of the. Property Reference object.
2, 4 creating COM objects
You can use the New-object command to create a Component Object Model (COM) component by using the-comobject parameter. Both WSH and ActiveX applications can apply COM components. For example, ie Browser can apply COM component model;
When you create a COM object with the ComObject parameter, you need to develop the attachment information to create the COM component. There are usually two types of attachment information: the ProgID and the programmatic identifier of the COM class .
The following ProgID can be specified in PS to create COM components: Wscript.Shell, Wscript.Network, Script.dictionary, Script.filesystemobject.
EXP: We create a wscript.shell COM component
PS c:\users\vol_20120330> $wshshell =new-object -comobject wscript.shell______________________________ ______________________________________________________ps c:\users\vol_20120330> $wshshellSpecialFolders CurrentDirectory -------------- ---------------- system.__comobject C:\Users\vol_20120330
2, 5 creating shortcuts with Wscript.Shell COM components
Step: 1, create a variable to store the Wscript.Shell object
2. Call the CreateShortcut method to create an Lnk object, the CreateShortcut method needs to pass a string that stores the path of the LNK file and the LNK file bag name as a parameter, as follows:
PS c:\users\vol_20120330> $wshshell. CreateShortcut ("$home \my.lnk") FullName : C:\Users\vol_20120330\ My.lnkarguments: Description : Hotkey: iconlocation :, 0RelativePath : TargetPath : WindowStyle
3. Use the TargetPath property of the Lnk object to specify the directory or file to which the Lin object is connected; To manipulate the Lnk object, you need to store it with a variable
EXP: Saving variables
PS c:\users\vol_20120330> $mylnk = $wshshell. CreateShortcut ("$home \my.lnk")
EXP: Set Target location
PS c:\users\vol_20120330> $mylnk. targetpath= $home
4. Store object, save Lnk object after setting
Exp:
PS c:\users\vol_20120330> $mylnk. Save ()
Let's look at whether the object was successfully created:
PS c:\users\vol_20120330> ls directory: C:\Users\vol_20120330Mode lastwritetime Length Name ---------------------------d-r-- 2012/3/30 18:27 Contacts d-r--2012/5/14 19:17 Desktop d-r--2012/5/12 17:18 Documents d-r--2012/5/14 1:49 Downloads d-r--2 012/5/2 9:51 Favorites d-r--2012/3/30 18:27 Link s d-r--2012/4/30 14:15 Music d-r--2012/3/30 18:27 Pictures d-r--2012/3/30 18:27 Saved Games d-r--2012/3/30 18:27 searches d-r--2012/3/30 18:27 Videos d----2012/5/12 19:02 Vol.-A---2012/5/14 21:59 498 my.lnk-A---2012/5/12 17:25 10346 process.txt-a---2012/5/12 17:31 5254 process1.txt-a---2012/4/15 19:14 509 regwizard.log -A---2012/4/15 19:14 9853 sanct.log
As shown above, the red section indicates that we have successfully created a shortcut. If you enter under the current path:. \my.lnk opens our Resource manager and navigates to the $home directory.
2, 6 use internetexplore.application progid to create IE browser Browse Blog Park
1, the first need to create an IE object
Exp:
PS c:\users\vol_20120330> $ie =new-object-comobject internetexplorer.application
2, then need to call the IE object's Navigate method to set the ID object to connect the URL.
EXP: Specify URL as www.cnblogs.com
PS c:\users\vol_20120330> $ie. Navigate ("Www.cnblogs.com")
3, using the document.body.innertext of IE to obtain the text content within the www.cnblogs.com
Exp:
As shown, we browsed the www.cnblogs.com using PS. If we go further, we can get the content we want, like downloading an ebook or something.
This will start an IE process in the system, the process will not automatically stop with PS exit, we need to terminate manually, or know when the shutdown will stop.
4, the $ie variable becomes invalid. Using $ie = $null
5, then use Remove-varibale IE delete variable $ie; We're not doing any experiments at the back of these two articles.
2.7 Getting warning messages about the NET Framework in PS
Because the RCW package in the PS-in-NET Framework is not exactly the same as the standard COM object, there are some subtle differences, and they behave differently, and using the strict parameter generates a related warning message when the COM object is created.
Exp:
New-object: Could not load COM type Excel.Application. Location Line: 1 characters: + $excel =new-object <<<< -comobject excel.application-strict + categoryinfo : Invalidtype: (:) [New-object], psargumentexception + Fullyqualifiederrorid:cannotloadcomobjecttype, Microsoft.PowerShell.Commands.NewO Bjectcommand
Third, summary
The WMI and COM components of Windows are a complex system that needs to be slowly understood to be applied, and then we will continue to explore this.
Powershell_ 0 Basic Self-study course _8_ Advanced Topics: WMI objects and COM components