Windows 8 and Windows Server 2012 Modules and Cmdlets

0
135
10 min read

Core modules

These modules are installed by default in both Windows 8 and Windows Server 2012. These cmdlets will be available on machines that are running the latest version of the Windows Management Framework as well.

Invoke-WebRequest

In the previous version of PowerShell, a .NET class was required to access web-based content. In PowerShell 3.0, the same can be accomplished by utilizing the Invoke-WebRequest cmdlet. The cmdlet returns an object HttpWebResponseObject that contains all kinds of information about the HTTP request such as the response code, the response content, and even parsed elements within the page, such as links and forms.

Usage examples

The following example retrieves a pages links, selects the first three, and outputs their innerHTML and destination:

PS C:> Invoke-WebRequest www.microsoft.com |

Select-Object -ExpandProperty links

| Select-Object -First 3 –Property innerHtml,href

innerHTML    href

---------    ----

Windows    http://windows.microsoft...

The following example retrieves the first image found on www.microsoft.com and displays it in the default web browser:

PS C: > Start-Process (invoke-webrequest -Uri http://www.microsoft.com |

Select-Object -ExpandProperty images

| Select-Object -First 1 -ExpandProperty Src)

Invoke-RestMethod

This cmdlet is used to invoke Representational State Transfer (REST) methods.

REST is a client/server architecture based on resource types and state. Universal Resource Identifiers (URI), are used to access different resources within a RESTful service. Typically, RESTful services utilize the existing HTTP protocol to communicate between the client and server.

Usage examples

The following example invokes a REST method using the HTTP GET method to retrieve a particular resource on the mydomain.com server. It is expected that the result will be returned as JSON:

Invoke-RestMethod –Uri http://www.mydomain.com/group/1/user/123

-Method GET -ReturnType Json

ConvertTo-Json

This cmdlet is used to convert objects in PowerShell into JavaScript Object Notation (JSON). JSON is used to serialize object structures to a string so that it can be sent in between a client and server. JSON is frequently used on wide area networks because its syntax is much more succinct than schemes such as XML.

Usage examples

The following example converts an object to JSON:

PS C:> Get-Variable PSUICulture | ConvertTo-Json

{

"Value":  "en-US",

"Name":  "PSUICulture",

"Description":  "UI Culture of the current Windows PowerShell Session.",

"Visibility":  0,

"Module":  null,

"ModuleName":  "",

"Options":  9,

"Attributes":  [

]

}

The following example converts an object to JSON that has members added at runtime:

PS C:> Get-Random | Add-Member -MemberType NoteProperty

-Value "MyValue" -Name "MyProperty" –PassThru | ConvertTo-Json

{

"value":  1705462562,

"MyProperty":  "MyValue"

}

The following example shows that the ConvertTo-Json treat data types like strings and integers differently than the objects shown previously. Notice that there is no length property serialized in this example, even though the string class exposes it.

PS C:> "MyString" | ConvertTo-Json

"MyString"

PS C:> Get-Random | ConvertTo-Json

1231

ConvertFrom-Json

This cmldet is used to convert a JSON string into an object that can be used in PowerShell. Unlike the ConvertTo-CliXml, the ConvertFrom-Json cmdlet does not preserve type data and will simply be of the type PSCustomObject. Particular data types can be converted into their object representation if put in the correct format.

Usage examples

The following example shows that the JSON string in the example is converted into a PSCustomObject with a property DateTime that is a String type:

PS C: > '{ "DateTime": "Thursday, February 23, 2012 7:56:18 PM" }'

| ConvertFrom-Json | Get-Member -Name Date

Time

TypeName: System.Management.Automation.PSCustomObject

Name  MemberType  Definition

----   ----------   ----------

DateTime NoteProperty System.String DateTime=Thursday,

February 23, 2012 7:56:18 PM

The following example shows that particular data type formats, in this case a date, are passed to the ConvertFrom-Json cmdlet, it will be able to convert it to a .NET class:

PS C: > '{ "DateTime": "/Date(1330048578834)/" }'

