PowerShell Core – Connecting to O365 Exchange Online

July 3, 2018

Now that we have PowerShell running on Windows 10… let’s see if we can get use it to get access to O365 Exchange Online.

 

First you need to load the MSOnline Windows PowerShell  Modules in PowerShell Core.

This is done using these 3 Lines :

Install-Module WindowsPSModulePath -Force -Scope CurrentUser 
 
Add-WindowsPSModulePath

Import-Module MsOnline

 

image

If you don’t use the first 2 lines you will get an error. Because PS Core doesn’t have the MSOline Module available.

So we grab it from the Windows PowerShell.

 

let’s set the credentials

$UserCredential = Get-Credential

image

Next create the remote session to load the module

 

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

image

 

Test it using some commands

image

Get-User

image

 

For Mac see here :

https://www.cgoosen.com/2017/03/connecting-to-exchange-online-with-remote-powershell-from-a-mac/

For the other O365 Services see here :

https://www.michev.info/Blog/Post/1954/powershell-core-and-office-365

Enjoy !

Advertisements

PowerShell Core – Running on Windows IoT using a RPI3

July 2, 2018

As mentioned on this post

I was planning to test PowerShell Core on Windows IoT running on an ARM platform using RPI3….

PS Core Version 6.x claims to be a cross platform Smile

It is still experimental as mentioned here :

https://docs.microsoft.com/en-us/powershell/scripting/setup/powershell-core-on-arm?view=powershell-6

So let’s take a look :

1. Download the correct ZIP

https://github.com/PowerShell/PowerShell/releases

Windows IoT running on RPI3 is a 32bit version (if you are using the free version)

image

2. Open the ZIP

This contains all the modules needed to run on your RPI3

image

Extract and copy all files over to your destination RPI3 folder.

 

image

For example c:\pscore

 

3. Setup Remoting on RPI3

Run this command and see PS notes :

You’ll get an error message and will be disconnected from the device because it has to restart WinRM

 

image

 

# Be sure to use the -PowerShellHome parameter otherwise it'll try to create a new
# endpoint with Windows PowerShell 5.1
.\Install-PowerShellRemoting.ps1 -PowerShellHome .
# You'll get an error message and will be disconnected from the device because it has to restart WinRM

 

4. Connect to the PS Core on your RPI3

# Be sure to use the -Configuration parameter.  If you omit it, you will connect to Windows PowerShell 5.1
Enter-PSSession -ComputerName <deviceIp> -Credential Administrator -Configuration powershell.6.0.2

image

 

5. How to run Windows PowerShell modules in PowerShell Core

Run these 2 commands to get access to the Windows PS modules

 

Install-Module WindowsPSModulePath -Force -Scope CurrentUser 

Add-WindowsPSModulePath

 

image

Or this way for a custom module

Import-Module "C:\Program Files (x86)\AutoIt3\AutoItX\AutoItX.psd1"
Get-Command *au3*

 

image

image

6. Test PS Core on your RPI3

image

All runs as expected Smile

 

But keep in mind that not all functions are supported (yet)

image

 

Even if it says so see help Sad smile

image

 

Running PowerShell Core on Linux or Mac see here to get started :

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-powershell-core-on-linux?view=powershell-6

https://docs.microsoft.com/en-us/powershell/scripting/setup/installing-powershell-core-on-macos?view=powershell-6

Enjoy !


PowerShell Core – Installing and Testing

July 2, 2018

PowerShell Core has reached version 6.1xxx at this moment …

 

Why do you need PS Core v6.1.x

https://blogs.msdn.microsoft.com/powershell/2018/01/10/powershell-core-6-0-generally-available-ga-and-supported/

https://docs.microsoft.com/en-us/powershell/scripting/whats-new/what-s-new-in-powershell-core-60?view=powershell-6

https://blogs.msdn.microsoft.com/powershell/2018/01/24/powershell-core-6-1-roadmap/

 

How to install

Since PS Core is a cross platform distribution, just pick the one you need Smile

https://github.com/PowerShell/PowerShell/releases

 

Download and run for a Windows 10 SxS installation

 

image

 

Select Open PS Core

or start is manually

image

Testing

In order to run PS Core you need to use the pwsh.exe commandline and not powershell.exe !

Check the PS Core version :

run pswh.exe –v

image

Or the famous command

$psversiontable

 

image

PowerShell core does not have all the modules of the full blown version available.

But it has already a lot… run

get.command 

it will list about 425 commands

image

 

If you need more :

Windows PowerShell Compatibility Pack

With the announcement of the Windows Compatibility Pack for .NET Core for .NET Core, PowerShell Core will regain some APIs that had been dropped in the transition to .NET Core.  This means we can re-enable some cmdlets that weren’t part of the PowerShell Core 6.0 release: WMI, EventLog, and PerfCounters.  In addition, some highly requested APIs will be available which means that some existing Windows PowerShell modules might just work in PowerShell Core 6.0.  This module will replace the WindowsPSModulePath module to add the Windows PowerShell $env:PSModulePath to PowerShell Core 6.0.  Finally, we can use this module to optionally add back some aliases that we removed from PowerShell Core 6.0 that exist in Windows PowerShell.

