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:
The following command statement adds the
Member01
server toPSDomain
using thePSDomain\PSAdmin
credential and restarts the machine once added. It also uses theForce
parameter, so it doesn't ask for confirmation.PS C:\>Add-Computer -ComputerName Member01 -LocalCredential Member01\Admin01 -DomainName PSDomain -Credential PSDomain\PSAdmin -Restart –Force
The following command statement adds the
Member01
andMember02
servers toPSDomain
using thePSDomain\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
The following command statement adds the
Member01
server toPSDomain
using thePSDomain\PSAdmin
credential, changes its name toNewMember01
, and restarts the machine once added:PS C:\>Add-Computer -ComputerName Member01 -Domain PSDomain -NewName NewMember01 -Credential PSDomain\PSAdmin –Restart
The following command statement adds the
Member02
server toPSDomain
using thePSDomain\PSAdmin
credential and moves it to thePSOU
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
The following command statement adds the
Member01
server to a workgroup namedTestWorkgroup
: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 theForce
parameter name to overcome that.-LocalCredential
: It is not a mandatory parameter, but it explicitly provides credentials to connect to the servers specified by theComputerName
parameter. Likewise, theCredential
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 theRestart
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 theCredential
andLocalCredential
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 isWORKGROUP
.
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
andQuiet
. TheQuiet
value returns a Boolean value, whereasDetailed
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.