ComatePlus & Powershell (ActiveXPosh)

Just starting out? Need help? Post your questions and find answers here.
User avatar
jackymb
User
User
Posts: 16
Joined: Wed Aug 11, 2004 7:37 pm
Location: AIX en PROVENCE (France)

ComatePlus & Powershell (ActiveXPosh)

Post by jackymb »

Hello,

Many Windows Business Application are now only supporting Powershell, as a scripting environment.
Like MS Exchange / SharePoint / Active Directory / MS SQL / etc.

Since 2008 Sapien Technologies has released a Powershell COM component named ActiveXPoSH, that makes the bridge between all
Download here http://www.sapien.com/blog/2008/06/25/a ... -download/
First register an account before getting the download access http://www.sapien.com/auth

Here is the example code in vbscript:

Code: Select all

'**************************************************************************
'
'	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.
'
'**************************************************************************
Dim ActiveXPosh

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

Function CreateActiveXPosh()
	Dim success

	' Create the PowerShell connector object
	Set ActiveXPosh = CreateObject("SAPIEN.ActiveXPoSHV3")
	success = ActiveXPosh.Init(vbFalse) 'Do not load profiles
	If success <> 0 then
		WScript.Echo "Init failed"
	end if
	If ActiveXPosh.IsPowerShellInstalled Then
	 WScript.Echo "Ready to run PowerShell commands"
	Else
	 WScript.Echo "PowerShell not installed"
	End If
	'Set the output mode 
	ActiveXPosh.OutputMode = OUTPUT_CONSOLE
	'ActiveXPosh.OutputMode = OUTPUT_WINDOW
End Function

Function DownloadFile(URL,Destination)
	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 & "')"
	WScript.Echo "Downloading ..."
	ActiveXPosh.Execute(Command)
	
	Set FSO = CreateObject("Scripting.FileSystemObject")
	If FSO.FileExists(Destination) Then
		WScript.Echo "File transfer complete"
	Else
		WScript.Echo "File Transfer failed"
	End If
End Function

Function 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 Each str In ActiveXPosh.Output 
	 	outtext = outtext & str
	 	outtext = outtext & VbCrLf
	Next
	
	' Alternatively you can get the output as a single String
'	outtext = ActiveXPosh.OutputString
	
	WScript.Echo outtext
	ActiveXPosh.ClearOutput() ' Empty the output buffer
End Function

' Create the actual Object
CreateActiveXPosh

Status = ActiveXPosh.GetValue("(Get-Service DnsCache).Status")
if(Status = "Stopped") then
   WScript.Echo "DnsCache Service is stopped"
else
   WScript.Echo "DnsCache Service is " & Status
end If


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

' Check if WinWord is running using PowerShell
if ActiveXPosh.Eval("get-process winword") = vbTrue Then
	WScript.Echo "Microsoft Word is running"
Else
	WScript.Echo "Microsoft Word is not running"
End If

'DownloadFile "http://support.sapien.com/bulletins/sb563.pdf","C:\Temp\sb563.pdf"

' Use  ListServices to show all services in this machine using PowerShell
ListServices

WScript.Echo ActiveXPosh.GetValue("$PSHOME")
Set ActiveXPosh = Nothing
I started to translate it into PB, but I can not seem to do a code that works properly.

Here is the beginning of translated code:

Code: Select all

XIncludeFile #PB_Compiler_Home + "..\MyInclude\COMatePLUSV5x\COMatePLUS.pbi"

#OUTPUT_CONSOLE = 0
#OUTPUT_WINDOW = 1
#OUTPUT_BUFFER = 2

Global oActiveXPosh.COMateObject 

oActiveXPosh = COMate_CreateObject("SAPIEN.ActiveXPoSHV3")
If COMate_GetLastErrorCode() = #S_OK
	Debug "Object  Ok" 
Else
 	Debug "Error !!!"
EndIf
Debug "0: " + COMate_GetLastErrorCode() + " - " +COMate_GetLastErrorDescription() 
 
If oActiveXPosh\Invoke("(Init()") = #S_OK ;Do Not load profiles
	Debug "Init Ok "
Else
	Debug "Init Failed"
	Debug "1: " + COMate_GetLastErrorCode() + " - " +COMate_GetLastErrorDescription() 
EndIf
	
If oActiveXPosh\GetIntegerProperty("IsPowerShellInstalled")
		 Debug "Ready to run PowerShell commands"
Else
	Debug "PowerShell not installed"
EndIf
Debug "2: " + COMate_GetLastErrorCode() + " - " +COMate_GetLastErrorDescription() 
	
	;Set the output mode 
	oActiveXPosh\SetProperty("OutputMode=OUTPUT_CONSOLE")
Debug "3: " + COMate_GetLastErrorCode() + " - " +COMate_GetLastErrorDescription() 
	
oActiveXPosh\Release()  
Debug "--- End ---"
I add the commands : COMate_GetLastErrorCode() and COMate_GetLastErrorDescription() to see what is going well at each step.

I stopped at the beginning because I have a lot of error.

Thank you to those who are going to help me
Windows 10 x64/86 - PB 5.73LTS (x86 & x64)
_________________________________________
~English is not my native language I'm using a translator~
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Kiffi »

Sorry, but the download-link in the Blog leads to PrimalScript and i don't
want to install a complete IDE to use a single ActiveX-Component.

