AutoIT – Accessing PowerShell using COM Object

January 23, 2017

AutoIT is COM enabled and more … so we can use the the SAPIEN ActiveXPosh COM Object to run Powershell code.

Or even to access .NET Assemblies …

You can use this COM object to run in any COM Compatible scripting host, like VBScript, Word, Excel, Etc …

 

First you need to download the FREE PS Com object here :

Look at the bottom of the site after login for SUPPORT next SUPPORT DOWNLOADS, Next see folder FREE COMPONENTS
http://www.sapien.com/downloads#

 

Here are some examples translated to AU3

;**************************************************************************
; Copyright (c) SAPIEN Technologies, Inc. All rights reserved
; This file is part of the PrimalScript 2007 Code Samples.
;
; File: ActiveXposh.vbs
;
; Comments:
;
; Disclaimer: This source code is intended only as a supplement to
;   SAPIEN Development Tools and/or on-line documentation.
;   See these other materials for detailed information
;   regarding SAPIEN code samples.
;
; THIS CODE AND INFORMATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
; KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
; IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
; PARTICULAR PURPOSE.
;**************************************************************************

#AutoIt3Wrapper_UseX64=N

Dim $ActiveXPosh

Const $OUTPUT_CONSOLE = 0
Const $OUTPUT_WINDOW = 1
Const $OUTPUT_BUFFER = 2

Func CreateActiveXPosh()
Dim $success

; Create the PowerShell connector object
$ActiveXPosh = ObjCreate("SAPIEN.ActiveXPoSH")
$success = $ActiveXPosh.Init(False) ;Do not load profiles
If $success <> 0 then
Consolewrite( "Init failed" & @CR)
endif
If $ActiveXPosh.IsPowerShellInstalled Then
Consolewrite( "Ready to run PowerShell commands" & @CR)
Else
Consolewrite( "PowerShell not installed" & @CR)
EndIf
; Set the output mode
$ActiveXPosh.OutputMode = $OUTPUT_CONSOLE
EndFunc


Func DownloadFile($URL,$Destination)
Local $Return
Dim $Command
Dim $FSO

;Download a file with PowerShell
$ActiveXPosh.Execute("$Client = new-object System.Net.WebClient")
;Note that variables are preserved between calls
; Construct a $Command
$Command = "$Client.DownloadFile('" & $URL & "','" & $Destination & "')"
ConsoleWrite ("Downloading ..." & @CR)
$ActiveXPosh.Execute($Command)

$FSO = ObjCreate("Scripting.FileSystemObject")
If $FSO.FileExists($Destination) Then
ConsoleWrite ("File transfer complete" & @CR)
Else
ConsoleWrite ("File Transfer failed" & @CR)
EndIf
Return $Return
EndFunc

Func ListServices()
Dim $outtext
; Set the $OUTPUT mode to $BUFFER
$ActiveXPosh.OutputMode = $OUTPUT_BUFFER
$ActiveXPosh.Execute("Get-WmiObject -class Win32_Service | Format-Table -property Name, State")

; Get the $OUTPUT line by line and add it to a variable
For $str In $ActiveXPosh.OUTPUT()
$outtext = $outtext & $str
$outtext = $outtext & @CRLF
Next

; Alternatively you can get the $OUTPUT as a single String
; $outtext = $ActiveXPosh.OutputString

ConsoleWrite ($outtext & @CR)
$ActiveXPosh.ClearOutput() ; Empty the $OUTPUT $BUFFER
EndFunc

; Create the actual Object
CreateActiveXPosh()

$Status = $ActiveXPosh.GetValue("(Get-Service UPS).Status")
if($Status = "Stopped") then
ConsoleWrite ("UPS Service is stopped" & @CR)
else
ConsoleWrite ("UPS Service is " & $Status & @CR)
EndIf


; List all running processes using PowerShell
$ActiveXPosh.Execute("Get-Process")

; Check if WinWord is running using PowerShell
if $ActiveXPosh.Eval("get-process winword") = 1 Then
ConsoleWrite ("Microsoft Word is running" & @CR)
Else
ConsoleWrite ("Microsoft Word is not running" & @CR)
EndIf

DownloadFile ("[url="http://izzy.org/scripts/PowerShell/activexposh.pdf","C:\Temp\activexposh.pdf"]http://izzy.org/scripts/PowerShell/activexposh.pdf","C:\Temp\activexposh.pdf[/url]")

; Use ListServices to show all services in this machine using PowerShell()
ListServices()

ConsoleWrite ("Version " & $ActiveXPosh.GetValue("$PSHOME") & @CR)

$ActiveXPosh = ""

 

Enjoy !

Advertisements

Windows Core IoT – Accessing a DotNet DLL assembly on a Raspberry PI3 using Windows Core IoT

January 23, 2017

In this post I showed how to create an Console app using my Visual Studio Light for ARM, to run on a Raspberry PI3 on Windows Core IoT.

Taking it one step further is that you access an DotNet Assembly that was created before, and seem to run as well on X86 as on ARM Smile

So most Assembly DLL’s seem to be architecture independend …

 

image

 

Let’s see here :

Using ILSpy you can see the source of the self made .NET Assembly.

image

 

image

I copied the DLL Assembly to the RPI3, did a remote access from PS IDE and ran the following code.

image

 

As you can see it ran without any issues ! the result was returned as expected … Smile

Enjoy !


Windows 10 – How to install Mono on Bash / Ubuntu

January 14, 2017

To get going first go here :

http://www.mono-project.com/docs/getting-started/install/windows/

Next read the steps to install it :

https://github.com/mono/website/issues/199

1. Installing the Mono Framework

image

image

image

image

After installation the Mono Framework is located in :

image

/usr/lib/mono/xxx

image

 

2. Testing the DotNet Framework

Once installed you can test it using the Hello Mono World Example :

http://www.mono-project.com/docs/getting-started/mono-basics/

First copy the Hello.cs to the Root folder (keep in mind that File Names are CASE SENSITIVE in Linux)

image

You can as well build GUI applications, but run it make sure you have the Xserver running first.

image

Otherwise you will see this error appearing.

image

 

So now we know there is a DotNet Framework installed on Linux, we can start scripting Winking smile

…. What about PowerShell on Linux or Mac … ?

Well after you got the DotNET installed on Linux you can add PowerShell too 🙂

Start here :

– https://blogs.msdn.microsoft.com/powershell/2016/08/18/powershell-on-linux-and-open-source-2/

– https://github.com/powershell/powershell

– http://www.sqlservercentral.com/blogs/anthony-nocentinos-blog/2016/08/18/getting-started-with-powershell-on-linux/

– http://www.sqlservercentral.com/blogs/steve_jones/2016/08/19/posh-on-ubuntu/

Enjoy !