| ConvertFrom-Json | Get-Member -Name DateTime

TypeName: System.Management.Automation.PSCustomObject

Name  MemberType  Definition

----  ----------  ----------

DateTime NoteProperty System.DateTime

DateTime=2/24/2012 1:56:18 AM

ControlPanelItem

The Get-ControlPanelItem and Show-ControlPanelItem cmdlets are used to list and show control panel items that are currently installed on the system. Using the Show-ControlPanelItem will cause the control panel window to be shown immediately.

Usage examples

The following example gets the Mouse control panel item and shows the Name, Category and Description properties:

PS C:> Get-ControlPanelItem -Name Mouse

| Select-Object Name,Category,Description

Name  Category  Description

----  --------  -----------

Mouse  {All Control Panel Items}Customize your...

The following example shows the Fonts control panel item using the canonical name:

PS C:> Show-ControlPanelItem -CanonicalName Microsoft.Fonts

Rename-Computer

This cmdlet is used to rename local or remote computers. The cmdlet can use both local or domain credentials and can trigger a restart.

Usage examples

The following example renames the computer driscoll-hv1 to driscoll-hv2 using the domain credentials. The command also bypasses confirmation and causes a restart of the machine:

PS C:> Rename-Computer –ComputerName driscoll-hv1

–NewName driscoll-hv2 –DomainCredential mdnadministrator –Force –Restart

TypeData

The TypeData cmdlets are used for managing the Extended Type System (ETS), type data that has been loaded into PowerShell. This type data is added through *.types.ps1xml files and are typically found in modules.

Usage examples

The following example returns the ETS type data for the System.Management.ManagementObject class and displays the added ETS members:

PS C: > Get-TypeData System.Management.ManagementObject

| Select-Object -ExpandProperty Members

Key      Value

---      -----
ConvertToDateTime  ...ScriptMethodData

ConvertFromDateTime  ...ScriptMethodData

 

The following example removes the ETS type data for the System.Guid class:

PS C: > [System.Guid]::NewGuid() | Get-Member –Name Guid

TypeName: System.Guid

Name    MemberType Definition

----    ---------- ----------

Guid    ScriptProperty System.Object Guid {get=$this.ToString();}

PS C: > Remove-TypeData –TypeName System.Guid

PS C: > [System.Guid]::NewGuid() | Get-Member –Name Guid

PS C: >

Unblock-File

This cmdlet is used to remove the alternate data stream from the file, known as the zone identifier. The zone identifier signifies that the file has come from the Internet and PowerShell will warn or prevent you from executing scripts with this zone identifier. This can become very hard to deal with when a lot of files are involved, such as with a module. The Unblock-File cmdlet makes this much easier.

Usage examples

The following example removes the zone identifier from the Test.ps1 file:

PS C:> Unblock-File –Path C:UsersAdamDocumentsTest.ps1

The following example unblocks all the files in the HyperV module directory:

PS C:> Get-ChildItem -Path C:UsersAdamDocumentsWindowsPowerShell ModulesHyperV | Unblock-File

Standard modules

These modules are part of the standard set of modules available on Windows 8 and Windows Server 2012 machines. They are not available to users who install the Windows Management Framework on machines such as Windows 7 or Windows Server 2008 R2. No features or roles are required to access any of these modules and most of them are based on CIM providers so they support CIM sessions.

NetAdapter module

This module is available for the users running Windows 8 and Windows Server 2012 and it requires no feature. It contains a total of 64 cmdlets. The following command shows how to import this module:

PS C: > Import-Module NetAdapter

The NetAdapter module exposes cmdlets that manage the network adapters, protocol bindings, and numerous other network related operations. The module is enabled by default and there are no features that need to be installed to enable it. The cmdlets in this module are based on CIM and support CIM sessions.

NetAdapter cmdlets

These cmdlets allow us to manage the network adapters that are currently installed on the system. The Get-NetAdapter allows us to retrieve them. The Set-NetAdapter allows us to change the MAC and VLAN ID of the adapters. The Enable-NetAdapater, Disable-NetAdapter, and Restart-NetAdapter allow us to change the state of the adapter.

