Here we are module 7 only two more. This is a short segment but lots of information. To keep things short I have split it among three posts. Check the Overview to see what is covered under this post.
To start your own learning check out Microsoft Virtual Academy:
(
http://www.microsoftvirtualacademy.com/training-courses/getting-started-with-powershell-3-0-jump-start?o=3276#?fbid=aVs9FfAH2DJ)
(rewritten based on my notes available here:
https://drive.google.com/file/d/0B1fwreWrAZioQ28xTmx0d29HUzQ/edit?usp=sharing)
Module 7: "Getting prepared for automation" consists of 2 video segments totaling ≈ 26 minutes along with a PowerPoint of 12 slides.
Overview
Variables
Variables are a huge part the power o f PowerShell. Variables can store not just numerical or string for output but also cmdlets for recall later, executed against, or even run method if supported. A variable is defined by $ and can be recalled by $name. Variables are not saved after shell or session is closed. Check out the slides for how to use variables and quotes.
Example of string for output:
$myvar="HelloWorld"
Sets HelloWorld into variable myvar
$myvar
To call variable we enter it's name
HelloWorld
Value returned from call
Example of numerical
$myvar="1354"
Sets HelloWorld into variable myvar
$myvar
To call variable we enter it's name
1354
Value returned from call
Example of cmdlet:
$myvar=Get-Service bits
Sets cmdlet Get-Service to variable myvar
$myvar
Calls variable myvar which executes stored cmdlet Get-Service bits
Status Name DisplayName
------ ---- -----------
Running bits Background Intelligent Transfer Ser...
Results of cmdlet
Example of cmdlet executed against:
$myvar=Get-Service bits
Sets cmdlet Get-Service to variable myvar
$myvar | gm
Calls variable myvar which executes stored cmdlet Get-Service bits and runs through pipe get-member
TypeName: System.ServiceProcess.ServiceController
Name MemberType Definition
---- ---------- ----------
Name AliasProperty Name = ServiceName
RequiredServices AliasProperty RequiredServices = ServicesDependedOn
Disposed Event System.EventHandler Disposed(System.Object, System.EventArgs)
Close Method void Close()
Continue Method void Continue()
....
Results of cmdlet
Example of cmdlet method:
$myvar=Get-Service bits
Sets cmdlet Get-Service to variable myvar
$myvar.status
Calls variable myvar which executes stored cmdlet Get-Service bits but only outputs Status
Running
Results of cmdlet
Variables are also not limited to a single word or continues string for a name. By using the brackets {} you can use phrases as the variable name.
${This is a test}=4
Sets value of 4 to variable This is a test
${This is a test}
Call variable This is a test
4
Results
Read-Host
Now it is all nice that we can static set a variable but what if I want to ask the user for information (name/amount/etc). To do this we use:
Read-Host "Message here"
To save this to a variable we just append $
var = to the front of string as below:
$myvar = Read-Host "Enter Computer name:"
Prompts the user to enter a value and saves it to variable myvar
Check out the rest of this segment:
Part 1:
http://cleeit.blogspot.com/2014/01/MVA-powershell-m07-part1.html
Part 3:
http://cleeit.blogspot.com/2014/01/MVA-powershell-m07-part3.html