Powershell‎ > ‎Training Notes‎ > ‎

Day 2

Review yesterday

[string]::Join($<array>,<value>)

 

<object>.<member>

 

Object is an instance of a class

Gain refference to an object in multiple ways

$<variable1> = Get-Process

$iCount = $<variable1>.count

 

$var = new-object "<class>"

instantiating a class

 

2 parsing methods

expression mode

command mode

 

Export-Console

get-help export-console -full | more # example 3

add-pssnapin newPSSnapin
export-console -path NewPSSnapinConsole.psc1
powershell.exe -PsConsoleFile NewPsSnapinConsole.psc1

powershell -?

PowerShell -PSConsoleFile SqlSnapIn.Psc1


Get-PSSnapin
Get-PSSnapin -registered
 
Get-Help Get-P*
Get-Help About 

 

get-help set-alias -full |more       # Example 5
function CD32 {set-location c:\windows\system32}
set-alias go cd32
Set-Alias -name "gh" -value "Get-Help" -description "Custom Alias" -force -passThru
 Find custom alias

 

Get-Alias | Where-Object {$_.Description -eq "Custom Alias"} | Select-Object name, definition,description |more
function ev1 {Get-EventLog -LogName $args[0] -Newest $args[1]}
Get-Command -commandtype function

new-alias -Name events -Value ev1 -description "Custom Alias" -passthru
events application 10
Notes

 Page 80 of Powershell step by step list of special variables

$args is used by functions to create positional parameters

Some commands modify the object like format commands (format-table) you need to do any filters etc before formating.

 Dynamic parmeters

Get-ChildItem cert:
Get-ChildItem cert: -recurse
Dir cert: -recurse
Get-Command get-childitem -argumentlist cert: | format-list *
Get-Command
get-command -commandtype application
get-command -commandtype externalscript
get-command export-csv -syntax
get-command -verb get
get-command -noun job

Filter vs Functions

 Filters have easier access to the $_ in a pipeline than a function does.

get-command -commandtype filter
get-command -commandtype function
filter example {write-host $_.name $_.Length}
dir c:\ | example
function example2 {write-host $_.name $_.Length}
dir c:\ | example2 #this will not work function example2 {Begin {$Input} Process {write-host $_.name $_.Length}} dir c:\ | example2 #this will work function example2 {Begin {$Input} Process {write-host $_.name $_.Length} End {$output}}
 
$var=Get-Item env:\path
$var.value
$env:path # shortcut to the 2 lines above

Get-Item env:\path

Start-Transcript

Start-Transcript -path "c:\pshell.log1.txt" -force
Start-Transcript

(get-command).count
$array= get-command
$array.count 

Get-Member

Get-Help GM -full | more
Get-Help Get-Member -parameter inputobject
Get-Member -InputObject (Get-Process)

Get-Process | Get-Member
get-process |Get-Member -Membertype property
get-process |Get-Member -Membertype property s*
Get child item (dir) pipe to get-member will return 2 members you can search the SDK for these object classes
  1. System.IO.DirectoryInfo
  2. System.IO.FileInfo
gci c:\ | gm

 Look at definition returned from the command bellow to know what to search SDK for.

Get-Member -input (get-item "C:\Program Files") -name getfiles
Get-Member -input (get-item "C:\Program Files") -name getfiles | Format-List
 
Get-Member -InputObject (Get-ChildItem)

 returns an object

Get-Member -InputObject (Get-ChildItem)

returns a string

Get-Service | Get-Member
(Get-Service wuauserv).canpauseandcontinue
Get-Service wuauserv | Format-List -property name, canpauseandcontinue
Get-Service wuauserv | Format-List -property *

(Get-Service wuauserv).stop()
Get-Service wuauserv
(Get-Service wuauserv).start()
Get-Service wuauserv
$service= get-service wuauserv
$service.CanStop

New-Object

You would use new-object when you want to do something that is not native to powershell. Page 36 of Powershell step by step shows an ilistration

$wshShell=New-Object -ComObject "wscript.shell"
Get-Member -input $wshShell # same result as next line
$wshShell | Get-Member
$wshShell.Run("Calc.exe")
$wshShell.ExpandEnvironmentStrings("%username%")
$wshNetwork = New-Object -ComObject "wscript.network"
Get-Member -input $wshNetwork
Get-Member -input ($wshNetwork.EnumNetworkDrives())
$wshNetwork.EnumPrinterConnections(
Dereference com objects
$wshShell = $null
$wshNetwork = $null
 look system.reflection.assembly in the sdk from the search look at assembly members

open windows explorer and browse to C:\WINDOWS\assembly

system.reflection is the namespace

assembly is the class

[system.reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")

[system.reflection.assembly]::Loadwithpartialname("System.Windows.Forms")

Now that we have loaded the class that we need we can use it

Create a GUI

$form = new-object "Windows.Forms.Form"
$button = New-Object "Windows.Forms.Button"
$button.Text = "Close me"
$button.add_click({$button.backcolor=[system.drawing.color]::red})
$form.Controls.Add($button)
$form.Activate()
$form.ShowDialog()

$button.Dispose()
$form.Dispose(

Read-Host

$szresponse = read-host "Please enter your password" -AsSecureString
$szresponse


Comments