AutoIT – Meets PowerShell using .NET Common Language Runtime (CLR) Framework

August 9, 2017

Since we AutoIT have got access the to the .NET Common Language Runtime (CLR) Framework 

We can access the PowerShell Automation Classes as well.

This is really powerfull stuff … because we can reuse .NET code as well as all the native CMDlet’s and scripts.

As you can see the script runs an PowerShell command that lists all running processes on your machine. And return the Output to a .NET Grid.

image

Which runs inside our Unmanaged Host, isn’t that cool …Smile

We can access native .NET Classes as well because PS runs on top off it.

Next is that you could access any kind of Cloud Web Service using the multitude of PS Modules that are available.

As well as accessing Custom Build Assemblies.

How to :

For more information on how to access .NET Classes see the above post about .NET Common Language CLR Framework.

Or Join the most friendly and helpful AutoIT Community on the net Smile

I will post some more examples and How To’s later on, so stay tuned …

Advertisements

Windows – WMI to access devices over SNMP – Error Invalid Namespace

January 20, 2015

I wanted to run an old script of mine to read out some network printer information using WMI – SNMP.

But I got an error saying that “Invalid Namespace“ Sad smile

Solutions :

You need to add the SNMP WMI providers to your machine.

1. Go to the Control Panel
2. Next select Programs and Features
3. Select Simple Network Management Protocol (SNMP)
4. Select WMI SNMP Provider

image

Once installed all errors are gone Smile

; Initialize error handler
$oMyError = ObjEvent("AutoIt.Error","MyErrFunc")   $iIP = 31   For $iIP = 31 To 45 ; IP range   $strTargetSnmpDevice = "10.0.0." & $iIP
 ConsoleWrite("Target Device : " & $strTargetSnmpDevice & @CRLF)
 ConsoleWrite("-------------" & @CRLF)   $objWmiLocator = ObjCreate("WbemScripting.SWbemLocator")
 $objWmiServices = $objWmiLocator.ConnectServer("", "root\snmp\localhost")   $objWmiNamedValueSet = ObjCreate("WbemScripting.SWbemNamedValueSet")
 $objWmiNamedValueSet.Add ("AgentAddress", $strTargetSnmpDevice)
 $objWmiNamedValueSet.Add ("AgentReadCommunityName", "public")   $colIfTable = $objWmiServices.InstancesOf("SNMP_RFC1213_MIB_ifTable",Default , $objWmiNamedValueSet)   For $objInterface In $colIfTable
    ConsoleWrite ("ifIndex [Key]:        " & $objInterface.ifIndex  & @CRLF & _
        "   ifAdminStatus:     " & $objInterface.ifAdminStatus 		& @CRLF & _
        "   ifDescr:           " & $objInterface.ifDescr         	& @CRLF & _
        "   ifInDiscards:      " & $objInterface.ifInDiscards    	& @CRLF & _
        "   ifInErrors:        " & $objInterface.ifInErrors      	& @CRLF & _
        "   ifInNUcastPkts:    " & $objInterface.ifInNUcastPkts   	& @CRLF & _
        "   ifInOctets:        " & $objInterface.ifInOctets      	& @CRLF & _
        "   ifInUcastPkts:     " & $objInterface.ifInUcastPkts 		& @CRLF & _
        "   ifInUnknownProtos: " & $objInterface.ifInUnknownProtos 	& @CRLF & _
        "   ifLastChange:      " & $objInterface.ifLastChange     	& @CRLF & _
        "   ifMtu:             " & $objInterface.ifMtu            	& @CRLF & _
        "   ifOperStatus:      " & $objInterface.ifOperStatus     	& @CRLF & _
        "   ifOutDiscards:     " & $objInterface.ifOutDiscards 		& @CRLF & _
        "   ifOutErrors:       " & $objInterface.ifOutErrors      	& @CRLF & _
        "   ifOutNUcastPkts:   " & $objInterface.ifOutNUcastPkts  	& @CRLF & _
        "   ifOutOctets:       " & $objInterface.ifOutOctets      	& @CRLF & _
        "   ifOutQLen:         " & $objInterface.ifOutQLen        	& @CRLF & _
        "   ifOutUcastPkts:    " & $objInterface.ifOutUcastPkts   	& @CRLF & _
        "   ifPhysAddress:     " & $objInterface.ifPhysAddress 		& @CRLF & _
        "   ifSpecific:        " & $objInterface.ifSpecific       	& @CRLF & _
        "   ifSpeed:           " & $objInterface.ifSpeed          	& @CRLF & _
        "   ifType:            " & $objInterface.ifType           	& @CRLF)
		ConsoleWrite("" & @CRLF)
 Next
