Powershell script

Hello for some reason bat scripts work but ps1 scripts don't. Is there something special that needs to be done for powershell scripts?

I have the below script running as a gateway update event script and the ps1 and bat files are located in the same location. The delete tag increments for bat but not ps1

import time
try:
	time.sleep(5)
#	gitEXE = "C:\\Program Files\\Inductive Automation\\Ignition\\data\\git-auto-commit.bat"
	gitEXE = "C:\\Program Files\\Inductive Automation\\Ignition\\data\\git-auto-commit.ps1"
	logger.warn(str(system.file.fileExists(gitEXE))) 
	system.util.execute([gitEXE])
	val = system.tag.readBlocking("[Ignition_Remote_IO_Gateway]aTestSharedFolder/delete")[0].value
	system.tag.writeBlocking("[Ignition_Remote_IO_Gateway]aTestSharedFolder/delete", val+1)
except Exception as e:
	logger.warn(str(e))```

Do you get an error ?

Nothing shows up in the console log or the logger on the gateway page with the except statement.

Normally Powershell scripts have some tighter default execution policies that you might need to inspect/adjust. See here for some starter information: about Execution Policies - PowerShell | Microsoft Learn

@pascal.fragnoud, @kcollins1 I found an error. It was outside the logger I created.

Error executing project update script.

com.inductiveautomation.ignition.common.script.JythonExecException: Traceback (most recent call last): File "<[Data_Management] Update Script>", line 9, in File "<[Data_Management] Update Script>", line 9, in at java.base/java.lang.ProcessBuilder.start(Unknown Source) at java.base/java.lang.ProcessBuilder.start(Unknown Source) at java.base/java.lang.Runtime.exec(Unknown Source) at java.base/java.lang.Runtime.exec(Unknown Source) at com.inductiveautomation.ignition.common.script.builtin.SystemUtilities.execute(SystemUtilities.java:146) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.io.IOException: java.io.IOException: Cannot run program "C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1": CreateProcess error=193, %1 is not a valid Win32 application

You might have to use powershell.exe to invoke the target .ps1 script. ref: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1

I'm executing

	system.util.execute([
		"powershell.exe",
		"-File 'C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1'"
	])

but it doesn't seem to be doing anything with no errors and I don't see the afile.txt in the data folder.

git-auto-commit.ps1 (simple create file)

New-Item afile.txt

I also tried

	import subprocess
	proc = subprocess.Popen(["powershell.exe -File 'C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1'"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	proc = subprocess.Popen(['powershell.exe', "-File 'C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1'"], stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
	output = proc.stdout.read()
	logger.warn(output)

but both those proc didn't work. First proc gave "[Errno 2] No such file or directory" and second proc gave "-File : The term '-File' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1 + -File 'C:\Program Files\Inductive Automation\Ignition\data\git-auto-c ... + ~~~~~ + CategoryInfo : ObjectNotFound: (-File:String) , CommandNotFou ndException + FullyQualifiedErrorId : CommandNotFoundException"

Thank you guys for the help so far

You're not structuring your commands correctly. Everywhere there's a space in a raw command line:
powershell.exe -File 'C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1'
You need a new entry in the list you pass to system.util.execute or subprocess:

system.util.execute([
		"powershell.exe",
		"-File",
		"'C:\Program Files\Inductive Automation\Ignition\data\git-auto-commit.ps1'"
	])
1 Like

You are almost certainly going to need to double up your backslashes, as those are escape characters in python string constants.

2 Likes

Or use r"something\\" to ignore escape characters, unless this doesn't work in jython :X

1 Like

Thank you everyone for the help!!! This worked

	system.util.execute([
		"powershell.exe",
		"-File",
		"C:\\Program Files\\Inductive Automation\\Ignition\\data\\git-auto-commit.ps1"
	])

I didn't need the single quotes in the path

2 Likes