Example it allows you to run encrypted commands as well out of the box… Smile

image

 

For a full list of command line options run pwsh.exe /? (or –h or –help or …. Winking smile )

Most relevant are :

pwsh -NoExit -Command Get-Date

image

 

Some of the commands you need to run using Administrator privileges or you get this error :

image

pwsh -Command {Get-WinEvent -LogName security} or pwsh -command “& {Get-WinEvent -LogName security}”

image

 

You can run Windows PowerShell modules in PowerShell Core on Windows

 

Install-Module WindowsPSModulePath -Force -Scope CurrentUser 

Add-WindowsPSModulePath

 

Next episode I will test it the PS Core on an RPI3 running Windows IoT, see here

Enjoy!


PowerShell – Using PoSH SSH Module to access Synology NAS

October 21, 2017

You can now access any SSH device using the PoSH SSH module.

Let’s give it a try using a Synology NAS Device.

Make sure that you enable the SSH Access Service

image

Next make sure you have installed the PoSH-SSH module

Find-Module Posh-SSH

image

Next install it using this command (run as Administrator) :

Find-Module Posh-SSH | Install-Module

After installing you can check the commands available

Get-Command -Module Posh-SSH

image

Now you are ready to make a connection Smile

New-SSHSession -ComputerName YourNASDeviceNAME

image

You can check the session info like this

Get-SSHSession
Get-SSHTrustedHost | fl

image

Let’s run some commands

Invoke-SSHCommand -SessionId 0 -Command "ls -all" | select-Object output -expandproperty output | fl

image

Or using this format :

$result = Invoke-SSHCommand -Index 0 -Command "uname -a; cd /; pwd; ls /volume1" 
$result.Output

image

$result = Invoke-SSHCommand -Index 0 -Command "uname -a" 
$result.Output

image

To stop the session use this command :

Remove-SSHSession -Index 0 -Verbose

image

Enjoy !!


Windows Core IoT – How to check the PowerShell Version and DotNet Version on

December 31, 2016

Just run this command and check your version.

 

image

 

As you can see that Windows 10 IoT core has PSEdition already installed.

Not sure yet if this is the same as DotNet Core ?

 

Best check for regular updates using the Web Interface.

image


Windows Core IoT – Accessing Raspberry PI3 – WMI Classes remotely using WBEM

December 30, 2016

Start using Powershell on Windows IoT on a Raspberry PI3

If you check all the available COM objects that are installed on the Windows 10 Core IoT, we see that WMI and WBEM is installed.

List all COM Objects like this.

image

This enables opportunities for Scripters to connect remote to access all available WMI classes Smile.

But unfortunately I always got an error message like this :

The RPC server is unavailable 0x800706BA

Solution :

First connect using WSMAN to connect to the RPI3 using PS

See my previous post Running Powershell Scripts on Windows 10 Core IoT installed on a Raspberry PI3

image

 

Once the Firewall is set correct, you have the way open to access the WMI classes remotely, without having to first access the local PI3 using WSMAN Winking smile

So let’s test it accessing the EventLogs on Windows IoT

image

Now that we have remote access we can use PS to list all EventLog Errors in a GRID to check what is going wrong.

image

 

All log files are located here :

image

You can also use other Scripting languages like VBScript

strComputer = “rpi3”
Set objSWbemLocator = CreateObject(“WbemScripting.SWbemLocator”)
Set objSWbemServices = objSWbemLocator.ConnectServer(strComputer, _
“Root\CIMv2”, _
“rpi3\administrator”, _
“P@ssW0rd”)

Set colItems = objSWbemServices.ExecQuery(“Select * from Win32_ComputerSystem”,,48)

For Each objItem in colItems
Wscript.Echo strComputer & ” responded.”
Wscript.Echo “Manufacturer: ” & objItem.Manufacturer
WScript.Echo “Model: ” & objItem.Model
Wscript.Echo “Status ” & objItem.Status
Wscript.Echo “SystemType: ” & objItem.SystemType
next

image

Or AU3 to remotely access the WMI Classes on the PI3 Smile

image

 

$StrCmp = "rpi3"
$StrUser = "rpi3\administrator"
$StrPswd = "P@ssW0rd"

$wbemFlagReturnImmediately = 0x10
$wbemFlagForwardOnly = 0x20

Local $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc")

$strComputer = "rpi3"
$objSWbemLocator = ObjCreate("WbemScripting.SWbemLocator")
$objSWbemServices = $objSWbemLocator.ConnectServer($StrCmp, "root\cimv2", $StrUser, $StrPswd)


