Página 1 de 3
[RESUELTO] Problema al ejecutar un script de PowerShell usando la función de ejecución de PowerShell
Publicado: 26 de abril de 2022 - 16:36
por Albin
Buen día,
Creamos un script de PowerShell que le permite modificar la cola de impresión de una impresora.
Aquí está el guión en cuestión
Código: Seleccionar todo
run(r'powershell.exe -executionpolicy bypass ./test.ps1')
test.ps1: contiene el script de PowerShell
Aquí está el script de PowerShell asociado
Código: Seleccionar todo
$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
}
El script no funciona y me gustaría saber cuál podría ser el problema.
¿La función de ejecución de PowerShell le permite ejecutar múltiples líneas de comando de PowerShell?
¿Cómo puedo ver los errores asociados?
GRACIAS
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 11:19 a. m.
por dcardon
Hola Albin,
puedes probar el comando
Atentamente,
Denis
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 12:13 p. m.
por Albin
Hola Denis,
probé el comando pero sigue sin funcionar.
Saludos,
Albin.
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 14:04
por dcardon
Esa es una cantidad de información bastante limitada...
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 14:28
por Albin
El problema que estoy encontrando es que cuando implemento el paquete que contiene el script
En una maquina no tengo error a nivel de consola Wapt pero no funciona.
Me gustaría saber cuál podría ser la causa del problema, por favor. Gracias
Atentamente,
Albin
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 14:36
por Albin
También habilité la ejecución de scripts de PowerShell en la máquina afectada
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 16:24
por Gaetan
Hola,
¿el script funciona fuera de WAPT?
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 16:27
por Albin
Hola Gaetan,
Sí, aparte de WAPT, el script funciona perfectamente.
Saludos,
Albin
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 16:40
por Gaetan
Dentro del paquete, ¿es posible ejecutar cada comando de PowerShell usando `run_powershell` en lugar de un script?
Esto es para intentar localizar el problema.
Re: Problema al ejecutar un script de PowerShell mediante la función de ejecución de PowerShell
Publicado: 27 de abril de 2022 - 17:11
por Albin
Acabamos de ejecutar cada comando del script con run_powershell
Aquí está el código:
Código: Seleccionar todo
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}')
Cuando implemento el paquete en una máquina, recibimos errores en la consola Wapt
Aquí están los errores:
Código: Seleccionar todo
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>
Atentamente,
Albin