WINDOWS POWERSHELL 4.0 EXAMPLES
Created by http://powershellmagazine.com
The Invoke-WebRequest cmdlet parses the response, exposing collections of forms,
links, images, and other significant HTML elements. The following example returns all
the URLs from a webpage:
$iwr = Invoke-WebRequest http://blogs.msdn.com/b/powershell
$iwr.Links
The next example returns an RSS feed from the PowerShell team blog:
$irm = Invoke-RestMethod blogs.msdn.com/b/powershell/rss.aspx
$irm | Select-Object PubDate, Title
If you, for example, need to execute one of the -PSSession cmdlets using alternate
credentials you must specify the credentials for the Credential parameter every time
you call the command. In PowerShell 3.0 and later, we can supply alternate credentials
by using new $PSDefaultParameterValues preference variable. For more information,
see about_Parameters_Default_Values.
$PSDefaultParameterValues = @{'*-PSSession:Credential'=Get-Credential}
How to parse webpage elements
The Invoke-RestMethod cmdlet sends an HTTP(S) request to a REST-compliant web
service. You can use it to consume data exposed by, for example, the OData service for
TechEd North America 2014 content. Put information about TechEd NA 2014 sessions
into a $sessions variable. Let's iterate through that collection of XML elements and
create custom PowerShell objects using [PSCustomObject]. Pipe the output to an Out-
GridView command specifying the PassThru parameter to send selected items from the
interactive window down the pipeline, as input to an Export-Csv command. (While you
are in a grid view window, try to filter only PowerShell Hands-on Labs, for example). If
you have Excel installed the last command opens a CSV file in Excel.
$sessions = Invoke-RestMethod https://odata.eventpointdata.com/tena2014/
Sessions.svc/Sessions
$sessions | ForEach-Object {
$properties = $_.content.properties
[PSCustomObject]@{
Code = $properties.Code
Day = $properties.Day_US
StartTime = $properties.Start_US
EndTime = $properties.Finish_US
Speaker = $properties.PrimarySpeaker
Title = $properties.Title
Abstract = $properties.Abstract
}
} | Out-GridView -PassThru | Export-Csv $env:TEMP\sessions.csv -NoTypeInformation
Invoke-Item $env:TEMP\sessions.csv
How to fetch data exposed by OData services
How to set custom default values for the parameters
In Windows PowerShell 3.0, Save-Help worked only for modules that are installed on
the local computer. PowerShell 4.0 enables Save-Help to work for remote modules.
Use Invoke-Command to get the remote module and call Save-Help:
$m = Invoke-Command -ComputerName RemoteServer -ScriptBlock { Get Module -
Name DhcpServer -ListAvailable }
Save-Help -Module $m -DestinationPath C:\SavedHelp
Use a PSSession to get the remote module and call Save-Help:
$s = New-PSSession -ComputerName RemoteServer
$m = Get-Module -PSSession $s -Name DhcpServer -ListAvailable
Save-Help -Module $m -DestinationPath C:\SavedHelp
Use a CimSession to get the remote module and call Save-Help:
$c = New-CimSession -ComputerName RemoteServer
$m = Get-Module -CimSession $c -Name DhcpServer -ListAvailable
Save-Help -Module $m -DestinationPath C:\SavedHelp
Save-Help
In PowerShell 2.0, if you had some local variable that you wanted to use when
executing a script block remotely, you had to do something like:
$localVar = 42
Invoke-Command -ComputerName Server 1 { param($localVar) echo $localVar } -
ArgumentList $localVar
In PowerShell 3.0 and later, you can use Using scope (prefix variable name with
$using:):
Invoke-Command -ComputerName Server1 { echo $using:localVar }
How to access local variables in a remote session