But at a first sight:

Code: Select all

If oActiveXPosh\Invoke("(Init()") = #S_OK ;Do Not load profiles
should be

Code: Select all

If oActiveXPosh\Invoke("Init(0)") = #S_OK ;Do Not load profiles
Greetings ... Peter
Hygge
User avatar
jackymb
User
User
Posts: 16
Joined: Wed Aug 11, 2004 7:37 pm
Location: AIX en PROVENCE (France)

Re: ComatePlus & Powershell (ActiveXPosh)

Post by jackymb »

@kiffi

Thank you for your reply.

must go down the page and click: Download Support.
But I can send you a PM with a link.

in fact there is a mistake because I had put '#FALSE', but it does not operate at

Code: Select all

If oActiveXPosh\Invoke("(Init(#FALSE)") = #S_OK
Greetings - Jacky
Windows 10 x64/86 - PB 5.73LTS (x86 & x64)
_________________________________________
~English is not my native language I'm using a translator~
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Kiffi »

jackymb wrote:But I can send you a PM with a link.
yes, that would be helpful.
jackymb wrote:If oActiveXPosh\Invoke("(Init(#FALSE)") = #S_OK
:wink:

Greetings ... Peter
Hygge
User avatar
Kiffi
Addict
Addict
Posts: 1485
Joined: Tue Mar 02, 2004 1:20 pm
Location: Amphibios 9

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Kiffi »

this works for me:

Code: Select all

XIncludeFile #PB_Compiler_Home + "..\MyInclude\COMatePLUSV5x\COMatePLUS.pbi"

EnableExplicit

#OUTPUT_CONSOLE = 0
#OUTPUT_WINDOW = 1
#OUTPUT_BUFFER = 2

Define oActiveXPosh.COMateObject 

oActiveXPosh = COMate_CreateObject("SAPIEN.ActiveXPoSHV3")

If oActiveXPosh
	
	If oActiveXPosh\Invoke("Init(0)") = #S_OK ;Do Not load profiles
		
		Debug "Init Ok "
		
		If oActiveXPosh\GetIntegerProperty("IsPowerShellInstalled")
			
			Debug "Ready to run PowerShell commands"
			
			oActiveXPosh\SetProperty("OutputMode=" + Str(#OUTPUT_CONSOLE))
			
			; Download a file with PowerShell:
			
			oActiveXPosh\Invoke("Execute('$Client = new-object System.Net.WebClient')")
			
			DeleteFile(GetTemporaryDirectory() + "pic.png")
			
			Define Command.s = "$Client.DownloadFile('http://abload.de/img/novice-and-experienceixcba.png','" + GetTemporaryDirectory() + "pic.png')"
			
			Debug "Downloading ..."
			
			; important: replace single quotes with $0027
			
			Command = ReplaceString(Command, "'", "$0027")
			
			oActiveXPosh\Invoke("Execute('" + Command + "')")
			
			If FileSize(GetTemporaryDirectory() + "pic.png") = -1
				Debug "Download failed"
			Else
				Debug "Download succeeded"
				RunProgram(GetTemporaryDirectory() + "pic.png")
			EndIf
			
		Else
			Debug "PowerShell not installed"
		EndIf
		
	Else
		Debug "Init Failed"
		Debug COMate_GetLastErrorCode() + " - " +COMate_GetLastErrorDescription() 
	EndIf
	
	oActiveXPosh\Release()
	
Else
	Debug "!COMate_CreateObject('SAPIEN.ActiveXPoSHV3')"
EndIf
Greetings ... Peter
Hygge
User avatar
jackymb
User
User
Posts: 16
Joined: Wed Aug 11, 2004 7:37 pm
Location: AIX en PROVENCE (France)

Re: ComatePlus & Powershell (ActiveXPosh)

Post by jackymb »

@Kiffi

thank you very much. Also works for me

With that I will continue with another command and script.

Very nice the humorous images.

Greetings - Jacky
Windows 10 x64/86 - PB 5.73LTS (x86 & x64)
_________________________________________
~English is not my native language I'm using a translator~
Sundance
User
User
Posts: 16
Joined: Sat Jun 07, 2014 10:12 am

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Sundance »

Hi.

I just tried to call some basic commands within the powershell and don't get any output of them.
When i try to call a simple dir command. How can i get the output of it?
I thought a 'Debug oActiveXPosh\GetStringProperty("Output")' would do but nothing appears.
Also when i do a '$result = dir' command and then 'Debug oActiveXPosh\GetStringProperty("$result")' i won't get any output.
And using ActiveXPosh/OutputString to get the complete output also fails.

Can someone give a little hint?


Thanks in advance
Sundance
Sundance
User
User
Posts: 16
Joined: Sat Jun 07, 2014 10:12 am

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Sundance »

Ok.
I've found that with 'Debug oActiveXPosh\GetStringProperty("OutputString")' I can get the output of a PS command.
Sundance
User
User
Posts: 16
Joined: Sat Jun 07, 2014 10:12 am

Re: ComatePlus & Powershell (ActiveXPosh)

Post by Sundance »

I still can't figure out how to read the value of a variable.
When doing a $result = dir
then I don't know how to read this value and get it into PureBasic.

Will try but if someone has a little hint for me... :-)
Post Reply