ConsoleWrite("" & @CRLF)
Next

Output is like this :

image

For Powershell it looks a bit different:


CLS

For ($iIP=32; $iIP -le 45; $iIP++){

    $CommunityString = "public"
    $IPAddress     = "10.0.0."+$iIP
    write-host $IPAddress

    $Opt = new-object Management.ConnectionOptions
    $Opt.Impersonation = 'Impersonate'

    $context = new-object management.ManagementNamedValueCollection
    $context.Add("AgentAddress", $IPAddress)
    $context.Add("AgentReadCommunityName", $CommunityString)

    $ogo =  new-object management.ObjectGetOptions($context, (new-object TimeSpan(0,0,0,5)), $true)

    $scope = new-object management.ManagementScope("root\snmp\localhost", $Opt)

    $query = new-object Management.ObjectQuery("select * from SNMP_RFC1213_MIB_system")

    $searcher = new-object Management.ManagementObjectSearcher($scope,$query)
    $searcher.Options.Context = $context

    $searcher.get()

}

Output is like this :

image

If you want to access devices like routers, switches, printers etc. using Native SNMP protocol.

You will need some tools to get hold of their public or private MIB Object ID’s.

Once of the many tools out there is Softperfect Network scanner.

image

Maybe I will address this later how to access the network Devices using native SNMP protocol Winking smile

Enjoy !


Windows – Install TTF Barcode Fonts

August 28, 2014

I had to install some barcode fonts to be used in Word and Excel for Label printing.

After installing it will look like this.

image

Some of the fonts are free to download from then net, like (Barcode 39 TTF), others are not.

Installing is normally a piece of cake. Right click the font file and choose Install.

image

But in some PC’s the file association has changed because of other applications being installed.

So there is no Install Sad smile

image

To find the associated application on the machine, use assoc.exe

image

Indeed you see it here :

HKCU\Software\Microsoft\Windows\CurrentVersion\Explorer\FileExts

image

To find what was the original file association, have a look here.

http://dhetricks.blogspot.be/2012/01/windows-7-file-association-fix.html

You see it was associated with Windows Fonts Viewer.

Being here : C:\Windows\System32\Fontview.exe

Solution :

Let fix it using AutoIT

#RequireAdmin

FileExtAssoc("ttf", "Fontview.exe")

func FileExtAssoc($sExt, $sApplication)
    RunWait(@COMSPEC & " /c ASSOC ." & $sExt & "=ExTest", "", @SW_HIDE)
    RunWait(@COMSPEC & " /c FTYPE ExTest=" & $sApplication , "", @SW_HIDE)
    MsgBox(0,"File Extension Application Association",'"' & $sExt & '"is now asscoiated with "' & $sApplication & '"',3)
EndFunc

Or using the GUI way like this.

image

image

image

Enjoy !


AutoIT – Visual Studio Light

August 24, 2014

Sometime you find some nice code on the internet like VB.Net or C#.

But it is of no use if it is not compiled Sad smile

As explained in this post you can compile you own .NET COM objects on the fly.

But if you want to do it using a GUI interface without installing the full blown Visual Studio. You can use this GUI Wrapper – Visual Studio Light

image

The first option is for compiling GUI Applications EXE’s.

The second one is to create DLL’s (Libraries) which you can use later on in a .Net application.

The third one is for compiling Console Applications EXE’s.

You can download it here

PS :  This wrapper is for VB.Net but can easily be changed to use C# replace the vbc.exe by csc.exe

If you run it you get an EXE or DLL

image

image

This is the code :

imports System.Windows.Forms

' The name of the module must match the name of the code item
' created in the hosting application via CreateItem.
module Script
  sub Main()
    MessageBox.Show("hi From DotNET !!")
  end sub
end module

How to run .Net Assemblies in Powershell see here

How to run .Net Assemblies form Memory in Powershell see here

Enjoy !


AutoIT – How to compile your own .NET COM Object

August 24, 2014

Wouldn’t it be nice to compile your own .NET COM DLL and use it on AutoIT.

Well we don’t need much to do this. Everything is available on your system.

Let’s see :

First we need of course some VB.Net code or C# that will exposes some methods or properties.

Imports System.Collections.Generic
Imports System.Text
Imports System.Runtime.InteropServices

Namespace myDotNetLibrary
  <classinterface   (CLASSINTERFACETYPE.AUTODUAL)> _
  Public Class myDotNetClass
    Private myProperty As String

    Public Sub New()
    End Sub

    Public Function myDotNetMethod(input As String) As String
      Return "Hello " & input
    End Function

    Public Property myDotNetProperty() As String
      Get
        Return myProperty
      End Get
      Set(ByVal value As String)
        myProperty = value
      End Set
    End Property

  End Class
