basic n.
Download
Skip this Video
Loading SlideShow in 5 Seconds..
Scripts PowerPoint Presentation
Download Presentation
Scripts

Loading in 2 Seconds...

play fullscreen
1 / 20

Scripts - PowerPoint PPT Presentation


  • 137 Views
  • Uploaded on

Basic. Scripts. Basics. Powershell scripts are flat text files that end with “.ps1” (e.g. myscript.ps1) Similar to other scripting languages, whatever you can do on a command line, you can place in the script. . Invocation Rules.

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about 'Scripts' - kimberly


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript
basics
Basics
  • Powershell scripts are flat text files that end with “.ps1” (e.g. myscript.ps1)
  • Similar to other scripting languages, whatever you can do on a command line, you can place in the script.
invocation rules
Invocation Rules
  • Unlike cmd.exe, the current folder is not automatically searched – it must be in your path or explicitly noted on the command line (eg .\myscript )
  • If you double click on a Powershell script from Windows Explorer it will open up the script in notepad.exe by default.
  • You must explicitly set the execution policy…
execution policy
Execution Policy
  • Restricted– Only interactive commands will run at the console.
  • AllSigned– Scripts must have a certificate issued by a trusted party.
  • RemoteSigned– Scripts downloaded require a 3rd party certificate. Local scripts will run without a certificate.
  • Unrestricted - All scripts run without restriction.
setting execution policy
Setting Execution Policy
  • To set the privilege you must have administrator privileges.
create a script
Create a Script
  • In your working folder enter the following command:
enter this into notepad
Enter this into notepad

param([int]$height, [decimal]$shoesize, [int]$weight)

"Height: " + $height

"Shoe: " + $shoesize

"Weight: " + $weight

if (($height * $shoesize) -gt $weight)

{

Write-Host "You are either tall or you have big feet."

}

else

{

#try to be politically correct here

Write-Host "You have the perfect weight."

}

breaking this down
Breaking this down

param([int]$height, [decimal]$shoesize, [int]$weight)

"Height: " + $height

"Shoe: " + $shoesize

"Weight: " + $weight

if (($height * $shoesize) -gt $weight)

{

Write-Host "You are either tall or you have big feet."

}

else

{

#try to be politically correct here

Write-Host "You have the perfect weight."

}

Named parameter

With strong typing

Concatenation and Conversion

Comment

processing on the command line
Processing on the Command Line

#PSCount-Lines

#Demonstration code

begin {

$lineCount = 0

}

process {

$lineCount++

}

end {

Write-Host "There are " $lineCount " lines."

}

extending it further
Extending it further

begin {

$lineCount = 0

if ($args[0] -eq "-Double"){

$multiplier = 2 }

else {

$multiplier = 1 }

}

process {

$lineCount++

}

end {

Write-Host "There are " `

($multiplier * $lineCount) " lines."

}

loop and flow control
Loop and Flow Control
  • If…. elseif… else…
  • Switch…… default
  • ForEach(Foreach-Object)
  • For
  • While
  • Do….. While
  • Do…..Until
  • Break & Continue
code example
Code Example

$processes = get-process

if ($processes.Length -gt 50)

{

write-host "Processes are high: " + $processes.Length

}

$pnames = &{foreach ( $i in $processes ) {$i.Name}}

if ($pnames -contains "notepad")

{

Write-host "Notepad is running."

}

switch default
Switch…… default

Switch [-regex|-wildcard|-exact][-casesensitive] -file <filename> (< variable >)

{

< Pattern 1> { code1 }

< Pattern 2> { code2 }

< Pattern 3> { code3 } …

Default { coden }

}

try this
Try this…

function listLastEvent {

param ([int] $lastLimit)

Get-EventLog-Newest $lastLimit Security

Get-EventLog-Newest $lastLimit System

Get-EventLog-Newest $lastLimit Application

}

listLastEvent 3 | `

Select Username, Source, TimeGenerated, Message | `

ConvertTo-Html > /temp/myreport.html

Invoke-Item /temp/myreport.html

return values
Return values
  • Functions can return values
  • $LastExitCode will capture the exit value of the last executable from the command line
  • $? – Similar to Unix but it holds either $true or $false to indicate success of the last call
  • $error – A collection of all the error messages so far ($error[0] is the most recent)
one more snippet
One more snippet

param ($filename = "/temp/nemo")

dir $filename 2>$null

if ($?) { Write-Host "That worked."}

else {(Write-Host "Could not find: " $filename) }