How to check the Powershell Version and DotNet Version on Windows 10 IoT Core

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


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

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 !


Running Powershell Scripts on Windows 10 Core IoT installed on 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

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.

Happy Scripting …

Enjoy !


Powershell – Access Windows Search

February 10, 2015

Windows has a Search feature that integrates in the OS and as well in Outlook.

Using the Control Panel you access the settings, which folders to index and which folders or Email folders.

image

image

To access the Search database you can use the “Search.CollatorDSO” Provider.

CLS

# GENERAL_COLUMNS
#$GENERAL_COLUMNS = "System.Kind"
$GENERAL_COLUMNS = "System.ItemPathDisplay"
#$GENERAL_COLUMNS = "System.ItemFolderPathDisplay"
#$GENERAL_COLUMNS = "System.Size"
#$GENERAL_COLUMNS = "System.DateCreated"
#$GENERAL_COLUMNS = "System.Author"
#$GENERAL_COLUMNS = "System.Keywords"

# DOCUMENT_COLUMNS
#$GENERAL_COLUMNS = "System.Image.HorizontalSize"
#$GENERAL_COLUMNS = "System.Image.VerticalSize"
#$GENERAL_COLUMNS = "System.Image.BitDepth"
#$GENERAL_COLUMNS = "System.Image.Compression"
#$GENERAL_COLUMNS = "System.Photo.CameraModel"
#$GENERAL_COLUMNS = "System.Photo.DateTaken"
#$GENERAL_COLUMNS = "System.Photo.Flash"

# MUSIC_COLUMNS
#$GENERAL_COLUMNS = "System.Music.Artist"
#$GENERAL_COLUMNS = "System.Music.Genre"
#$GENERAL_COLUMNS = "System.Music.TrackNumber"
#$GENERAL_COLUMNS = "System.Audio.Compression"
#$GENERAL_COLUMNS = "System.Audio.SampleRate"
#$GENERAL_COLUMNS = "System.DRM.IsProtected"
#$GENERAL_COLUMNS = "System.Music.AlbumTitle"
#$GENERAL_COLUMNS = "System.Rating"
#$GENERAL_COLUMNS = "System.Audio.EncodingBitrate"

# VIDEO_COLUMNS
#$VIDEO_COLUMNS = "System.RecordedTV.ChannelNumber"
#$VIDEO_COLUMNS = "System.RecordedTV.EpisodeName"
#$VIDEO_COLUMNS = "System.RecordedTV.NetworkAffiliation"
#$VIDEO_COLUMNS = "System.RecordedTV.RecordingTime"
#$VIDEO_COLUMNS = "System.Video.Compression"
#$VIDEO_COLUMNS = "System.Video.EncodingBitrate"
#$VIDEO_COLUMNS = "System.Video.FrameHeight"
#$VIDEO_COLUMNS = "System.Video.FrameWidth"    
  
$keyword1 = "PowerShell"  
$keyword2 = "au3"  
$extension = ".PDF"
$folder = "C:\"  


$objConnection = New-Object -com ADODB.Connection  
$objRecordSet = New-Object -com ADODB.Recordset