End Namespace

Next we need a .Net compiler ? Ha but that is default available on each system that has a .Net Framework installed Smile

Go and have a look here : C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe

The vbc.exe is the compiler for the .Net 2.0 framework. But you will find as well the compiler for .NET 3.0 and 4.0 if you have that framework installed.

So now we need to compile the code to a DLL and register it.

#RequireAdmin

; Framework 2.0
;$vbc = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\vbc.exe" ;  ; check the path of your version
;$RegAsm = "C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\RegAsm.exe" ; check the path of your version
; Framework 4.0
$vbc = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\vbc.exe" ;  ; check the path of your version
$RegAsm = "C:\WINDOWS\Microsoft.NET\Framework\v4.0.30319\RegAsm.exe" ; check the path of your version

RunWait($vbc & " /target:library hello.vb", @ScriptDir, @SW_HIDE) ; compile the .net DLL
RunWait($RegAsm & " /codebase hello.dll", @ScriptDir, @SW_HIDE) ; register the .net DLL

$obj = ObjCreate("myDotNetLibrary.myDotNetClass")
$obj.myDotNetProperty = " ... from DotNet to the AutoIt World !"

MsgBox(0,"My Own DotNet Object " , $obj.myDotNetMethod($obj.myDotNetProperty) & @CRLF)

RunWait($RegAsm & " /unregister hello.dll", @ScriptDir, @SW_HIDE) ; unregister the .net DLL

Ones you run the code it will compile the code to a DLL and register it in the GAC

Or if you want don’t want to register the COM Assembly to the GAC, you can use this approach 😉

 

image

And run it as a COM object, where AU3 will access it’s methods and properties.

image

You can see if the COM object registered correctly here :

image

For a C# compiler you need to look for the csc.exe instead of the vbc.exe

Enjoy!


PowerShell – Module for AutoIT

August 24, 2014

In the last Beta releases AutoIT is shipped with a .NET Assembly as well as a Powershell Module

You can find it here :

image

First import the module and run this example.

CLS

# Import the module manfiest
Import-Module (${Env:ProgramFiles(x86)} + "\AutoIt3\Beta\AutoItX\AutoItX3.psd1")

Invoke-AU3Run("Notepad")

Wait-AU3WinActive("Untitled")

Send-AU3Key("I'm in notepad");

$winHandle = Get-AU3WinHandle("Untitled");

Sleep -Seconds 2

Close-AU3Win($winHandle)

# Get the list of AutoItX cmdlets
Get-Command *AU3*

# Get detailed help for a particular cmdlet
Get-Help Get-AU3WinHandle

Getting help on the cmdlets and commands using the Get-Command and Get-Help will list you all needed information to get started.

image

image

Even if the functionality is limited at the moment, it is a good sign AU3 is moving towards PS. Smile


PowerShell – The ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered on the local machine

August 22, 2014

If you get this error

Exception calling “Open” with “0” argument(s): “The ‘Microsoft.Jet.OLEDB.4.0’ provider is not registered on the local machine.”

image

Background
Microsoft OLEDB Drivers work on 32 or 64 bit architecture, running Windows XP, Vista, 7 or Windows 8.

Some of the drivers are only 32 Bit compatible ! like this above one.

Additionally on some Windows 64 bit systems, it is possible that some of the Microsoft OLEDB DLLs have not been registered.

Solution No. 1

Check that you are running the 32 bit PowerShell Console or ISE. In the past with Powershell v1.0 you could start it form here :

image

 

 

 

 

 

 

 

But later on after upgrading to v3.0 it disappeared.There are still 2 hints where you can see if you are running x64 or x86.After starting the ISE

image

you will see it here in the Title Bar it says … x86image

The command window you see the location SysWOW64

You can start it from here :

image

The Solution No. 2.
The solution is to manually register those DLLs.
go to Start->Run and type cmd
this starts the Command Prompt

Go to a special folder
cd c:\windows\sysWOW64

Now you need to register the OLE DB 4.0 DLLs by typing these commands and pressing return after each. Might be only the first 2 will register, no problem.

regsvr32 Msjetoledb40.dll
regsvr32 Msjet40.dll
regsvr32 Mswstr10.dll
regsvr32 Msjter40.dll
regsvr32 Msjint40.dll

http://support.microsoft.com/kb/278604

image

If errors appear.

Install AccessDatabaseEngine.exe from the MS site and try again.

http://www.microsoft.com/en-us/download/details.aspx?id=13255

x86 for MS Office x32 bit and

x64 for MS Office x64 bit

image

Hope this will help solving some frustrations.