In programming, objects are said to maintain state, and provide behavior. State is maintained by using elements called properties. Properties are variables that are specific to a given type of object. For instance, when you think of a car as an object type, you can identify certain properties that all cars have. That is, you can access all cars by using the notation object.property. For instance, the following code sets the make, model, and year properties of a variable named $myCar.
$myCar.Make = ‘Dodge’ $myCar.Model = ‘Durango’ $myCar.Year = 2006
Methods define object behavior. You can think of methods as actions that you can perform with a given object. For instance, you can turn a car on, turn it off, accelerate, brake, and turn. You can implement these actions in an object of type car as methods. Some methods require additional information to be executed. For instance, when you are accelerating a car, you might have to specify the acceleration rate, and the time you want to remain accelerating at that rate. Or, when you are turning, you might have to specify the direction and angle of the turn. You can access methods by using the notation object.method(args). For instance, the following code calls the TurnOn and
Accelerate methods of a car object stored in a variable named $myCar:
$myCar.TurnOn() $myCar.Accelerate(20)
Now, consider an Active Directory environment. Imagine that you want to write a script that shows the relative identifier (RID) master, Primary Domain Controller (PDC) emulator, infrastructure master, and domain mode of a given domain. Your script would look like the following:
> $myDomain = Get-ADDomain > $myDomain.RIDMaster LON-DC1.Adatum.com > $myDomain.PDCEmulator LON-DC1.Adatum.com > $myDomain.InfrastructureMaster LON-DC1.Adatum.com > $myDomain.DomainMode Windows208R2Domain
While typing the code above in Windows PowerShell ISE, you will notice that IntelliSense displays the list of properties and methods available for the $myDomain variable. If you point the mouse over any of the properties, you will notice that a pop-up window displays the data type for the property, its name, and access actions (get and set). These actions specify if you can read (get) or write (set) data to the property. For instance, if you hover your mouse on the PDCEmulator property, you will notice the pop-up window with the following description:
System.String PDCEmulator {get;}
You also can access methods or actions from a variable. For example, to determine the BaseType of $myDomain, you can use the GetType() method by running the following command:
> $myDomain.GetType()
IsPublic IsSerial Name BaseType --- --- ---- --- True False ADDomain Microsoft.ActiveDirectory.Management.ADPartition
MCT USE ONL
Y. STUDENT USE PROHIBITED
2-16 Managing Windows Server 2012 by Using Windows PowerShell
Note: BaseType of an object shows the name of the class (type of object) on which the variable is based.
When you use methods, you must follow the method name with ( ); otherwise Windows PowerShell will interpret it as a property.
You also can use variables in calculations. For example, to declare two variables, and then add them together, use the following commands:
> $a = 1 > $b = 2 > $a + $b 3
When you use variables in calculations, ensure that they are typed correctly, because typing them incorrectly could lead to unexpected results. For example, notice when variables are typed as string (text) data instead of numbers:
> $c = “3” > $d = “4” > $c + $d 34
Instead of adding the two values numerically, the variables are instead concatenated together. When you mix types together, there is more potential for unexpected results because Windows PowerShell will automatically cast or convert some data types. For example, see how the data is cast in the following example: > $a = 1 > $b = 2 > $c = “3” > $d = “4” > $a + $c 4 > $c + $a 31
In these examples, the type of the first variable is used to cast the other variables for the calculation. To better control how data is cast, you can specify the data type for each variable. To control how each variable is cast, see the following example:
> [string] $a + $c 13
> [int] $c + $a 2
Additional Reading: about_Variables http://go.microsoft.com/fwlink/?linkID=269668
Sometimes, your Windows PowerShell session may be opened for a long time, and you may be running several commands, and using dozens of variables. At any time, you can run the Get-Variables cmdlet to view a list of variables available in your session. If you are running low on memory, you can delete some of these variables by using the Remove-Variable cmdlet.
MCT USE ONL
Y. STUDENT USE PROHIBITED
Upgrading Your Skills to MCSA Windows Server® 2012 2-17
The Windows PowerShell Pipeline
Windows PowerShell is an object oriented script- ing environment. This means that the input and outputs of the cmdlets are objects that can be manipulated. In some instances, you may want to take the output of one cmdlet and pass it as input to another cmdlet for additional actions or filter- ing. For example, when you have to list all user accounts in your domain that are currently dis- abled, you may probably use the Get-ADUser cmdlet. This cmdlet requires at least one para- meter, the Filter parameter. Although, you can use this parameter for filtering, you can also pass the wildcard character, *, to this parameter as shown below: > Get-ADUser –Filter *You can use piping extensively in Windows PowerShell as it is in other shells. Windows PowerShell differs from typical shells because the data in the pipeline is an object instead of just simple text. Having an object in the pipeline enables you to easily access all the properties of the returned data. The data in the pipeline is assigned to a special variable named $_ that only exists while the pipeline is executing. For example, if you want to retrieve accounts that are disabled, you can use the Where-Object cmdlet to return only accounts that are disabled. To do this, run the following command:
Get-ADUser –Filter * | Where-Object {$_.Enabled –eq $false}
You can extend pipes beyond two cmdlets. For instance, you want to take the list of accounts retrieved above and enable them. You do this by simply extending the pipe to another cmdlet,
Enable-ADAccount, as shown in the following command:
Get-ADUser –Filter * | Where-Object {$_.Enabled –eq $false} | Enable-ADAccount
Note: This example is for teaching purposes only. It enables all of the disabled accounts in the domain, and should not be performed in a production environment because this may enable accounts that should remain disabled.