Book Image

Instant Windows PowerShell Guide

By : Harshul Patel
Book Image

Instant Windows PowerShell Guide

By: Harshul Patel

Overview of this book

Windows PowerShell has become a booming scripting language over the last couple of years. It has extensive support with an ample number of vendor products, providing a standardized platform for automation and administration. It has massive support for all Microsoft products which creates a layer that can easily automate everything. In the latest version, the PowerShell team has introduced much more functionality with thousands of CMDLETs, part of various modules.This book is a quick reference guide to enable you to get the most out of the latest Windows PowerShell techniques. In this book, you will find new enhancements in the latest version of PowerShell with some helpful examples. This book enables you to quickly move from older versions of PowerShell to Version 3.0 and Version 4.0.This practical, example-oriented book helps you to overcome the difficulty of using and discovering CMDLETs by providing precise information about everything that has been newly introduced in the latest version of Windows PowerShell. It also focuses on the new configuration management system with the help of DSC as a new feature of Windows PowerShell v4.0.You will learn how to use the newly introduced CMDLETs and parameters to perform daily routine tasks. You will also learn how to administer the servers remotely and maintain persistent sessions to provide continuity. You will gain an insight into writing efficient scripts by using various parameters, snippets, and workflows to gain more productivity. You will also be introduced to various modules like CimCmdlets, PSScheduledJob, PSDesiredStateConfiguration, and so on in order to enhance your scripts with the latest instrumentation. Finally this book will make you aware of the capabilities of PowerShell v4.0 and how to fully leverage the functionality introduced in the new version.
Table of Contents (7 chapters)

Administer the system (Intermediate)


So far, we have covered changes to the settings in Windows PowerShell Version 3.0 in a general way. Now, let's try to capture how Version 3.0 is useful to the system administrator regarding the ease of working with the console. This recipe consists of multiple subrecipes.

Getting ready

The Add-Computer CMDLET is used for adding a local computer to a domain or workgroup.

How to do it...

Try executing the following code sequence:

  1. The following command statement adds the Member01 server to PSDomain using the PSDomain\PSAdmin credential and restarts the machine once added. It also uses the Force parameter, so it doesn't ask for confirmation.

    PS C:\>Add-Computer -ComputerName Member01 -LocalCredential Member01\Admin01 -DomainName PSDomain -Credential PSDomain\PSAdmin -Restart –Force
    
  2. The following command statement adds the Member01 and Member02 servers to PSDomain using the PSDomain\PSAdmin credential and restarts the machines once added:

    PS C:\>Add-Computer -ComputerName Member01, Member02 -Domain PSDomain -LocalCredential TestDomain\User01 -UnjoinDomainCredential TestDomain\Admin01 -Credential PSDomain\PSAdmin –Restart
    
  3. The following command statement adds the Member01 server to PSDomain using the PSDomain\PSAdmin credential, changes its name to NewMember01, and restarts the machine once added:

    PS C:\>Add-Computer -ComputerName Member01 -Domain PSDomain -NewName NewMember01 -Credential PSDomain\PSAdmin –Restart
    
  4. The following command statement adds the Member02 server to PSDomain using the PSDomain\PSAdmin credential and moves it to the PSOU organizational unit after adding to the domain:

    PS C:\>Add-Computer -ComputerName Member02 -Domain PSDomain –OUPath "OU=PSOU,DC=PSDomain,DC=com" -Credential PSDomain\PSAdmin 
    
  5. The following command statement adds the Member01 server to a workgroup named TestWorkgroup:

    PS C:\>Add-Computer -ComputerName Member01 -WorkgroupName TestWorkgroup
    

How it works…

