Download presentation
Presentation is loading. Please wait.
Published byAlbert Ferguson Modified over 9 years ago
1
Windows PowerShell Deep Dive 1 Morgan Simonsen morgan.simonsen@ementor.no Ementor
2
What Will We Cover? PS Security PS Drives, Providers and SnapIns PS Profiles Advanced management Exchange 2007 Management 2
3
Level 300 Helpful Experience Experience with PowerShell Command line experience; cmd.exe, BASH etc. Command line experience; parameters, output etc. Scripting experience (PERL, VBScript, JScript) etc.) 3
4
Introducing Windows PowerShell New Windows command-line shell designed for system administrators Inteactive prompt and scripting environment Built on.NET Common Language Runtime (CLR) and.NET Framework 4
5
PowerShell Security Execution Policies: –Restricted (default) –AllSigned –RemoteSigned –Unrestricted (bad idea) Exectuion Policy can be managed through Group Policy (PowerShellExecutionPolicy.adm) No default open for.ps1 files. 5
6
PS Profiles 4 profiles in PS (listed in load order): –%windir%\system32\WindowsPowerShell\V1.0\profile.ps1 –%windir%\system32\WindowsPowerShell\V1.0\Microsoft.PowerShell_profile.ps1 –%UserProfile%\My Documents\WindowsPowerShell\profiel.ps1 –%UserProfile%\My Documents\WindowsPowerShell\Microsoft.PowerShell_profiel.ps1 The most specific profile has precedent $profile Test-path $profile Execution policy must allow configuration files 6
7
PowerShell Console Files XML files for customizing the console.psc1 extension Cmdlet: Export-Console -psconsolefile ConsoleFileName variable
8
Working with processes Get-Process Stop-Process 8
9
Working with services Get-Service New-Service Restart-Service Resume-Service Set-Service Start-Service Stop-Service Suspend-Service 9
10
Working with the filesystem PS can navigate all Windows drives with a reconizable filesystem Aliases for common directory naviagtion commands (cd, dir) Get-ChildItem 10
11
PowerShell scripts.ps1 extension Does not run when double-clicked Requires full path (.\ ) Controlled by Execution Policy 11
12
Advanced Pipelining Manipulating objects in the pipeline: –Where-Object –ForEach-Object –Select-Object –Sort-Object 12
13
PS Variables Varable names always start with $ Set-Location variable: Clear-Variable Get-Variable New-Variable Remove-Variable Set-Variable 13
14
Working with functions PS supports creating functions Names can be assignes to script blocks 14
15
Redirecting data Out-Printer Out-Null Out-Host Out-File 15
16
Operators ilike / inotlike eq / ne gt / lt 16
17
Are you sure? WhatIf Confirm Suspend get-mailbox | where-object {$_.servername -eq “smbex01"} | move-mailbox – targetdatabase"smbex02\Mailbox Store" -whatif 17
18
Working with Event Logs Get-Eventlog Get-EventLog –list Get-EventLog system -newest 3 18
19
Scripting with COM Access existing instrumentation Bind to COM objects $fso = New-Object -ComObject Scripting.FileSystemObject $m = [System.Runtime.InteropServices.Marshal] $word = $m::GetActiveObject("Word.Application") Invoke methods/access properties $fso.GetDrive(“C:”) $fso.VolumeName = “System Drive” Understand/extend instrumentation Extend and discover properties/methods Update-TypeData Office.Word.Types.ps1xml $fso | Get-Member Manipulate and format results Define and import custom formating Update-FormatData Office.Word.Format.ps1xml $word.RecentFiles | Sort name | Format-Table Allows more simpler/more powerful COM scripts because of utilities and formatting 19
20
Scripting with WMI PowerShell provides native WMI support Get-WmiObject Allows for inspection of WMI namespace Get-WmiObject –list [-Namespace xx] Get-WmiObject –Class xx –Namespace xx – Property xxx – Filter xxx –ComputerName xxx – Credential xxx Native language support [WMI] “\\JPSDESK10\root\cimv2:Win32_Process.Handle="0“ \\JPSDESK10\root\cimv2:Win32_Process.Handle="0 [WMICLASS] "ROOT\cimv2:WIN32_PROCESS" [WMISEARCHER]"select * from Win32_process WHERE Name = 'calc.exe'" 20
21
Scripting with ADSI PowerShell provides native access to ADSI Binding to ADSI object $objDomain = [ADSI]"LDAP://localhost:389/dc=NA,dc=fabrikam,dc=com” Creating ADSI object Creating ADSI object $objOU = $objDomain.Create("organizationalUnit", "ou=HR") $objOU.SetInfo() Modifying ADSI object $objOU.Put("description", "Human Resources") $objOU.SetInfo() Reading ADSI object $objOU.Get("description") Deleting ADSI object $objDomain.Delete(“organizationalUnit”, “ou=HR”) 21
22
Scripting Group Policy $gpm = New-Object -ComObject GPMgmt.GPM
23
Scripting with.NET PowerShell provides native access to any.NET class Create any object [reflection.assembly]::LoadWithPartialName("System.Windows. Forms") $d = New-Object System.DateTime 2006,12,25 Access Properties/Invoke Methods $d.DayOfWeek$d.AddDays(-30) Access Statics [DateTime]::Now[DateTime]::IsLeapYear(2006) Allows admins to easily access and leverage a huge API set because of scriptability, utilities and formatting 23
24
Scripting with XML PowerShell provides native XML support Native datatype $x=[xml]"<a><b><c>TEST</c></b></a>“ $b =[xml](type c:\i386\mssecure.xml) Native syntax to access “data” view of properties $b.BulletinDataStore.Bulletins.Bulletin[0] Access to XML methods $b.BulletinDataStore.SelectNodes(“//Patch”) XML properties available through PSBase property $b.BulletinDataStore.PSBase.innerXml 24
25
Scripting with Text Invoke existing tools Existing command run directly after variables are expanded Harvest data from existing tools Parse output into variables using text utilities. Pipe data to SELECT and use –FIRST and –LAST Select-String Select-String Dir | Select-String Dir | Select-String [DateTime]”12/25/2006 7:00” ([DateTime]”12/25/2006 7:00”).AddDays(-30) Use functions/scripts to wrap the commands and convert output to objects or provide standard syntax Safely process text Use CLR types via Windows PowerShell to safely parse text [URI]” http://blogs.msdn.com/powershell/archive/2006/04/25/583234.aspx” Allows admins to get 2-10x more power out of existing commands because of scriptability 25
26
EXCHANGE 2007 MANAGEMENT 26
27
Reduced Complexity 27
28
PowerShell Add-ons PoshConsole http://www.codeplex.com/PoshConsole http://www.codeplex.com/PoshConsole PowerShell Plus: http://www.powershell.com/plus/ http://www.powershell.com/plus/ PowerGadgets: http://www.powergadgets.com http://www.powergadgets.com 28
29
For More Information Windows PowerShell Team blog at: http://blogs.msdn.com/PowerShell http://blogs.msdn.com/PowerShell Windows PowerShell ScriptCenter at: http://www.microsoft.com/technet/scriptce nter/hubs/msh.mspx http://www.microsoft.com/technet/scriptce nter/hubs/msh.mspx 29
30
Books about PS 30
Similar presentations
© 2025 SlidePlayer.com Inc.
All rights reserved.