$objConnection.Open("Provider=Search.CollatorDSO;Extended Properties='Application=Windows';")  
$objRecordSet.Open("SELECT $GENERAL_COLUMNS FROM SYSTEMINDEX WHERE System.FileExtension = '$extension' `
                    AND (Contains(Contents,'$keyword1') OR Contains(Contents, '$keyword2')) `
                    AND System.ItemPathDisplay LIKE '$folder\%'", $objConnection)  

if ($objRecordSet.EOF -eq $false) {$objRecordSet.MoveFirst() }  
  
while ($objRecordset.EOF -ne $true) {  
  $objRecordset.Fields.Item("System.ItemPathDisplay").Value  
  $objRecordset.MoveNext()  
 }


rv folder, extension, keyword1, keyword2

As you can see there are many columns you can search. Or using some Queries you can run this on your indexed mails

image

The bonus you get it that you can use this technology to do Remote Search over your network on a remote server or PC.

Enjoy !


Powershell – Running Parallel Workflows FAST !

January 9, 2015

Windows Workflows are really great in particular to execute parallel code.

When would you want to use this ?

Let’s take this example, where you want to know all IP addresses of the PC’s in your network.

It you had to scan them 1 by 1 sequentially it would be a SLOW process.

Therefore the new feature of Windows Workflows in PS v3.0 it the tool to use. In combination with the parallel execution parameter.

But you might run in to this error if you are trying to use Parameters in you Workflow statement.

Positional parameters are not supported in a Windows PowerShell Workflow. To invoke this command, use explicit parameter names with all values. For example: “Command -Parameter <value>”.
+ CategoryInfo          : ParserError: (:) [], ParseException
+ FullyQualifiedErrorId : PositionalParametersNotSupported

image

It took me a more then an hour to figure out what this meant and another hour to figure out how to correct it.

I ran this code and got the above error ?

CLS

import-module activedirectory

workflow Invoke-ForEachParallel-WF  {

param([string[]]$computerName) 

 # The contents of the foreach block will be executed in parallel
 # look at the keyword -parallel

 foreach -parallel ($computer in $computerName)
 { # "Executing on $computer"
   test-connection -count 1 $computer
  }

}

Invoke-ForEachParallel-WF  (Get-AdComputer -filter {(Name -Like "srv*") } | select name -ExpandProperty name)

Solution :

CLS

import-module activedirectory

workflow Invoke-ForEachParallel-WF  {

param([string[]]$ComputerName) 

 # The contents of the foreach block will be executed in parallel
 # look at the keyword -parallel

 foreach -parallel ($computer in $computerName)
 { # "Executing on $computer"
   test-connection -count 1 -ComputerName $computer
   # Look at the special syntax -ComputerName + variable $computer
  }

}

Invoke-ForEachParallel-WF  (Get-AdComputer -filter {(Name -Like "srv*") } | select name -ExpandProperty name)

Look at the special extra parameter “-ComputerName” value in the foreach code block

The code runs realy fast and will give you this kind of output.

image

Hope this will save you all some time.


Powershell – Using the .Net Charting Controls

January 4, 2015

Picked this up on the internet Smile Great extension to Powershell and .NET

What you need to get started is the

Microsoft Chart Controls for Microsoft .NET Framework 3.5

The Charting Controls documentation is here :

Microsoft Chart Controls for .NET Framework Documentation

image

Once installed you are ready to go, here is an example script showing the top 5 largest files in a directory.

image

If you hit the “Save button” it will save the graph to the desktop.

Here is the script :

CLS

if([Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization") -eq $null)
{
    #Microsoft Chart Controls are not installed
    [void][reflection.assembly]::Load("System.Windows.Forms, Version=2.0.0.0,`
    Culture=neutral, PublicKeyToken=b77a5c561934e089")
    [void][System.Windows.Forms.MessageBox]::Show("Microsoft Chart Controls for Microsoft .NET 3.5`
    Framework is required", "Microsoft Chart Controls Required")
    #Open the URL
    [System.Diagnostics.Process]::Start("http://www.microsoft.com/downloads/en/`
    details.aspx?familyid=130F7986-BF49-4FE5-9CA8-910AE6EA442C&displaylang=en");
    return $false
}

# load the appropriate assemblies 
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms") 
[void][Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms.DataVisualization")

   
# create chart object 
$Chart = New-object System.Windows.Forms.DataVisualization.Charting.Chart 
$Chart.Width = 500 
$Chart.Height = 450
$Chart.Left = 40 
$Chart.Top = 30
   
# create a chartarea to draw on and add to chart 
$ChartArea = New-Object System.Windows.Forms.DataVisualization.Charting.ChartArea 
$Chart.ChartAreas.Add($ChartArea)


# add data to chart 
$Files = dir $env:USERPROFILE -Recurse -File | sort length  | select -last 5 # | % {"$_.Name " + [math]::Round(($_.Length / 1MB),2)}
$FileNames = @(foreach($File in $Files){$File.Name}) 
$FileSize = @(foreach($File in $Files){[math]::Round(($File.Length / 1MB),2)}) 


[void]$Chart.Series.Add("Data") 
$Chart.Series["Data"].Points.DataBindXY($FileNames, $FileSize)

# add title and axes labels 
[void]$Chart.Titles.Add("Top 5 Files - Descending") 
$ChartArea.AxisX.Title = "File Name" 
$ChartArea.AxisY.Title = "File Size - [MB]"

# Find point with max/min values and change their colour 
$maxValuePoint = $Chart.Series["Data"].Points.FindMaxByValue() 
$maxValuePoint.Color = [System.Drawing.Color]::Red 

$minValuePoint = $Chart.Series["Data"].Points.FindMinByValue() 
$minValuePoint.Color = [System.Drawing.Color]::Green

# change chart area colour 
$Chart.BackColor = [System.Drawing.Color]::Transparent

# make bars into 3d cylinders 
$Chart.Series["Data"]["DrawingStyle"] = "Cylinder"

# display the chart on a form 
$Chart.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right -bor 
                [System.Windows.Forms.AnchorStyles]::Top -bor [System.Windows.Forms.AnchorStyles]::Left

# sorting the dataset
$Chart.Series["Data"].Sort([System.Windows.Forms.DataVisualization.Charting.PointSortOrder]::Descending, "Y")

# add a save button 
$SaveButton = New-Object Windows.Forms.Button 
$SaveButton.Text = "Save" 
$SaveButton.Top = 500 
$SaveButton.Left = 450 
$SaveButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right 
$SaveButton.add_click({$Chart.SaveImage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")}) 


# save chart to file 
$Chart.SaveImage($Env:USERPROFILE + "\Desktop\Chart.png", "PNG")

$Form = New-Object Windows.Forms.Form 
$Form.Text = "PowerShell Chart" 
$Form.Width = 600 
$Form.Height = 600 
$Form.controls.add($Chart) 
$Form.Add_Shown({$Form.Activate()}) 

$Form.controls.add($SaveButton)

$Form.ShowDialog()

A good way to start is reading here :

http://blogs.technet.com/b/richard_macdonald/archive/2009/04/28/3231887.aspx

You can build some nifty Tools with this ?

image

Enjoy!


Powershell – Output to a GUI

January 4, 2015

Ever wanted to get the output of PowerShell to a GUI.

Well that’s not so difficult, since PS is built on top op .NET you can use the .NET Forms Class for this.

This example will capture your Computer Name, and the Process list and display it is a list box.

CLS

# add a Listbox
$objListBox = New-Object Windows.Forms.Listbox
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(15,10)
$objListBox.Size = New-Object System.Drawing.Size(550,10)
$objListBox.Height = 400

# add a save button
$oButton = New-Object Windows.Forms.Button
$oButton.Text = "Get Computer Name"
$oButton.Top = 500
$oButton.Left = 350
$oButton.Width = 150
$oButton.Anchor = [System.Windows.Forms.AnchorStyles]::Bottom -bor [System.Windows.Forms.AnchorStyles]::Right 

$oButton.add_click({$SaveButton.Text = $env:ComputerName})
$oButton.add_click({$objListBox.Items.Add($env:ComputerName)})

$oButton.add_click({

$sTemp = Get-Process 

$sTemp | % {  $item = $_.ProcessName
$objListBox.Items.Add($item) }

})

# Build the GUI
$Form = New-Object Windows.Forms.Form
$Form.Text = "PowerShell Output"
$Form.Width = 600
$Form.Height = 600 

$Form.Add_Shown({$Form.Activate()}) 

$Form.controls.add($oButton)
$Form.controls.add($objListBox)

$Form.ShowDialog()

rv objListBox, Form, oButton, sTemp, item

When pressing the button it will trigger the actions Winking smile

Enjoy!