Article Series published: Powershell für .NET-Entwickler

This post describes the article series about Powershell that I wrote for Windows Developer and shows a couple of samples.

The Windows Developer Magazin (https://entwickler.de/windows-developer-magazin) published my article series on Powershell for .NET Developers.

Die Grundlagen für die DevOps-Welt Teil 1: PowerShell für .NET-Entwickler
https://entwickler.de/windows-developer-magazin/windows-developer-5-16-237794.html

Entwickeln mit Power Shell-Unterstützung Teil 2: Praxisbeispiele aus dem DevOps-Alltag
https://entwickler.de/windows-developer-magazin/windows-developer-6-16-243066.html

Powershell Code Samples:
http://www.manuelmeyer.net/2016/03/powershell/

If interested, you can order it at: https://entwickler.de/windows-developer-magazin
A reading sample is available at: https://entwickler.de/leseproben/grundlagen-devops-welt-239412.html

The article introduces Powershell to the .NET developer and explains why it is a really powerful tool that can tremendously support your daily development activities.

Part 1: Die Grundlagen für die DevOps-Welt Teil 1: PowerShell für .NET-Entwickler

The article begins by introducing the help system integrated into Powershell. Did you know that you can open an external window with details to a command?

Get-Help Get-Process -ShowWindow

image

The article continues by covering the following topics:

  • CmdLets
  • Pipelines
  • Objects
  • Output Formatting
  • Result Filtering

 

ConvertTo-Html

Did you know that you can convert the results of a Powershell CmdLet to JSON, HTML, XML, or CSV? Just pipe to a ConvertTo- command:

dir | ConvertTo-Html | Out-File D:\files.html

image

Entwickeln mit Power Shell-Unterstützung Teil 2: Praxisbeispiele aus dem DevOps-Alltag

The second part of the articles covers advanced concepts:

  • Powershell Remoting
  • COM Interop
  • Windows Management Instrumentation (WMI)
  • Powershell Providers
  • Working with Sql Server
  • Working with IIS
  • Working with WebServices
  • Various Snippets
  • Powershell in the Browser for Microsoft Azure

 

Powershell Remoting

Powershell Remoting is REALLY powerful. With remoting you can work on a remote machine as if it was a local computer. There are two distinct modes. You can use Enter-PSSession to open a Powershell session on a remote machine or you can use Invoke-Command to send a command to one or MANY remote machines. The following snippet gets the first entry of the Security event log from the three machines LTMME05, LTMME06, LTMME07.

Invoke-Command -ComputerName LTMME05, LTMME06, LTMME07 -ScriptBlock {Get-EventLog Security -Newest 1}

 

COM Interop

Did you know that you can use COM interop to fill an Excel sheet with the currently running window services? Check the code at: http://www.manuelmeyer.net/2016/03/powershell/

Abb2_ManuelMeyer_Powershell2_ComInteropExcel

SOAP WebServices

Did you know that you can create a typed SOAP-WebService client in Powershell?

$uri = 'http://www.webservicex.net/airport.asmx?WSDL'
$airportProxy = New-WebServiceProxy -Uri $uri -Namespace ws
$airportProxy.getAirportInformationByAirportCode('ZRH')

Result:

<NewDataSet>
  <Table>
    <AirportCode>ZRH</AirportCode>
    <CityOrAirportName>ZURICH</CityOrAirportName>
    <Country>Switzerland</Country>
    <CountryAbbrviation>CH</CountryAbbrviation>
    ...
  </Table>
</NewDataSet>

 

REST WebServices

Calling REST Services is just as easy:

$uri = 'https://maps.googleapis.com/maps/api/directions/json'
$query = '?mode=driving&origin=Rheinstrasse 105, 64283 Darmstadt&destination=Rheinstrasse 66, 55116 Mainz'
$response = Invoke-RestMethod $uri$query
$response.routes[0].legs[0].distance

Result:

text    value
----    -----
35.0 km 35033

Powershell Providers

Powershell providers are a great feature. You might already know that you can navigate through the file system in Powershell. All the familiar commands from the DOS-Shell like “dir”, “cd”, “ren”, and so on still work in Powershell. However, these are NOT Powershell CmdLets. The CmdLets would be “Get-ChildItem” for “dir”, “Set-Location” for “cd” and “Rename-Item” for “ren”. So, how does this work?

Meet Powershell providers. A Powershell provider lets you navigate through a hierarchical structure. The FileSystemProvider is the default provider in Powershell. This is why the CmdLet “Get-ChildItems” lists the contents of the active directory. Powershell provides multiple providers. Enter Get-PSProviders for a list:

Name        Drives      
----        ------      
Registry    {HKLM, HKCU}
Alias       {Alias}     
Environment {Env}       
FileSystem  {C, D, Y, Z}
Function    {Function}  
Variable    {Variable}  
Certificate {Cert}      
WSMan       {WSMan}

Every provider lets you navigate through and modify hierarchical structures. The providers can be accessed by the use of abstractions called drives. If, for instance, you want to navigate through the HKEY_LOCAL_MACHINE tree in the registry, you can switch to the HKLM drive and navigate using dir, cd, …

PS C:\WINDOWS\system32> cd hklm:
PS HKLM:\> cd .\DRIVERS\
PS HKLM:\DRIVERS\> dir 

    Hive: HKEY_LOCAL_MACHINE\DRIVERS 

Name                           Property
----                           --------
DriverDatabase                 Version : 167772160

In addition to the in-the-box providers, you can add others by using the import-module statement. You could for example explore the providers for Sql Server or IIS by either installing the products or by downloading their Powershell Modules. You need to import the module first and can then access the provider via drive:

PS C:\> Import-Module WebAdministration
PS C:\> Get-PSProvider

Name                 Capabilities
----                 ------------
Registry             ShouldProcess, Transactions
Alias                ShouldProcess
Environment          ShouldProcess
FileSystem           Filter, ShouldProcess, Credentials
Function             ShouldProcess
Variable             ShouldProcess
WebAdministration    ShouldProcess


PS C:\> cd IIS:
PS IIS:\> dir

Name
----
AppPools
Sites
SslBindings


PS IIS:\> dir .\AppPools\

Name                     State        Applications
----                     -----        ------------
.NET v4.5                Started
.NET v4.5 Classic        Started
CalculatorApplicationPoo Started
l
DefaultAppPool           Started      Default Web Site
                                      /ContosoUniversity
                                      /Services
MME                      Stopped
NewAppPool               Started
swimmingpool             Started