Posts

DAY- 9 : Advance Pipeline-2

Image
Custom Properties It's pretty easy to make property and parameter names line up when you're creating the input from scratch. Things get tougher when you're forced to deal with the objects that are created for you or data that's being produced by someone else. For example, we're going to introduce a new command that you might not have access to New-ADUser. It is a part of Active Directory module. New-ADUser has a number of parameters, each designed to accept information about a new Active Directory user. Here are some examples: 1. Name( this is mandatory ) 2. Samaccountname 3. Department 4. City 5. Title For this example, we'll again assume you're getting a csv file, but it's coming from your company's HR department. You've given them your desired file format dozen of time but they persist in giving you something that's close but not quite right as shown below. As you can see in above fig. that

DAY 8 - Advance Pipeline

Image
At this, point you've learned to pretty effective with PowerShell's Pipeline. Running Pipeline commands is powerful, accomplishing in one line what used to take several lines of the script. But if I said you can even do better than this, in this blog we'll dig deep into the pipeline and discover its powerful capabilities.  How PowerShell Passes Data Down to Pipeline Whenever you string two commands in a pipeline, PowerShell has to figure out how to get the output of the first command to the input of the second command. Let say the first command as command A and the second command as command B. A will produce something and B, which needs to accept A's output as input and then do its own thing. Command A | Command B For example, suppose you have a text file that contains one computer name on each line. You might want to use those names as the input to some command, telling that command which computers you want to run against. Get-cont

DAY-7: POWERSHELL OBJECT'S

Image
Sorting Objects Most PowerShell Cmdlets produce objects in a deterministic fashion, which means that they tend to produce objects in the same order every time you run the command. Both service and process, for example, are listed in alphabetical order by name. What if we want to change that. For example, we want to display a list of processes, which is the biggest consumers of Virtual Memory (VM) at the top of the list. For this, we need to re-order that list of objects based on VM property. PowerShell provides a simple Cmdlet, Sort-Object, which does exactly that: Get-Process | Sort-Object -property VM We're hoping that you'll follow this blog and try to run all the given commands to test it yourself. That command isn't exactly what we wanted. It did a sort on VM, but it did so in ascending order, with the largest values at the bottom of the list. By reading help for the sort-object, we see that it has a -descending parameter

DAY-6: OBJECT'S IN POWERSHELL

Image
Objects: Data By Another Name Use of Objects in PowerShell sometimes can be one of the most confusing elements but at the same time, it's the also one of the most critical concepts, affecting everything you do in the shell. What are the Objects? Run a command Get-Process in PowerShell. You Should see a table with several columns, but those columns barely have the complete information about the processes. Each process object also has a machine name, main window handle, maximum working set, exit code and time, and a great deal of information. In fact, you'll find more than 60 pieces of information associated with a process. Now the question is, why did PowerShell show so few of them? The simple fact is that most of the things PowerShell can access offer more information than what will comfortably fit on the screen. When you run a command, such as Get-Process, Get-Service, or anything, PowerShell constructs a table containing all the inf

Day-5: PowerShell Modules & PSSnapin

Image
Introduction One of the PowerShell's primary Strengths is its extensibility. As Microsoft continues to invest in PowerShell, it develops more and more commands for products like Exchange, SharePoint Server, System Center Family, SQL Server and so on. Typically when you install these product's Management tools, you also get a graphical management console of some kind and one or more extensions for Windows PowerShell. How IT Works You all are familiar with the graphical Microsoft Management Console (MMC), same management team within Microsoft develops both the MMC and PowerShell Modules (or Snap-in) for particular MMC. When you open a new, blank MMC console you add Snap-in like DNS, Active Directory Users and Computer.    PowerShell works in almost exactly the same way. Install the management tools for a given product, doing so will give you any related PowerShell extensions, and it may even create a product specific management she. These Product s

DAY-4: PowerShell Pipeline and Exporting Data

Image
Pipeline - Connecting commands   PowerShell connects commands to each other using something called a Pipeline. The pipeline provides a way for one command to pass, or pipe, its output to another command, allowing that second command to have something to work with. The concept of joining commands is not limited to PowerShell, but PowerShell takes the same piping to a greater extend for better effect.  Exporting Output to Files 1. Get-Process 2. Get-service When we run above commands, a table with several columns of information appears on the screen.  It's great to have information on the screen, but that isn't all you might want to do with the information. For example, if you want to export the information into a CSV file that could be read into an application like Microsoft excel.  Exporting to CSV Exporting to a file is where the pipeline and a second command come in handy. Get-Process | Export-csv C:\procs.csv We

DAY-3: Running PowerShell Cmdlets

Image
PowerShell: Running Commands Powershell, as its name implies, is a shell. It's similar to Cmd.exe command line that you probably used previously. It also has a strong resemblance to the Unix shells, like Bash or even original Unix Bourne shell. Only the difference come with PowerShell commands with all the other shells is that PowerShell has a unique Anatomy of the Cmdlets structure, which is missing in other shells.This structure is very easy to understand and make use while running Cmdlets. Now I will make you understand the anatomy of the Cmdlets in PowerShell. The Anatomy of PowerShell Command The figure shows the basic anatomy of a complex PowerShell command. I have tried to use a complex command so that you can all of the different things that might show up. Now we will tell you all few rules for writing PowerShell commands. 1. Cmdlets Naming Convention - Microsoft has established a naming convention for Cmdlets. The rule is this: Names start with a