Page 1 of 3
[SOLVED] Problem executing a PowerShell script using the PowerShell run function
Published: April 26, 2022 - 4:36 PM
by Albin
Good morning,
We created a PowerShell script that allows you to modify the print queue of a printer.
here is the script in question
Code: Select all
run(r'powershell.exe -executionpolicy bypass ./test.ps1')
test.ps1: contains the PowerShell script
Here is the associated PowerShell script
Code: Select all
$OldServerName = "xxxxxxxxxxxxx"
$NewServerName = "yyyyyyyyyyyy"
#Get existing network printers
$CurrentPrinters = Get-WmiObject Win32_Printer | Where-Object {$_.Network -eq "true"} | Select-Object -ExpandProperty Name
#Remove existing network printers
Get-WmiObject -Class Win32_Printer | Where-Object {$_.Network -eq 'true'} | ForEach-Object {$_.delete()}
#Map the printers from a new server.
$CurrentPrinters | ForEach-Object {
$newprintername = $_.Replace( $OldServerName, $NewServerName )
Add-Printer -ConnectionName $newprintername
}
The script is not working and I would like to know what the problem might be.
Does the PowerShell run function allow you to execute multiple PowerShell command lines?
How can I see the associated errors?
THANKS
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 11:19 AM
by dcardon
Hello Albin,
you can try the command
Code: Select all
run_powershell_from_file("filename.ps1")
Sincerely,
Denis
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 12:13 PM
by Albin
Hello Denis,
I tested the command but it still doesn't work.
Regards,
Albin
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 2:04 PM
by dcardon
That's a rather limited amount of information...
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 2:28 PM
by Albin
The problem I'm encountering is that when I deploy the package containing the script
Code: Select all
run_powershell_from_file("testwapt.ps1")
On one machine I have no error at the Wapt console level but it does not work.
I would like to know what the cause of the problem might be, please. Thank you
Sincerely,
Albin
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 2:36 PM
by Albin
I also enabled the execution of PowerShell scripts on the affected machine
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 4:24 PM
by Gaetan
Hello,
is the script functional outside of WAPT?
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 4:27 PM
by Albin
Hello Gaetan,
Yes, apart from WAPT the script works perfectly.
Regards,
Albin
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 4:40 PM
by Gaetan
Within the package, is it possible to execute each PowerShell command using `run_powershell` instead of a script?
This is to try and locate the problem.
Re: Problem executing a PowerShell script using the PowerShell run function
Published: April 27, 2022 - 5:11 PM
by Albin
We just executed each command of the script with run_powershell
Here is the code:
Code: Select all
run_powershell('$OldServerName = "xxxxxxxx"')
run_powershell('$NewServerName = "yyyyyyyy"')
run_powershell('$CurrentPrinters = Get-WmiObject Win32_Printer | Where-Object {$_.Network -eq "true"} | Select-Object -ExpandProperty Name')
run_powershell('-WmiObject -Class Win32_Printer | Where-Object {$_.Network -eq "true"} | ForEach-Object {$_.delete()}')
run_powershell('$CurrentPrinters | ForEach-Object { $newprintername = $_.Replace( $OldServerName, $NewServerName ) Add-Printer -ConnectionName $newprintername}')
When I deploy the package to a machine, we get errors in the Wapt console
Here are the errors:
Code: Select all
Installing:
Traceback (most recent call last):
File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 1080, in run_powershell
result = run(cmd,
File "C:\Program Files (x86)\wapt\waptutils.py", line 2091, in run
raise CalledProcessErrorOutput(proc.returncode, cmd, ''.join(output+return_stderr))
waptutils.CalledProcessErrorOutput: Command 'powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -OutputFormat text -EncodedCommand "JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQAgAD0AIAAiAFMAaQBsAGUAbgB0AGwAeQBDAG8AbgB0AGkAbgB1AGUAIgAKAC0AVwBtAGkATwBiAGoAZQBjAHQAIAAtAEMAbABhAHMAcwAgAFcAaQBuADMAMgBfAFAAcgBpAG4AdABlAHIAIAB8ACAAVwBoAGUAcgBlAC0ATwBiAGoAZQBjAHQAIAB7ACQAXwAuAE4AZQB0AHcAbwByAGsAIAAtAGUAcQAgACIAdAByAHUAZQAiAH0AIAB8ACAARgBvAHIARQBhAGMAaAAtAE8AYgBqAGUAYwB0ACAAewAkAF8ALgBkAGUAbABlAHQAZQAoACkAfQAgACAAfAAgAEMAbwBuAHYAZQByAHQAVABvAC0ASgBzAG8AbgAgAA==" ' returned non-zero exit status 1.
Output:#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">-WmiObject : Le terme «-WmiObject» n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou _x000D__x000A_</S><S S="Error">programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès _x000D__x000A_</S><S S="Error">est correct et réessayez._x000D__x000A_</S><S S="Error">Au caractère Ligne:2 : 1_x000D__x000A_</S><S S="Error">+ -WmiObject -Class Win32_Printer | Where-Object {$_.Network -eq "true" ..._x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~_x000D__x000A_</S><S S="Error"> + CategoryInfo : ObjectNotFound: (-WmiObject:String) [], CommandNotFoundException_x000D__x000A_</S><S S="Error"> + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Program Files (x86)\wapt\common.py", line 3938, in install_wapt
exitstatus = setup.install()
File "C:\windows\TEMP\waptq24g89cv\setup.py", line 15, in install
File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 1084, in run_powershell
raise CalledProcessErrorOutput(e.returncode, cmd, e.output)
waptutils.CalledProcessErrorOutput: Command 'powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -OutputFormat text -EncodedCommand "JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQAgAD0AIAAiAFMAaQBsAGUAbgB0AGwAeQBDAG8AbgB0AGkAbgB1AGUAIgAKAC0AVwBtAGkATwBiAGoAZQBjAHQAIAAtAEMAbABhAHMAcwAgAFcAaQBuADMAMgBfAFAAcgBpAG4AdABlAHIAIAB8ACAAVwBoAGUAcgBlAC0ATwBiAGoAZQBjAHQAIAB7ACQAXwAuAE4AZQB0AHcAbwByAGsAIAAtAGUAcQAgACIAdAByAHUAZQAiAH0AIAB8ACAARgBvAHIARQBhAGMAaAAtAE8AYgBqAGUAYwB0ACAAewAkAF8ALgBkAGUAbABlAHQAZQAoACkAfQAgACAAfAAgAEMAbwBuAHYAZQByAHQAVABvAC0ASgBzAG8AbgAgAA==" ' returned non-zero exit status 1.
Output:#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">-WmiObject : Le terme «-WmiObject» n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou _x000D__x000A_</S><S S="Error">programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès _x000D__x000A_</S><S S="Error">est correct et réessayez._x000D__x000A_</S><S S="Error">Au caractère Ligne:2 : 1_x000D__x000A_</S><S S="Error">+ -WmiObject -Class Win32_Printer | Where-Object {$_.Network -eq "true" ..._x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~_x000D__x000A_</S><S S="Error"> + CategoryInfo : ObjectNotFound: (-WmiObject:String) [], CommandNotFoundException_x000D__x000A_</S><S S="Error"> + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
CalledProcessErrorOutput: Command 'powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -OutputFormat text -EncodedCommand "JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQAgAD0AIAAiAFMAaQBsAGUAbgB0AGwAeQBDAG8AbgB0AGkAbgB1AGUAIgAKAC0AVwBtAGkATwBiAGoAZQBjAHQAIAAtAEMAbABhAHMAcwAgAFcAaQBuADMAMgBfAFAAcgBpAG4AdABlAHIAIAB8ACAAVwBoAGUAcgBlAC0ATwBiAGoAZQBjAHQAIAB7ACQAXwAuAE4AZQB0AHcAbwByAGsAIAAtAGUAcQAgACIAdAByAHUAZQAiAH0AIAB8ACAARgBvAHIARQBhAGMAaAAtAE8AYgBqAGUAYwB0ACAAewAkAF8ALgBkAGUAbABlAHQAZQAoACkAfQAgACAAfAAgAEMAbwBuAHYAZQByAHQAVABvAC0ASgBzAG8AbgAgAA==" ' returned non-zero exit status 1.
Output:#< CLIXML
<Objs Version="1.1.0.1" xmlns="http://schemas.microsoft.com/powershell/2004/04"><S S="Error">-WmiObject : Le terme «-WmiObject» n'est pas reconnu comme nom d'applet de commande, fonction, fichier de script ou _x000D__x000A_</S><S S="Error">programme exécutable. Vérifiez l'orthographe du nom, ou si un chemin d'accès existe, vérifiez que le chemin d'accès _x000D__x000A_</S><S S="Error">est correct et réessayez._x000D__x000A_</S><S S="Error">Au caractère Ligne:2 : 1_x000D__x000A_</S><S S="Error">+ -WmiObject -Class Win32_Printer | Where-Object {$_.Network -eq "true" ..._x000D__x000A_</S><S S="Error">+ ~~~~~~~~~~_x000D__x000A_</S><S S="Error"> + CategoryInfo : ObjectNotFound: (-WmiObject:String) [], CommandNotFoundException_x000D__x000A_</S><S S="Error"> + FullyQualifiedErrorId : CommandNotFoundException_x000D__x000A_</S><S S="Error"> _x000D__x000A_</S></Objs>
Sincerely,
Albin