[SOLVED] Problem executing a PowerShell script using the PowerShell run function

Questions about WAPT Server / Requests and help related to the WAPT server
Forum Rules
Community Forum Rules
* English support on www.reddit.com/r/wapt
* French community support is available on this forum
* Please prefix the topic title with [RESOLVED] if it is resolved.
* Please do not edit a topic that is tagged [RESOLVED]. Open a new topic referencing the old one.
* Specify the installed WAPT version, full version, and build number (2.2.1.11957 / 2.2.2.12337 / etc.) as well as the Enterprise/Discovery edition.
* Versions 1.8.2 and earlier are no longer supported. The only questions accepted regarding version 1.8.2 are related to upgrading to a supported version (2.1, 2.2, etc.).
* Specify the server OS (Linux/Windows) and version (Debian Buster/Bullseye - CentOS 7 - Windows Server 2012/2016/2019).
* Specify the OS of the administration/package creation machine and the machine with the problematic agent, if applicable (Windows 7/10/11/Debian 11/etc.).
* Avoid asking multiple questions when opening a topic, otherwise it may be ignored. If there are multiple topics, open separate topics, preferably one after the other and not all at the same time (i.e., do not spam the forum).
* Include code snippets, screenshots, and other images directly in the post. Links to Pastebin, Bitly, and other third-party sites will be systematically removed.
* As with any community forum, support is provided voluntarily by members. If you require commercial support, you can contact Tranquil IT's sales department at 02.40.97.57.55
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 26, 2022 - 4:36 PM

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
User avatar
dcardon
WAPT Expert
Messages: 1932
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

April 27, 2022 - 11:19

Hello Albin,

you can try the command

Code: Select all

run_powershell_from_file("filename.ps1")
Sincerely,

Denis
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 27, 2022 - 12:13

Hello Denis,

I tested the command but it still doesn't work.

Regards,

Albin
User avatar
dcardon
WAPT Expert
Messages: 1932
Registration: June 18, 2014 - 09:58
Location: Saint Sébastien sur Loire
Contact :

April 27, 2022 - 2:04 PM

That's a rather limited amount of information...
Denis Cardon - Tranquil IT
Share your experiences on WAPT! Send us your blog and article URLs in the "Your Opinion of the forum, and we'll feature them on the WAPT
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 27, 2022 - 2:28 PM

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
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 27, 2022 - 2:36 PM

I also enabled the execution of PowerShell scripts on the affected machine
User avatar
Gaetan
Messages: 169
Registration: August 8, 2019 - 10:16
Location: Toulouse

April 27, 2022 - 4:24 PM

Hello,

is the script functional outside of WAPT?
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 27, 2022 - 4:27 PM

Hello Gaetan,

Yes, apart from WAPT the script works perfectly.

Regards,
Albin
User avatar
Gaetan
Messages: 169
Registration: August 8, 2019 - 10:16
Location: Toulouse

April 27, 2022 - 4:40 PM

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.
Albin
Messages: 17
Registration: Apr 26, 2022 - 2:55 p.m.

April 27, 2022 - 5:11 PM

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
Last edited by Albin on 29 Apr 2022 - 09:59, edited 1 time.
Locked