DAY- 9 : Advance Pipeline-2














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 shell can import the csv file correctly. Resulting in 2 objects with 4 properties each. The problem is that the -dept property won't line up with the -Department parameter of New-ADUser, the login property is meaningless, and we don't have the name and samaccountname parameters, which is required for creating new users with New-ADUser command.

import-csv .\newaduser.csv | new-aduser

How you can fix this? One way to do this is to open the csv file and edit the headers in the csv file manually. Another way is to fix this by the shell, look at the below example:






















Let explain what happened with select properties,


  • We used Select-object and its -Property parameter. We started by specifying the property *, which means " all of the existing properties." Notice that the * is followed by a comma, which means we are continuing the list of properties. 
  • We then created a hash table, which is the construct starting with @{ and ending with }. Hash tables consist of one or more key=value pair, and select-object has been programmed to look for some specific keys, which you will provide to it.
  • The first key Select-Object wants can be Name, N, Label, and the value for that key is the name of the property we want to create.
  • The second key that Select-Object needs can be either Expression or E, the value for this key is a script block, contained within { curly brackets }, you can use a special $_ placeholder to refer to the existing piped-in object.


Now we have done taking the content of CSV file and modified the file content for our use. Now our new output matches what New-ADuser wants to see, so we can now create new users by running this command

PS F:\> Import-csv .\newaduser.csv | select-object -property *,
>> @{name= 'samaccountname' ;expression={$_.login}},
>> @{ label= 'name' ;expression={$_.login}},
>> @{ n= 'Department' ;e={$_.dept}} | 
>>  New-ADUser

Parenthetical Commands

Sometimes, no matter how hard you try, you can't make pipeline input work. For example, consider Get-WmiObject command. You will learn more about it in the upcoming blogs, but right now, look at the help for its -ComputerName property as shown below.














This parameter doesn't accept computer names from the pipeline. So, how we can retrieve names from some text, csv file and input to the ComputerName parameter. The following will not work:

get-content .\computers.txt | get-wmiobject -class win32_bios

The string object produces by Get-Content won't match the -CompuerName parameter of Get-WmiObject. What we can do? Use parentheses:

Get-WmiObject -class win32_bios -computerName ( get-content .\computers.txt )

Think back to high school algebra class, and you'll recall that parentheses mean "do this first." That's what PowerShell does it run parenthetical command first. The result of this command, in this case, a bunch of string Objects-are fed to the parameter. Because -CompuerName happens to want a bunch od string objects, the command works.

The Parenthetical command trick is powerful because it doesn't rely on pipeline parameter binding at all. It takes objects and sticks them right into the parameter. But the technique doesn't work if your parenthetical command isn't generating the exact type of object that parameter expect.

Comments

Popular posts from this blog

DAY-1 : POWERSHELL…WHAT IT IS>>>AND WHY

Day-5: PowerShell Modules & PSSnapin

DAY-7: POWERSHELL OBJECT'S