Powershell‎ > ‎Training Notes‎ > ‎

PowerShell v2 Day 3

Blah

Day 3

Powershell ISE default file encoding will not let you sign scripts.
$psISE
$psISE.CurrentFile | fl *
$psise.CurrentPowerShellTab.AddOnsMenu
 

http://www.dougfinke.com/blog/index.php/2009/07/08/six-important-menu-add-ons-for-powershells-ise/

 
 

Modules

$env:PSModulePath
Get-Module -ListAvailable



Import-Module -Prefix BT BitsTransfer #add a prefix to avoid name collision if necessary

 script variables
$PSScriptRoot
 

Windows Remoting

Windows remoting stuff
create new sessions and use them interactively
winrs -r:servername cmd.exe
Enter-PSSession -ComputerName server01
create a persistant session
$s = New-PSSession server01
Enter-PSSession -Session $s
exit
Remove-PSSession $s

 

Invoke-Command -ComputerName server01,server02,server22 -ScriptBlock { Get-Process }

 

modify pssession config security

Set-PSSessionConfiguration microsoft.powershell -ShowSecurityDescriptorUI

  

$job = Start-Job -ScriptBlock { gci c:\ -Recurse}
Receive-Job -Job $job -Keep
$job | gm -MemberType event
Register-ObjectEvent -InputObject $job -EventName StateChanged -Action { Write-Warning "Job state changed"}

 

Get all cmdlets with the -asJob parameter
Get-Command -CommandType cmdlet | Where-Object { $_.Parameters.Keys -contains "AsJob"}

  

Get folder size

http://technet.microsoft.com/en-us/library/ff730945.aspx  

Get-DiskUsage.psm1  

Errors

$error
$error[0] # most recent error in stack
$error.clear()

$ErrorActionPreference
Get-Variable *preference*

Get-ChildItem HKLM:
$ErrorActionPreference = "SilentlyContinue"
Get-ChildItem HKLM:

$ErrorActionPreference = "Continue"
Get-ChildItem HKLM: -ErrorAction SilentlyContinue
Get-ChildItem HKLM: -ErrorVariable $gcierror

$Error[0]
$Error[0] | fl *
$Error[0] | fl * -Force

$Error[0].Exception.message
$Error[0].Exception.message | gm

write-error
throw #terminatingerror


$? returns true if the last command had no errors
$? returns false if the last command had any errors
$LASTEXITCODE # for external application
cmd
net stop winmgmt && net start winmgmt
powrshell
net stop winmgmt if ($?) {net start winmgmt}

Trace-Command { Get-Process explorer } -Name ParameterBinding -Option All -PSHost
Get-TraceSource

Debugging

Set-PSBreakpoint -Script file.ps1 -Line 3
Get-Variable -Scope 0
? # get commands in debug mode

Set-PSBreakpoint -varibale var -mode ReadWrite
Set-PSBreakpoint -command get-process

  old v1 way of doing this was to include $host.EnterNestedPrompt() in your script

$host.EnterNestedPrompt()
[Enum]::GetNames("System.ConsoleColor")
[Console]::Beep()
Set-StrictMode
Set-PSDebug -Strict

  

  

  


 
 

WMI

__This is the same as $_
ISA is the same as -is

Get-WmiObject -Query "Select * from Meta_Class where __THIS ISA '__EVENT'"


[AppDomain]::CurrentDomain.GetAssemblies()
Add-Type -AssemblyName "System.Windows.Forms"
[System.Windows.Forms.MessageBox]::Show("Hello", "Caption", "YesNoCancel")
[AppDomain]::CurrentDomain.GetAssemblies()
 
  
Comments