Windows PowerShell is more advanced successor of the command line. It can be used for Windows system automation and configuration. It can also access the file system, registers, processes services, network components or selected applications.
Before the first usage it is recommended to verify whether the
PowerShell is activated in the
Windows OS.
If not then follow the steps below:
Control panels>Programs and functions>Activate or deactivate Windows system functions => "Windows PowerShell 2.0".
If the
PowerShell is to be used for executing commands that require higher permissions then it is necessary to run 32 or 64-bit
PowerShell "as administrator" (based on the 32- or 64-bit PROMOTIC runtime type used) and execute the following command:
Set-ExecutionPolicy Unrestricted
and choose and confirm the option "
A".
Example1:
Launches the ping command and then netstat
JavaScriptVBScriptSelect and copy to clipboard
var oShell = Pm.AxGetObject("new", "WScript.Shell");
var sInvoke = "";
sInvoke += "ping 127.0.0.1\n";
sInvoke += "netstat";
oShell.Run("PowerShell " + sInvoke, 1, false);
Dim oShell, sInvoke
Set oShell = Pm.AxGetObject("new", "WScript.Shell")
sInvoke = ""
sInvoke = sInvoke & "ping 127.0.0.1" & vbCrLf
sInvoke = sInvoke & "netstat"
oShell.Run "PowerShell " & sInvoke, 1, false
Sending message from PowerShell into the PROMOTIC application:
This example allows using
PowerShell and the
Invoke-WebRequest command with the
"POST" method to send a message into the
PmaWebDir object. It can then be captured in the
onPageAction event and by means of parameters
pEvent.FilePath,
pEvent.Method,
pEvent.Params,
pEvent.Data to process it further.
The
Invoke-WebRequest method itself is preceded by authentication with the name and password that protects the Web server of the PROMOTIC application.
This script can be saved into the file with the
ps1 extension and then run as a parameter of the
PowerShell program.
$user = 'oper'
$pass = ''
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{Authorization = $basicAuthValue}
Invoke-WebRequest 'http://127.0.0.1/dir/test.htm?test=abc' -Headers $Headers -Body 'ahoj' -Method 'POST'
Sending a request for data from PowerShell into the PROMOTIC application:
This example allows using
PowerShell and
Invoke-WebRequest with the "GET" method to send a request for data to the
PmaWebDir object.
The
Invoke-WebRequest method itself is preceded by authentication with the name and password that protects Web server of the PROMOTIC application.
This script can be saved into the file with the
ps1 extension and then run as a parameter of the
PowerShell program.
$user = 'oper'
$pass = ''
$pair = "$($user):$($pass)"
$encodedCreds = [System.Convert]::ToBase64String([System.Text.Encoding]::ASCII.GetBytes($pair))
$basicAuthValue = "Basic $encodedCreds"
$Headers = @{Authorization = $basicAuthValue}
$res = Invoke-WebRequest 'http://127.0.0.1/dir/test.htm?test=abc' -Headers $Headers -Method 'GET'
$res.Content
# Script for processing received data
It can be captured in the
onPageModify event and the parameters can be used to evaluate and return the requested data to the client. For example:
JavaScriptSelect and copy to clipboard
if (pEvent.FilePath == "test.htm")
{
pEvent.PageString = "Test OK";
}
Windows default printer settings:
JavaScriptVBScriptSelect and copy to clipboard
var oShell = Pm.AxGetObject("new", "WScript.Shell");
var sInvoke = "";
sInvoke += "$MYPRINTER = 'SHARP MX-B350W PCL6'\n";
sInvoke += "$PRINTERTMP = (Get-CimInstance -ClassName CIM_Printer | WHERE {$_.Name -eq $MYPRINTER}[0])\n";
sInvoke += "$PRINTERTMP | Invoke-CimMethod -MethodName SetDefaultPrinter | Out-Null\n";
oShell.Run("PowerShell " + sInvoke, 1, false);
Dim oShell, sInvoke
Set oShell = Pm.AxGetObject("new", "WScript.Shell")
sInvoke = ""
sInvoke = sInvoke & "$MYPRINTER = 'SHARP MX-B350W PCL6'" & vbCrLf
sInvoke = sInvoke & "$PRINTERTMP = (Get-CimInstance -ClassName CIM_Printer | WHERE {$_.Name -eq $MYPRINTER}[0])" & vbCrLf
sInvoke = sInvoke & "$PRINTERTMP | Invoke-CimMethod -MethodName SetDefaultPrinter | Out-Null" & vbCrLf
oShell.Run "PowerShell " & sInvoke, 1, false
Forced immediate termination of all Excel applications on the computer:
JavaScriptVBScriptSelect and copy to clipboard
var oShell = Pm.AxGetObject("new", "WScript.Shell");
oShell.Exec("taskkill /F /IM EXCEL.exe");
Dim oShell
Set oShell = Pm.AxGetObject("new", "WScript.Shell")
oShell.Exec "taskkill /F /IM EXCEL.exe"