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
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 stopped at the beginning because I have a lot of error.
Thank you to those who are going to help me