








































































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Created by Microsoft's "The Scripting Guy," Ed Wilson, this guide helps you understand how PowerShell can be used to manage the networking aspects of your server and client computers.
Typology: Study Guides, Projects, Research
1 / 80
This page cannot be seen from the preview
Don't miss anything!









































































ReadMe
About this Book
Introduction
Security Issues
Using PowerShell Cmdlets
Supplying Options for Cmdlets
Using Command-Line Utilities
Working with Help Options
Working with Modules
Working with Network Adapters
Identifying Adapters
Enabling and Disabling Adapters
Renaming Adapters
Finding Connected Adapters
Adapter Power Settings
Getting Network Statistics
Resources
By Ed Wilson
Cover design by Nathan Vonnahme
Created by Microsoft's "The Scripting Guy," Ed Wilson, this guide helps you understand how PowerShell can be used to manage the networking aspects of your server and client computers.
This guide is released under the Creative Commons Attribution-NoDerivs 3.0 Unported License. The authors encourage you to redistribute this file as widely as possible, but ask that you do not modify the document.
Was this book helpful? The author(s) kindly ask(s) that you make a tax-deductible (in the US; check your laws if you live elsewhere) donation of any amount to The DevOps Collective to support their ongoing work.
Check for Updates! Our ebooks are often updated with new and corrected content. We make them available in three ways:
Our main, authoritative GitHub organization, with a repo for each book. Visit https://github.com/devops-collective-inc/ Our GitBook page, where you can browse books online, or download as PDF, EPUB, or MOBI. Using the online reader, you can link to specific chapters. Visit https://www.gitbook.com/@devopscollective On LeanPub, where you can download as PDF, EPUB, or MOBI (login required), and "purchase" the books to make a donation to DevOps Collective. You can also choose to be notified of updates. Visit https://leanpub.com/u/devopscollective
GitBook and LeanPub have slightly different PDF formatting output, so you can choose the one you prefer. LeanPub can also notify you when we push updates. Our main GitHub repo is authoritative; repositories on other sites are usually just mirrors used for the publishing process. GitBook will usually contain our latest version, including not-yet-finished bits; LeanPub always contains the most recent "public release" of any book.
About this Book 4
Windows PowerShell is not new technology. Windows PowerShell 4.0 ships in Windows 8. and in Windows Server 2012 R2, it has therefore been around for a while. Windows PowerShell is an essential admin tool designed specifically for Windows administration. By learning to use Windows PowerShell, network administrators quickly gain access to information from Windows Management Instrumentation, Active Directory and other essential sources of information. Additionally, Microsoft added Windows PowerShell support to the Common Criteria requirements for shipping enterprise applications. Therefore, to manage Microsoft Exchange, Azure, SQL Server, and others one needs to know and to understand how to use Windows PowerShell. In the networking world, this knowledge is also a requirement for managing DNS, DHCP, Network Adapters, and other components.
Windows PowerShell comes in two flavors - the first is an interactive console (sort of like a KORN or a BASH console in the UNIX world) built into the Windows command prompt. The Windows PowerShell console makes it simple to type short commands and to receive sorted, filtered, formatted results. These results easily display to the console, but can redirect to XML, CSV, or text files. The Windows PowerShell console offers several advantages such as speed, low memory overhead, and a comprehensive transcription service that records all commands and command output.
There is also the Windows PowerShell ISE. The Windows PowerShell ISE is an Integrated Scripting Environment, but this does not mean you must use it to write scripts. In fact, many Windows PowerShell users like to write their code in the Windows PowerShell ISE to take advantage of the color syntax-highlighting, drop down lists, and automatic parameter revelation features. In addition, the Windows PowerShell ISE has a feature, called the _Show Command Add-On _that permits using a mouse to create Windows PowerShell commands from a graphical environment. Once created, the command either runs directly, or adds to the script pane (the choice is up to you).
Introduction 5
On Windows Server 2012 (and on Windows Server 2012 R2), it is not necessary to go through the Start screen / Search routine because an icon for the Windows PowerShell console exists by default on the taskbar of the desktop.
NOTE : The Windows PowerShell ISE (the script editor) does not exist by default on Windows Server 2012 and Windows Server 2012 R2. You add the Windows PowerShell ISE as a feature.
Introduction 7
There are two ways of launching Windows PowerShell - as an administrator and as a normal user. It is a best practice when starting Windows PowerShell to start it with minimum rights. On Windows 8 (and on Windows 7) this means simply clicking on the Windows PowerShell icon. It opens as a non-elevated user (even if you are logged on with Administrative rights). On Windows Server 2012, Windows PowerShell automatically launches with the rights of the current user and therefore if you are logged on as a Domain Administrator, the Windows PowerShell console launches with Domain Administrator rights.
Because Windows PowerShell adheres to Windows security constraints, a user of Windows PowerShell cannot do anything that the user account does not have permission to do. Therefore, if you are a non-elevated normal user, you will not have rights to do things like install printer drivers, read from the Security log, or change system time.
Even if you are an administrator on the local Windows 8 (or Windows 7) desktop machine and you do not launch Windows PowerShell with admin rights, you will get errors when attempting to do things like see the configuration of your disk drives. This command and associated error appears here.
PS C:> get-disk get-disk : Access to a CIM resource was not available to the client. At line:1 char:
TIP : There is an inconsistency with errors arising when attempting to run cmdlets that require elevated rights. For example, when inside a non-elevated Windows PowerShell console, the error from Get-Disk is _Access to a CIM resource was not available to the client. _The error from Stop-Service is _Cannot open xxx service on computer. _While the Get-VM cmdlets simply returns no information (an no error). Therefore, as a first step in troubleshooting, check for console rights.
Security Issues 8
PS C:> get-disk
Number Friendly Name OperationalS Total Size Partition tatus Style
0 INTEL SSDSA2BW160G3L Online 149.05 GB MBR
Security Issues 10
PowerShell cmdlets all work in a similar fashion. This simplifies their use. All Windows PowerShell cmdlets have a two-part name. The first part is a verb (not always strictly a grammatical verb however). The verb indicates the action for the command to take. Examples of verbs include Get, Set, Add, Remove, or Format. The noun is the thing to which the action will apply. Examples of nouns include Process, Service, Disk, or NetAdapter. A dash combines the verb with the noun to complete the Windows PowerShell command. Windows PowerShell commands, named cmdlets (pronounced command let), because they behave like small commands or programs are used standalone, or pieced together via a mechanism called the pipeline (refer to chapter two for the use of the pipeline ).
The most common verb - Get
Out of nearly 2,000 cmdlets (and functions) on Windows 8, over 25 percent of them use the verb Get. The verb Get retrieves information. The Noun portion of the cmdlet specifies the information retrieved. To obtain information about the processes on your system, open the Windows PowerShell console by either clicking on the Windows PowerShell icon on the task bar (or typing PowerShell on the start screen of Windows 8 to bring up the search results for Windows PowerShell (as illustrated earlier)). Once the Windows PowerShell console appears, run the Get-Process cmdlet. To do this, use the Windows PowerShell Tab Completion feature to complete the cmdlet name. One the cmdlet name appears, press the key to cause the command to execute.
NOTE : The Windows PowerShell Tab Completion feature is a great time saver. It not only saves time (by reducing the need for typing) but it also helps to ensure accuracy, because Tab Completion accurately resolves cmdlet names - it is sort of like a spell checker for cmdlet names. For example, attempting to type a cmdlet name such as Get- NetAdapterEncapsulatedPacketTaskOffload accurately (for me anyway) could be an exercise in frustration. But using tab completion, I only have to type Get-Net and I hit the key about six times and the correctly spelled cmdlet name appears in the Windows PowerShell console. Learning how to quickly, and efficiently use the tab completion is one of the keys to success in using Windows PowerShell.
Using PowerShell Cmdlets 11
Get-Hotf +
The command, and the output associated with the command appear here.
To find information about services on the system, use the Get-Service cmdlet. Once again, it is not necessary to type the entire command. The following command uses Tab Expansion to complete the Get-Service command and to execute it.
Get-Servi +
NOTE : The efficiency of Tab Expansion depends upon the number of cmdlets, functions, or modules installed on the computer. As more commands become available, the efficiency of Tab Expansion reduces correspondingly.
The following (truncated) output appears following the Get-Service cmdlet.
PS C:> Get-Service
Status Name DisplayName
Running AdobeActiveFile... Adobe Active File Monitor V Stopped AeLookupSvc Application Experience Stopped ALG Application Layer Gateway Service Stopped AllUserInstallA... Windows All-User Install Agent
Using PowerShell Cmdlets 13
To find information about network adapters on your Windows 8 (or Windows Server 2012) machine, use the Get-NetAdapter cmdlet. Using Tab Expansion, type the following:
Get-NetA +
The command and associated output appear here.
PS C:> Get-NetAdapter
Name InterfaceDescription ifIndex Status
Network Bridge Microsoft Network Adapter Multiplexo... 29 Up Ethernet Intel(R) 82579LM Gigabit Network Con... 13 Not Pre... vEthernet (WirelessSwi... Hyper-V Virtual Ethernet Adapter #4 31 Up vEthernet (External Sw... Hyper-V Virtual Ethernet Adapter #3 23 Not Pre... vEthernet (InternalSwi... Hyper-V Virtual Ethernet Adapter #2 19 Up Bluetooth Network Conn... Bluetooth Device (Personal Area Netw... 15 Disconn... Wi-Fi Intel(R) Centrino(R) Ultimate-N 6300... 12 Up
If you want to see the network connection profile that Windows 8 (or Windows Server 2012) detected for each interface, use the Get-NetConnectionProfile cmdlet. To run this command, use the following command with Tab Expansion.
Get-NetC +
The command and associated output appear here.
Using PowerShell Cmdlets 14
PS C:> Get-Culture
LCID Name DisplayName
1033 en-US English (United States)
The second culture related grouping of information is the current user interface (UI) settings for Windows. The UI culture settings determine which text strings appear in user interface elements such as menus and error messages. To determine the current UI culture settings that are active use the Get-UICulture cmdlet. Using Tab Expansion to call the Get-UICulture cmdlet, type the following:
Get-Ui +
The command and output associated from the command appears here.
PS C:> Get-UICulture
LCID Name DisplayName
1033 en-US English (United States)
NOTE : On my laptop, both the current culture and the current UI culture are the same. This is not always the case, and at times, I have seen machines become rather confused when the user interface is set for a localized language, and yet the computer itself was still set for US English (this is especially problematic when using virtual machines created in other countries. In these cases, even a simple task like typing in a password becomes very frustrating. To fix these types of situations you can use the Set-Culture cmdlet.
To find the current date or time on the local computer, use the Get-Date cmdlet. When typing the Get-Date cmdlet name in the Windows PowerShell console tab expansion does not help too much. This is because there are 15 cmdlets (on my laptop) that have a cmdlet name that begins with the letters Get-Da (this includes all of the Direct Access cmdlets as well as the Remote Access cmdlets). Therefore using Tab Expansion (on my laptop anyway) to get the date requires me to type the following:
Get-Dat +
Using PowerShell Cmdlets 16
The above command syntax is just the same number of letters to type as doing the following:
Get-Date +
The following illustrates the command and the output associated with the command.
PS C:> Get-Date
Tuesday, November 20, 2012 9:54:21 AM
Windows 2.0 introduced the Get-Random cmdlet, and when I saw it I was not too impressed. The reason was that I already knew how to generate a random number. Using the .NET Framework System.Random class, all I needed to do was create a new instance of the System.Random object, and call the _next _method. This appears here.
PS C:> (New-Object system.random).next() 225513766
Needless to say, I did not create all that many random numbers. I mean, who wants to do all that typing. But once I had the Get-Random cmdlet, I actually began using random numbers for all sorts of things. Some of the things I have used the Get-Random cmdlet to do appear in the following list.
Pick prize winners for the Scripting Games Pick prize winners for Windows PowerShell user group meetings
To connect to remote servers in a random fashion for load balancing purposes To create random folder names To create temporary users in active directory with random names
To wait a random amount of time prior to starting or stopping processes and services (great for performance testing)
The Get-Random cmdlet has turned out to be one of the more useful cmdlets. To generate a random number in the Windows PowerShell console using Tab Expansion type the following on the first line in the console:
Using PowerShell Cmdlets 17
The easiest Windows PowerShell cmdlets to use require no options. But unfortunately, that is only a fraction of the total number of cmdlets (and functions) available in Windows PowerShell 4.0 as it exists on either Windows 8.1 or Windows Server 2012 R2. Fortunately, the same Tab Expansion technique used to create the cmdlet names on the Windows PowerShell console, works with parameters as well.
When working with Windows PowerShell cmdlets, often the cmdlet only requires a single parameter to filter out the results. If a parameter is the default parameter, you do not have to specify the parameter name - you can use the parameter positionally. This means that the first value appearing after the cmdlet name, is assumed to be a value for the default (or position 1) parameter. On the other hand, if a parameter is a named parameter the parameter name (or parameter alias or partial parameter name) is always required when using the parameter.
For example to find all of the _update _hotfixes, use the Get-HotFix cmdlet with the - Description parameter and supply a value of _update _to the -Description parameter. This is actually easier than it sounds. Once you type Get-Hot and press the key you have the Get- Hotfix portion of the command. Then a space and -D completes the Get-HotFix -Description portion of the command. Now you need to type Update and press. With a little practice, using Tab Expansion becomes second nature. You only need to type the following:
Get-Hot +
The completed command and the output associated with the command appear in the figure that follows.
Supplying Options for Cmdlets 19
If you attempt to find only update types of hotfixes by supplying the value _update _in the first position, an error raises. The offending command, and associated error, appears here.
PS C:> Get-HotFix update Get-HotFix : Cannot find the requested hotfix on the 'localhost' computer. Verify the input and run the command again. At line:1 char:
The error, while not really clear, seems to indicate that the Get-HotFix cmdlet attempts to find a hotfix named _update. _This is, in fact, the attempted behavior. The help file information for the Get-HotFix cmdlet reveals that -ID is position 1. This appears here.
-Id <String[]> Gets only hotfixes with the specified hotfix IDs. The default is all hotfixes on the computer.
Required? false Position? 1 Default value All hotfixes Accept pipeline input? false Accept wildcard characters? False
Well, what about using the -Description parameter, you may ask? The help file tells that the - Description parameter is a named parameter. This means you can only use the -Description parameter if you specify the parameter name as was accomplished earlier in this section. Here is the applicable portion of the help file for the -Description parameter.
Supplying Options for Cmdlets 20