Given below are the additional parameter names introduced with Add-Computer in Version 3.0:

  • -ComputerName: It specifies the names of the computers, separated by commas, to add them into a domain or workgroup. By default, it takes the local computer name.

  • -Force: It avoids prompt confirmation. By default, it asks for confirmation for each server that we add to the domain or workgroup. We can use the Force parameter name to overcome that.

  • -LocalCredential: It is not a mandatory parameter, but it explicitly provides credentials to connect to the servers specified by the ComputerName parameter. Likewise, the Credential parameter is used to provide valid credentials to join to the domain.

  • -NewName: This parameter provides a new name to a computer in the new domain. It only works when one computer name is supplied to be added or removed.

  • -Restart: It restarts the computers that have been added to the domain or workgroup. Generally, it requires a restart once after joining into a domain and the Restart parameter does this job well.

  • -UnjoinDomainCredential: It passes the credentials of the user account that has permission to unjoin the computer from the current domain. This parameter is useful when we are moving from one domain to another. Likewise, use the Credential and LocalCredential parameters to provide credentials to join the domain and connect to other computers, respectively.

  • -WorkgroupName: This parameter has been introduced with the release of Windows PowerShell 4.0. It specifies the name of the workgroup to which the computers are added. This parameter is only important while adding the computers to a workgroup. Its default value is WORKGROUP.

There's more…

The Remove-Computer CMDLET has the same set of enhancements as Add-Computer. The changes are identical with respect to both the CMDLETs.

Rename-Computer

The Rename-Computer CMDLET is introduced in Windows PowerShell v3.0. It requires the NewName parameter of the server and the DomainCredential parameter to put these changes in effect.

For example:

PS C:\>Rename-Computer -NewName NewMember01 -DomainCredential PSDomain\PSAdmin –Restart

The preceding command statement changes the name of the local computer to NewMember01 using the domain admin credentials PSDomain\PSAdmin. It prompts for the password, changes the name of the local computer, and restarts it to put the changes in effect.

The Rename-Computer CMDLET also supports parameters such as ComputerName, Force, LocalCredential, and Passthru.

Operating the control panel from the console

Two handy CMDLETs introduced with Windows PowerShell version 3.0 are: Get-ControlPanelItem and Show-ControlPanelItem.

PS C :\> Get-ControlPanelItem -Name *Device* | Format-List

The previous command displays all the control panel items containing the Device keyword. Format-List shows information in a list format. The output is shown as follows:

Name          : Device Manager
CanonicalName : Microsoft.DeviceManager
Category      : {All Control Panel Items}
Description   : View and update your hardware's settings and driver software.
Name          : Devices and Printers
CanonicalName : Microsoft.DevicesAndPrinters
Category      : {Hardware and Sound}
Description   : View and manage devices, printers, and print jobs
PS C :\> Get-ControlPanelItem -Name *Printers* | Show-ControlPanelItem

The preceding command statement gets the control panel items that are related to printers and, further, Show-ControlPanelItem opens the Device and Printers window.

Test-Connection

In Version 3, a new parameter name Source is introduced with the Test-Connection CMDLET. If there is a need to check the connectivity of a single machine from multiple locations, the Source parameter is very handy to use.

PS C:\>Test-Connection –Source Member01, Member02 -ComputerName DC01 -Credential PSDomain\PSAdmin

The preceding command statement checks the connectivity for the server DC01 from two servers named Member01 and Member02.

Test-NetConnection

The Test-NetConnection CMDLET is introduced in Windows PowerShell 4.0. It shows the diagnostic information of a connection. It shows various results in the output, for example, the DNS lookup, traceroute information, and so on.

The following are the various parameters that come along with this CMDLET:

  • -CommonTCPPort <String>: It defines the common service TCP port number. The values are: HTTP, PING, RDP, and SMB.

  • -ComputerName <String>: It specifies the DNS name or IP address of the target machine.

  • -Hops <Int32>: It defines the number of hops of traceroute.

  • -InformationLevel <String>: It provides the level of information. The values are: Detailed and Quiet. The Quiet value returns a Boolean value, whereas Detailed gives you in-depth information about a connection.

  • -Port <Int32>: It specifies the TCP port number of a target machine.

  • -TraceRoute: It tests the connectivity of the machine to a remote machine.

For example:

PS C :\> Test-NetConnection –ComputerName PSTest.PSLab.com -Port 8080 -InformationLevel Detailed

The preceding command statement checks the connectivity of PSTest.PSLab.com with respect to the port number 8080 and shows detailed information about the established connection.