1 / 97

The Hidden Charms of Windows PowerShell 3.0 and 4.0

The Hidden Charms of Windows PowerShell 3.0 and 4.0. June Blender Senior Programming Writer Microsoft Corporation @ juneb_get_help juneb@microsoft.com. Today’s Topics. A Bit O’ History: How did we get to Windows PowerShell 4.0? Cool Features of Windows PowerShell 3.0

brosh
Télécharger la présentation

The Hidden Charms of Windows PowerShell 3.0 and 4.0

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. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. The Hidden CharmsofWindows PowerShell 3.0 and 4.0 June Blender Senior Programming Writer Microsoft Corporation @juneb_get_help juneb@microsoft.com

  2. Today’s Topics • A Bit O’ History: How did we get to Windows PowerShell 4.0? • Cool Features of Windows PowerShell 3.0 • What’s in Windows PowerShell 4.0 Preview (beside DSC) • Windows PowerShell Help • Q & A

  3. A bit o’ history…. • 2005: Windows PowerShell 1.0 • Out-of-band download • Included in Windows Server 2003 • 2009: Windows PowerShell 2.0 • Included: Windows 7 and Windows Server 2008 • WMF: Windows XP, Windows Server 2003 R2 • Microsoft .NET 2.5, 3, CLR 3 • 2012: Windows PowerShell 3.0 (Includes 2.0) • Included: Windows 8 and Windows Server 2012 • WMF: Windows 7, Windows Server 2008 • Microsoft .NET 4, 4.5, CLR 4 • Includes: Windows PowerShell 2.0 • 2013: Windows PowerShell 4.0 Preview • Included: Windows 8.1 and Windows Server 2012 R2 • WMF: Windows 7 SP1, Windows Server 2008 SP1, Windows Server 2012 • Microsoft .NET 4.5, CLR 4

  4. A bit o’ history…. • 2005: Windows PowerShell 1.0 • Introduction to a .NET-based shell • 2009: Windows PowerShell 2.0 • Remote management • 2012: Windows PowerShell 3.0 • Coverage, Workflows • 2013: Windows PowerShell 4.0 • Control, DSC

  5. Windows PowerShell 4.0 Preview • Windows PowerShell 4.0 • Included in Windows 8.1 and Windows Server 2012 R2 • Windows Management Framework 4.0for Windows 7, Windows Server 2008, Windows Server 2008 R2 • Requires .NET 4.5 (Get-ItemProperty -Name Release -Path 'HKLM:\Software\Microsoft\NET Framework Setup\NDP\v4\Full' • Windows 8, Windows Server 2012 • Upgrade to Windows 8.1, Windows Server 2012 R2 • Windows PowerShell 2.0 • Not included in Windows PowerShell 4.0 Preview • To install, use Server Manager or Programs in Control Panel

  6. How to Get Windows PowerShell • Windows PowerShell 2.0 • Included in Windows 7 and Windows Server 2008 • Windows Management Framework 2.0for Windows XP, Windows Vista, Windows Server 2003 • Windows 8.1: Programs in Control Panel • Windows Server 2012 R2: Server Manager • Windows PowerShell 3.0 (Includes 2.0) • Included in Windows 8 and Windows Server 2012 • Windows Management Framework 3.0for Windows 7, Windows Server 2008, Windows Server 2008 R2 • Windows PowerShell 4.0 • Included in Windows 8.1 and Windows Server 2012 R2 • Windows Management Framework 4.0for Windows 7, Windows Server 2008, Windows Server 2008 R2

  7. Cool Features in PowerShell Core 3.0 • The Basics • Simplified syntax • Getting properties and methods • Get-Command – Improved! • Module AutoLoading • Get-ChildItem – Improved! • Get-Help -ShowWindow • Show-Command • Map Network Drive • Unblock-File • ISE snippets • The Advanced Parts • Disconnected Sessions • Windows PowerShell Workflow • Scheduled Jobs • Dynamic Type Data • Updatable Help Windows PowerShell 3.0 includes Windows PowerShell 2.0 running side-by-side Get-Help

  8. Simplified Syntax: {$_.} Avoid errors Make it work Make everyone successful

  9. Simplified Syntax: ForEach-Object {$_.} PS 2.0: Get-Module | ForEach-Object {$_.NestedModules} PS 3.0: Get-Module | ForEach-Object NestedModules Get-Module | ForEach-Object {$_.NestedModules.Name} Get-Help ForEach-Object

  10. Simplified Syntax: Where-Object {$_.} PS 2.0: Get-Process | Where {$_.ProcessorAffinity} Get-Process | Where {$_.BasePriority –gt 8} PS 3.0: Get-Process | Where ProcessorAffinity Get-Process | Where BasePriority –gt 8 Get-Help Where-Object

  11. Get Properties… of multiple objects . PS 2.0: # One object (Get-Module PSScheduledJob).Name (Get-Module -ListAvailable).Name #Null PS 3.0: # One or more objects (Get-Module -ListAvailable).Name Get-Help about_Properties

  12. Invoke Methods… of multiple objects PS 2.0: # One object # Multiple objects $a = ‘abc’ $b = ‘abc’, ‘def’ $a.ToUpper() $b.ToUpper() #FAIL PS 3.0: # One or more objects $a.ToUpper() $b.ToUpper() ABC ABC DEF Get-Help about_Methods

  13. Count / Length… of a single object if ($p.Count –gt 0)… PS 2.0: # One object # Multiple objects 'abc'.Count ('abc', 'def').Count #Nothing 2 PS 3.0: # One object # Zero objects 'abc'.Count $null.Count 1 0 Get-Help about_Properties, about_Arrays

  14. Get-Command • PS 2.0: • Gets commands in the current session. • PS 3.0: • Gets commands in all installed modules. • In PSModulePath environment variable • + current session Get-Help Get-Command

  15. Module Auto-Loading Import-Module • PS 2.0: • Get-Module | Import-Module • PS 3.0: • Modules are imported automatically: • Use a command in the module • Get-Command (any command in the module) • Get-Help (any command in the module) • Modules are *not* imported automatically: • Get-Command * • Get-Help * • Not in PSModulePath environment variable Get-Help about_Modules

  16. Module Auto-Loading “Gotcha” Import-Module PS C:\> Get-Job –JobNameMyScheduledJob # Nothing PS C:\> Get-ScheduledJobMyScheduledJob Id Name JobTriggers Command Enabled -- ---- ----------- ------- ------- 1 MyScheduledJob 1 Update-Help True PS C:\> Get-Job –JobNameMyScheduledJob Id Name PSJobTypeName State HasMoreData -- ---- ------------ ----- ----------- MyScheduledJobPSScheduledJob Completed True Get-Help about_Modules, about_Scheduled_Jobs

  17. Get-ChildItem (dir) PS 3.0: New Parameters in FileSystem drives -File dir s* -af -Directory dirs* -ad -ReadOnlydirs* -af-ar -Hidden dirs* -ar-ah -System dirs* -as -Attributes dir-att !d,!d+h#No spaces D, R, H, S; !, +, “,” PS C:\> Dir *.ps1 –Attributes !r | attrib +r Get-Help Get-ChildItem -Path C:

  18. Get-Help -ShowWindow Settings are saved. PS C:\> Get-Help Update-Help –ShowWindow Get-Help Get-Help

  19. Get-Help –ShowWindow #Gotcha Displays only the first paragraph of parameter descriptions Get-Help Get-Help

  20. Show-Command DEMO !! Get-Help Show-Command

  21. Map Network Drive New-PSDrive -Persist PS 2.0: New-PSDrivecreates drives in the current session. PS 3.0: New-PSDrive –Persist creates mapped network drive. Get-Help New-PSDrive

  22. Map Network Drive New-PSDrive –Persist • Root must be on a remote computer • Name must comply with Windows drive name standards (one letter) • User-specific, including “Run as administrator” PS C:\> New-PSDrive -Root \\Server01\PowerShell -Name P -PSProviderFileSystem-Persist Name Used (GB) Free (GB) Provider Root ---- --------- --------- -------- ---- P 1148.67 248.54 FileSystem \\Server01\\PowerShell Get-Help New-PSDrive

  23. Unblock-File • The Unblock-File cmdlet lets you open files that were downloaded from the Internet. • It unblocks Windows PowerShell script files that were downloaded from the Internet so you can run them, even when the Windows PowerShell execution policy is RemoteSigned. • Much better than changing the execution policy to Unrestricted. Get-Help Unblock-File

  24. Unblock-File Get-Help Unblock-File

  25. ISE Snippets • Creates a reusable text "snippet" in Windows PowerShell ISE. • Comes with built-in snippets • You can create your own snippets! • Snippet Cmdlets: • New-IseSnippet • Get-IseSnippet • Import-IseSnippet Get-Help New-ISESnippet

  26. ISE Snippet "Gotchas" • Description parameter is required. • To view snippets, Ctrl-J or Edit/Start-Snippets or Right-click/Start Snippets • If you run New-IseSnippet in the console (not ISE), it works, but returns a InvokeMethodOnNull error that you can ignore. • To delete a user-created snippet, use Remove-Item PS C:\> Get-IseSnippet <title> | Remove-Item Get-Help New-ISESnippet

  27. Create an ISE Snippet DEMO !!

  28. What’s New in Core 3.0? • The Advanced Parts • Disconnected Sessions • Session Configuration Files • Windows PowerShell Workflow • Updatable Help • Scheduled Jobs • Dynamic Type Data Windows PowerShell 3.0 includes Windows PowerShell 2.0 !

  29. Disconnected Sessions At work… • Start a PSSession. • Run commands in the PSSession. • [ Disconnect from the PSSession.] • Exit Windows PowerShell. • Shut down the machine. • Go home… • Boot up your laptop. • Start Windows PowerShell. • Reconnect to the PSSession. • Get the results of commands run in the session. At home… Get-Help about_Remote_Disconnected_Sessions

  30. Disconnected Sessions • DISCONNECTED SESSION CMDLETS • New-PSSession: Creates a PSSession. • Disconnect-PSSession: Disconnects a PSSession. • Connect-PSSession: Connects to a disconnected PSSession. • Receive-PSSession: Gets the results of commands that ran in disconnectedsessions. • Get-PSSession: Gets PSSessions on the local computer or on remote computers. • Invoke-Command: InDisconnectedSessionparameter creates a PSSessionand immediately disconnects. Get-Help New-PSSession

  31. PSSessions are session-independent • PS 2.0: • New-PSSession: Creates a connection from • the current session to another computer. • PS 3.0: • New-PSSession: Creates a connection that is • saved at the remote end, even if the current • Session is disconnected. Localhost To: Server01To: Server012 Server012 From: Server01From: Server012 From: Server02 Get-Help New-PSSession

  32. Get-PSSession -ComputerName • PS 2.0: • Gets sessions in the current session that are connected to <ComputerName>. • PS 3.0: • Gets all sessions on <ComputerName> for the current user. • To get sessions in the current session, omit -ComputerName. Localhost To: Server01 To: Server012 Server012 From: Server01From: Server012 From: Server02 Get-Help Get-PSSession

  33. Get-PSSession -ComputerName # On Server01 PS C:\> New-PSSession –ComputerName Server02 PS C:\> Get-PSSession –ComputerName Server02 Id Name ComputerName State ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 Session1 Server02 Opened Microsoft.PowerShellAvailable Server02 From: Server01 # On Server02 PS C:\> Get-PSSession Id Name ComputerNameState ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 Session1 Server02 Opened Microsoft.PowerShellBusy # On Server03 PS C:\> Get-PSSession –ComputerName Server02 Id Name ComputerNameState ConfigurationName Availability -- ---- ------------ ----- ----------------- ------------ 1 Session1 Server02 DisconnectedMicrosoft.PowerShellBusy Get-Help Get-PSSession

  34. How to Disconnect: • Disconnect-PSSession • Invoke-Command -InDisconnectedSession How to Connect / Reconnect: • Connect-PSSession • Receive-PSSession Get-Help about_Remote_Disconnected_Sessions

  35. Disconnected Sessions#Gotchas Don’t exceed the IdleTimeout Set the BufferMode carefully • IdleTimeoutMs: How long a disconnected session can be idle before being deleted. Default value. • OutputBufferingMode: What happens to session output when the output buffer is full. • Values: • Block: Stop the command • Drop: Delete old output (FIFO) • MaxIdleTimeoutMs: The maximum idle timeout that is permitted by the session configuration. Get-Help about_Remote_Disconnected_Sessions

  36. Disconnected Sessions DEMO !! Get-Help about_Remote_Disconnected_Sessions

  37. Disconnected Sessions: How it WorksSession Configurations • PSSessions are saved in the session configuration ("endpoint") on the remote computer (aka "server-side") • A session configuration is a collection of settings that create the environment of sessions for remote users. • Under the covers, they are WSMan plug-ins. (dir WSMan:\<computer>\Plugin) • Every session uses a session configuration. If you don't specify one, you get the default, Microsoft.PowerShell • The ACL on the session configuration determines who can connect to the computer remotely. • You can use session configurations to create custom sessions for remote users. Get-Help about_Session_Configurations

  38. Session ConfigurationsSession Configuration Files • To create a session configuration in Windows PowerShell 2.0, you need to write a C# program. • To create a session configuration in Windows PowerShell 3.0 (and later!), you use a session configuration file that contains a hash table. • Yes, you can create your own custom session configurations. It's easy! • New-PSSessionConfigurationFile -Path MySession.pssc • Register-PSSessionConfiguration -Name WithProfile -StartupScript C:\users\juneb\Documents\WindowsPowerShell\profile.ps1 Get-Help about_Session_Configuration_Files, New-PSSessionConfigurationFile

  39. Scheduled Jobs Don’t exceed the IdleTimeout Set the BufferMode carefully • Scheduled jobs are a unique hybrid of Windows PowerShell background jobs and scheduled tasks. • Run asynchronously and in the background on the schedule that you specify. • Results of each job instance that runs are automatically saved and easily retrieved. • Use Job cmdlets to manage the running instances. • You can see scheduled jobs in Task Scheduler and manage them in Windows PowerShell. Get-Help about_Scheduled_Jobs

  40. Scheduled Jobs Don’t exceed the IdleTimeout Set the BufferMode carefully • Trigger: New-JobTrigger: Sets the schedule. • Options: New-ScheduledJobOption: Set conditions for running the job. • Command that runs (script block / script) • Create the scheduled job: Register-ScheduledJob ("Run as administrator") Get-Help about_Scheduled_Jobs

  41. Scheduled Jobs "Gotchas" • Run as administrator • Be mindful of the options. • Options use Task Scheduler default values. • Might prevent the task from running -- ever. • Read:Get-Help New-ScheduledJobOption –Parameter * Get-Help about_Scheduled_Jobs

  42. Scheduled Jobs “Gotcha” PSC:\> Get-ScheduledJob PS C:\> # function elevate; “Run as administrator” PS C:\> Start-Process PowerShell –Verb RunAs PS C:\> Get-ScheduledJob Id Name JobTriggers Command Enabled -- ---- ----------- ------- ------- 1 UpdateHelp 1 Update-Help True Get-Help about_Scheduled_Jobs

  43. Dynamic Type Data • Adds extended type data only to the current session • Much simpler than creating a Types.ps1xml file • TypeData Cmdlets • Update-TypeData -TypeName adds the type to the session • Update-TypeData-DefaultDisplayPropertySet changes the properties that are displayed by default • Get-TypeData gets the type data in the current session (static and dynamic) • Remove-TypeData deletes extended type data from the current session Get-Help Update-TypeData, about_Types.ps1xml

  44. Dynamic Type Data PS C:\>Update-TypeData –TypeNameSystem.Management.Automation.PSModuleInfo-MemberNameSupportsUpdatableHelp-MemberTypeScriptProperty -Value {if ($this.HelpInfoUri) {$True} else {$False}} PS C:\>Get-Module -ListAvailable | Format-Table Name, SupportsUpdatableHelp Name SupportsUpdatableHelp ---- ------------------------------- Microsoft.PowerShell.Diagnostics True Microsoft.PowerShell.Host True Microsoft.PowerShell.Management True Microsoft.PowerShell.Security True PSDiagnostics False PSScheduledJob True TroubleshootingPack False Get-Help Update-TypeData, about_Types.ps1xml

  45. Dynamic Type Data PS C:\>Update-TypeData -TypeNameSystem.DateTime -DefaultDisplayPropertySetDayOfWeek, Date, TimeOfDay PS C:\>Get-Date | Format-List DayOfWeek: Tuesday Date: 7/9/2013 12:00:00 AM TimeOfDay: 17:32:49:5829247 Get-Help Update-TypeData, about_Types.ps1xml

  46. Updatable Help Don’t exceed the IdleTimeout Set the BufferMode carefully • Problem: Outdated help files • Help is continuously updated. Help XML files are part of Windows, so they're hard to update (trusted installer). • Online help is great, but users want updated help at the command line. • Solution: Updatable Help • Update-Help cmdlet downloads help files from the Internet and installs them (correctly!). • Compares versions and installs when a new one is available. Get-Help about_Updatable_Help

  47. Updatable Help • Offline: Save-Help • Save-Help cmdlet downloads help files from the Internet and saves them in a file system directory. • Update-Help gets help from a file system directory and installs it (correctly!) • Multilingual Support • HelpInfo information files support a version for each locale. • Enhanced Online Help • Get-Help -Online works even when there are no help files on the box. • Autogenerated Help • Get-Help creates basic help when there are no help files online or on the box.

  48. Update-Help Don’t exceed the IdleTimeout Set the BufferMode carefully • Figures out which modules support Updatable Help • Finds Updatable Help at an Internet site. • Compares versions • Downloads help package • Unpacks it • Verifies it • Installs it in the locale-specific subdirectory of the module directory. • To use the help, type Get-Help. • You don't need to restart Windows PowerShell. Get-Help Update-Help, about_Updatable_Help

  49. Save-Help Don’t exceed the IdleTimeout Set the BufferMode carefully • Figures out which modules support Updatable Help • Finds Updatable Help at an Internet site. • Compares versions • Downloads help package to a specified <directory> • Update-Help -SourcePath <directory> • Checks version • Unpacks files • Verifies them • Installs them Get-Help Save-Help, about_Updatable_Help

  50. Update-Help "Gotchas" Don’t exceed the IdleTimeout Set the BufferMode carefully • Must be Administrator to update help in $pshome (System32). • You might be interrupted by network • Some module don't support Updatable Help. • Look for HelpInfoUri property value • Updatable help hasn't been released or updated. • Online Help (-Online) isn’t supported. • Look for HelpUriproperty value Get-Help about_Updatable_Help

More Related