Greg Szabo philosobear com gregphilosobear com MONTREAL POWERSHELL
Greg Szabo philosobear. com greg@philosobear. com MONTREAL POWERSHELL #2 Creating Scripts and Functions
AGENDA • Introduction • Execution policy • My first Power. Shell script and function • Functions • Input • Positional and named inputs • The “param” keyword • Variable types • Result • The “return” keyword • The “echo” alias • More about functions
EXECUTION POLICY • Set-Execution. Policy • Enables you to determine which Windows Power. Shell scripts (if any) will be allowed to run on your computer. • • Restricted (default in Windows 8, 8. 1 and 2012) All. Signed Remote. Signed (default in Windows 2012 R 2) Unrestricted Get-Help about_Execution_Policies #To disable restrictions and warnings: Set-Execution. Policy By. Pass
MY FIRST POWERSHELL SCRIPT • Write a script that gives you all new files for a folder • Hint: dir c: Windows | Where-Object { $_. Last. Write. Time. Add. Days(7) -ge (Get-date)}
FIRST POWERSHELL FUNCTION • The polite admin function Get-Greeting { return “Hello world” } • Convert the script on the previous slide to a function • Add the option to change the folder • Add the option to look “n” days back
FUNCTIONS - BASICS • The input • • Positional and named inputs Variable types The “param” keyword The function’s scope • The result • The “return” placebo • The “echo” placebo and Out-Host
FUNCTIONS – ADVANCED DEMO • More about functions • Begin/Process/End • Cmdlet. Binding • Parameter sets
FURTHER RESOURCES Greg Szabo philosobear. com greg@philosobear. com • Get-Help about_Functions_* • https: //technet. microsoft. com/en-us/library/hh 847829. aspx • Get-Help about_Scopes • https: //technet. microsoft. com/en-us/library/hh 847849. aspx • Verb naming rules • https: //technet. microsoft. com/en-us/library/ms 714428(v=vs. 85). aspx • Hey, Scripting Guy! Blog • http: //blogs. technet. com/b/heyscriptingguy/ • About Power. Shell streams • http: //blogs. technet. com/b/heyscriptingguy/archive/2014/03/30/understandingstreams-redirection-and-write-host-in-powershell. aspx
- Slides: 8