Usage examples

The following example returns all the network adapters on the local machine:

PS C:> Get-NetAdapter

Name  Interface MacAddress Operational LinkSpeed

       Index Status

----  --------- ---------- ----------- ---------

Wired... 12 00-15-5D-91-3A-0C     Up 10 Gbps

The following example resets the network adapters that match the name Wired*:

PS C:> Get-NetAdapter Wired* | Restart-NetAdapter

The following example sets the VLAN ID of all the network adapters that match the name Wired*:

PS C:> Get-NetAdapter Wired* | Set-NetAdapter –VLANID 3

NetAdapterBinding

Theses cmdlets let us manage the network bindings for the various interfaces defined in the local or remote system. In addition to seeing the current defined bindings, such as TCP/IP v4, we can enable and disable the bindings for particular interfaces.

Usage examples

The following example returns the network bindings that match the display name *TCP/IPv4*:

PS C: > Get-NetAdapterBinding -DisplayName *TCP/IPv4*

Name  DisplayName ComponentID   Enabled

----  ----------- -----------   -------

Wired...Internet... ms_tcpip   True

The following example disables all network adapter bindings that match the display name *TCP/IPv6*:

PS C: > Get-NetAdapterBinding -DisplayName *TCP/IPv6* | Disable- NetAdapterBinding

NetAdapaterAdvancedProperty

These cmdlets are used for managing some of the advanced properties for network adapters. Some of these properties include receive buffer size and TCP checksum offload for IPv4 and IPv6. We can retrieve the properties using the Get-NetAdapterAdvancedProperty cmdlet and set them using the Set-NetAdapterAdvancedProperty cmdlet. We can even create new properties using the New-NetAdapterAdvancedProperty cmdlet.

Usage examples

The following example gets the Receive Buffers advanced NetAdapter property:

PS C:> Get-NetAdapterAdvancedProperty –DisplayName "Receive Buffers"

| Select-Object -ExpandProperty RegistryValue

512

The following example enables the Jumbo Packet advanced NetAdapter property:

PS C:> Set-NetAdapterAdvancedProperty –Name "Wired Ethernet Connection"

–DisplayName "Jumbo Packet" –Value "Enabled"

SmbShare module

This module is available in Windows 8 and Windows Server 2012 and it does not require any feature as well. There are a total of 23 cmdlets in this module. The following command is used to import it:

PS C:> Import-Module SmbShare

The SmbShare module is used to manage Server Message Block (SMB) shares, connections, and other share related information on a Windows machine. The cmdlets found within the module can create new shares, configure permissions, and enumerate connections to the shares. The cmdlet can also control SMB client configuration settings and network interfaces used for the SMB connections. The cmdlets within this module are native CIM cmdlets and support CIM sessions.

SmbShare

The SmbShare cmdlets allow for enumeration, creation, removal, and access control of the SMB shares on the current server.

Usage examples

The following example selects the first SMB share:

PS C: > Get-SmbShare | Select-Object –First 1

Name    ScopeName    Path   Description

----     ---------    ----   -----------

ADMIN$    *    C:Windows   Remote Admin

The following example creates a new SMB share with the name Share and the path C:Share:

PS C:> New-SmbShare –Path C:Share –Name Share

The following example removes the SMB share named Share:

PS C:> Remove-SmbShare –Name Share

SmbSession

These cmdlets are used for managing sessions that currently are connected to the SMB shares on the local or remote machine.

Usage examples

The following example enumerates the current SMB session connections on the local machine:

PS C: > Get-SmbSession

SessionId    ClientComputerName   ClientUserName  NumOpens

---------     ------------------    --------------   --------

377957122073   [fe80::952f:e0...  MDNAdministrator 3

The following example closes all the SMB session connections for the mdnadministrator user on the local machine:

PS C:> Get-SmbSession -ClientUserName mdnadministrator | Close-SmbSession

LEAVE A REPLY

Please enter your comment!
Please enter your name here