Powershell

Windows PowerShell is a new task-based command-line shell and scripting language designed especially for system administration. Built on the .NET Framework, Windows PowerShell helps IT professionals and power users control and automate the administration of the Windows operating system and applications that run on Windows.

Built-in Windows PowerShell commands, called cmdlets, let you manage the computers in your enterprise from the command line. Windows PowerShell providers let you access data stores, such as the registry and certificate store, as easily as you access the file system. In addition, Windows PowerShell has a rich expression parser and a fully developed scripting language.

Much of what you see her is based on the Active Directory Module for windows powershell, Simply click the preceding link for a Google search on installation. So lets look at a few examples.

Example 1
List all domain controllers and their primary ip addresses.
Get-ADGroupMember 'Domain Controllers' | select name | ForEach-Object{ Get-ADComputer -Identity $_.name -Properties * | select name,ipv4address }

In the above, the Get-ADGroupMember retrieves the members of an Active Directory group. The Members can be users, other groups or computers. In this case, we retrieve the members of the 'Domain Contollers' group using Get-ADGroupMember 'Domain Controllers'. At this point our output would look something like the following. (Notice the name field)

distinguishedName : CN=server1,OU=Domain Controllers,DC=my,DC=domain,DC=com
name : server1
objectClass : computer
objectGUID : 123aaa00-1212-ffff-5555-23456eea3422
SamAccountName : LDNCORDC13$
SID : S-1-5-21-12625362536125-1767523625367125-7125356125361253-55555
distinguishedName : CN=server2,OU=Domain Controllers,DC=my,DC=domain,DC=com
name : server2
objectClass : computer
objectGUID : 123aaa00-1212-ffff-5555-23456eea3422
SamAccountName : LDNCORDC13$
SID : S-1-5-21-12625362536125-1767523625367125-7125356125361253-55555

Then comes the pipeline. In powershell, and other languages although more on the *nix side, you can send the output of one command to the input of the next command. You may have done this before by typing a file and the, using the pipeline character | send that data to findstr as in type c:\somefile.txt | findstr /i "mystring". Well, it works basically the same with powershell. Now, in powershell things are objects, rather than lines of text, but from this view the operation is the same.

So, in our command above, we send the output of Get-ADGroupMember 'Domain Controllers' to the select command and we select only the name. So, at this time, we have a list similar to the following.

name
----
server1
server2

From here we send this list to the ForEach-Object loop. The ForEach-Object will go through all of the items passed, in this case, the domain controller names, and we can take action using that name. In this case, we are going to look up the computer entry itself in Active Directory, we do this with the Get-ADComputer -Identity $_.name -Properties * section of the ForEach-Object. This is then sent to another command Get-ADComputer which retrieves a computer object from Active directory. We tell Get-ADComputer that we only want the computer object that matches our name field -Identity $_.name and that we want all of the objects properties returned -Properties *.

From here we again send the output, via the pipeline, (remember the pipeline character '|'), to another select command and select only the name and the ipv4 address select name,ipv4address. From all of this, we get a list similar to the following.

name              ipv4address
----              -----------
server1           111.111.111.111
server2           222.222.222.222

We could send this to other commands, output to a CSV or XML file or anything we desire. We are only limited by our imagination.

 

Leave a Reply