Promotic

How to use PowerShell

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 you need 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:
Runs 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" + "\\r\\n";
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 with the help 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'\\r\\n";
sInvoke += "$PRINTERTMP = (Get-CimInstance -ClassName CIM_Printer | WHERE {$_.Name -eq $MYPRINTER}[0])\\r\\n";
sInvoke += "$PRINTERTMP | Invoke-CimMethod -MethodName SetDefaultPrinter | Out-Null\\r\\n";
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");
PROMOTIC 9.0.27 SCADA system documentation MICROSYS, spol. s r.o.

Send page remarkContact responsible person
Navigation:
 
 
- Running PowerShell commands from PROMOTIC application
 
 
- SVG
© MICROSYS, spol. s r.o.