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
- Execution Policy (http://cleeit.blogspot.com/2014/01/MVA-powershell-m07-part1.html)
- What is it
- How to set it
- Signing scripts (http://cleeit.blogspot.com/2014/01/MVA-powershell-m07-part1.html)
- Create
- Load
- Sign
- Results
- Variables
- Defining
- Prompt for input (Read-Host)
- Output to screen (http://cleeit.blogspot.com/2014/01/MVA-powershell-m07-part3.html)
- Write-Host
- Write-Warning/Error/Debug
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
$myvarTo call variable we enter it's name
HelloWorldValue returned from call
Example of numerical
$myvar="1354"Sets HelloWorld into variable myvar
$myvarTo call variable we enter it's name
1354Value returned from call
Example of cmdlet:
$myvar=Get-Service bitsSets cmdlet Get-Service to variable myvar
$myvarCalls 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 bitsSets cmdlet Get-Service to variable myvar
$myvar | gmCalls 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 bitsSets cmdlet Get-Service to variable myvar
$myvar.statusCalls variable myvar which executes stored cmdlet Get-Service bits but only outputs Status
RunningResults 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}=4Sets value of 4 to variable This is a test
${This is a test}Call variable This is a test
4Results
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
No comments:
Post a Comment