$colItems = $objSWbemServices.ExecQuery("Select * from Win32_ComputerSystem", "WQL", $wbemFlagReturnImmediately + $wbemFlagForwardOnly)

For $objItem in $colItems
          ConsoleWrite($strComputer & " responded." & @CRLF)
          ConsoleWrite("Manufacturer: " & $objItem.Manufacturer & @CRLF)
          ConsoleWrite("Model: " & $objItem.Model & @CRLF)
          ConsoleWrite("Status " & $objItem.Status & @CRLF)
          ConsoleWrite("SystemType: " & $objItem.SystemType & @CRLF)
next

Func _ErrFunc($oError)
    ; Do anything here.
    ConsoleWrite(@ScriptName & " (" & $oError.scriptline & ") : ==> COM Error intercepted !" & @CRLF & _
            @TAB & "err.number is: " & @TAB & @TAB & "0x" & Hex($oError.number) & @CRLF & _
            @TAB & "err.windescription:" & @TAB & $oError.windescription & @CRLF & _
            @TAB & "err.description is: " & @TAB & $oError.description & @CRLF & _
            @TAB & "err.source is: " & @TAB & @TAB & $oError.source & @CRLF & _
            @TAB & "err.helpfile is: " & @TAB & $oError.helpfile & @CRLF & _
            @TAB & "err.helpcontext is: " & @TAB & $oError.helpcontext & @CRLF & _
            @TAB & "err.lastdllerror is: " & @TAB & $oError.lastdllerror & @CRLF & _
            @TAB & "err.scriptline is: " & @TAB & $oError.scriptline & @CRLF & _
            @TAB & "err.retcode is: " & @TAB & "0x" & Hex($oError.retcode) & @CRLF & @CRLF)
EndFunc   ;==>_ErrFunc

Build your first Console App for an ARM Architecture:

If you are interested in taking it a few steps further, you can here how to build a Console App EXE for IoT.

Happy Scripting !


Windows Core IoT – Running PowerShell scripts against a Raspberry PI3

December 29, 2016

Microsoft released a Windows 10 IoT Core version for Raspberry PI 2 & 3.

To install this it just go here select your hardware follow the steps, and  it will go as a piece of a PI  Winking smile

 

image

Windows IoT Web Interface

Once you have the Windows Core IoT running on your RPI you can access it over the web

IP address + Port 8080

image

Access using the Windows 10 Client App

Don’t forget to switch on the REMOTE SERVER switch first.

image

Powershell access to Windows IoT on as PI3

Next you can connect to it using Powershell

Run the PS IDE as administrator and run this command.

cls

net start WinRM

Set-Item WSMan:\localhost\Client\TrustedHosts -Value rpi3

Enter-PSSession -ComputerName rpi3 -Credential administrator

Once logged in remotely using PS WSMAN connection, see the device name appearing.

image

You can check the PS Version : Check PS Version

Windows IoT Dashboard Application

You can also use the dashboard to launch a PS Session

image

File Sharing and internet connection Sharing

https://developer.microsoft.com/en-us/windows/iot/docs/windowsfilesharing

https://developer.microsoft.com/en-us/windows/iot/docs/internetconnectionsharing

You can also connect to the file system using a regular Share access.

image

 

Starting the FTP server on IOT

https://developer.microsoft.com/en-us/windows/iot/docs/ftp

Use PS and type start C:\Windows\System32\ftpd.exe

Connect using SSH Terminal

https://developer.microsoft.com/en-us/windows/iot/docs/ssh

Example see here

Windows 10 IoT Core Command Line Utils

https://developer.microsoft.com/en-us/windows/iot/Docs/CommandLineUtils

Many command line Utils are at your disposal :

image

You can run regular PS Commands against the RPI3 Smile

image

There is also a screen capturer to make print screens : Screencapture.exe

image

image

Example

image

 

WMI using WMIC Util over WSMAN PS connection

https://technet.microsoft.com/en-us/library/bb742610.aspx

wmic bios

wmic cpu get name

wmic cpu get maxclockspeed

image

Here you can see that the RPI3 runs on an ARM 7 processor at 1200 Mhz

WMI Access from a desktop host using WBEM

This opens a lot of possibilities for us scripters to create apps and retrieve system data and more …

Remote access to PI3 WMI on Windows IoT using WBEM : Example

MS Firewall Logging Setting

You can activate the Firewall logging which makes it easy for debugging.

image

image

AU3 Powershell Module on Raspberry PI3

First copy the libraries on the RPI3

image

Here you can see all the Cmdlet’s from the AU3 Powershell module

 

image

Not that you can do a lot with it, since all the underlying AU3 Dll’s have been compiled using an X86 architecture.

And they will not run on an ARM architecture unfortunately.

If you want to run PowerShell scripts ON a RPI3 and not against an RPI3 see here :

https://audministrator.wordpress.com/2018/07/02/powershell-core-running-on-windows-iot-using-a-rpi3/

Happy Scripting …

Enjoy !