Page 1 sur 1

Script PowerShell : la ligne de commande est trop longue

Posté : 30 mai 2023 - 16:48
par jlatieule
Bonjour,



Je tourne en rond concernant l'exécution d'un script powershell. J'arrive à l'exécuter via cmd ou la console powershell mais je n'arrive pas à exécuter le script via wapt.
Le retour est une très grosse chaine de caractère alors que je ne devrait pas y avoir grand chose.
J'ai vu un post qui parle de blob mais je ne vois pas comment l'utiliser.

Pouvez-vous m'aider ?

Voici quelques extrait

Code : Tout sélectionner

AAIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAgAD0AIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAuAFMAcABsAGkAdAAoACIAJwAiACkAWwAxAF0ACgAgACAAVwByAGkAdABlAC0ATwB1AHQAcAB1AHQAIAAiAEMAbwBuAGYAaQBnACAAZgBpAGwAZQAgAGYAbwB1AG4AZAAgAGkAbgAgAHcAaQBuAGQAbwB3AHMAIABzAGUAcgB2AGkAYwBlACAAZgBvAGwAZABlAHIAIgAKACAAIABPAHUAdABwAHUAdABJAEQAYQBuAGQAUABXACAAJAByAHUAcwB0AGQAZQBzAGsAXwBpAGQAIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAKAH0ACgAKAFMAdABvAHAALQBQAHIAbwBjAGUAcwBzACAALQBOAGEAbQBlACAAUgB1AHMAdABEAGUAcwBrACAALQBGAG8AcgBjAGUAIAA+ACAAbgB1AGwAbAAKAFMAdABhAHIAdAAtAFMAZQByAHYAaQBjAGUAIAAtAE4AYQBtAGUAIABSAHUAcwB0AEQAZQBzAGsAIAA+ACAAbgB1AGwAbAAKACAAIAB8ACAAQwBvAG4AdgBlAHIAdABUAG8ALQBKAHMAbwBuACAA" ' returned non-zero exit status 1.
Output:La ligne de commande est trop longue.
:
Traceback (most recent call last):
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 1088, in run_powershell
    result = run(cmd,
  File "C:\Program Files (x86)\wapt\waptutils.py", line 2110, in run
    raise CalledProcessErrorOutput(proc.returncode, cmd, ''.join(output+return_stderr))
waptutils.CalledProcessErrorOutput: Command 'powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -OutputFormat text -EncodedCommand "JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQAgAD0AIAAiAFMAaQBsAGUAbgB0AGwAeQBDAG8AbgB0AGkAbgB1AGUAIgAKAAoAPAAjACAATABpAHMAdABlACAAZABlAHMAIABjAG8AbQBtAGEAbgBkAGUAcwAgAHMAaQBsAGUAbgBjAGkAZQB1AHMAZQAgADoAIABoAHQAdABwAHMAOgAvAC8AdwB3AHcALgByAGUAZABkAGkAdAAuAGMAbwBtAC8AcgAvAHIAdQBzAHQAZABlAHMAawAvAGMAbwBtAG0AZQBuAHQAcwAvAHoAcgAyAHcAdABxAC8AYQBsAGwAXwBpAG4AcwB0AGEAbABsAF8AcABhAHIAYQBtAGUAdABlAHIAcwBfAGkAXwBrAG4AbwB3AF8AYQBiAG8AdQB0AF8AcwBpAGwAZQBuAHQAaQBuAHMAdABhAGwAbAAvAAoACQAtAC0AdQBuAGkAbgBzAHQAYQBsAGwACgAJAC0ALQBhAGYAdABlAHI

Code : Tout sélectionner

MAcwB3AG8AcgBkACIAKQAgAH0AKQAKACAAIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAgAD0AIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAuAFMAcABsAGkAdAAoACIAJwAiACkAWwAxAF0ACgAgACAAVwByAGkAdABlAC0ATwB1AHQAcAB1AHQAIAAiAEMAbwBuAGYAaQBnACAAZgBpAGwAZQAgAGYAbwB1AG4AZAAgAGkAbgAgAHcAaQBuAGQAbwB3AHMAIABzAGUAcgB2AGkAYwBlACAAZgBvAGwAZABlAHIAIgAKACAAIABPAHUAdABwAHUAdABJAEQAYQBuAGQAUABXACAAJAByAHUAcwB0AGQAZQBzAGsAXwBpAGQAIAAkAHIAdQBzAHQAZABlAHMAawBfAHAAdwAKAH0ACgAKAFMAdABvAHAALQBQAHIAbwBjAGUAcwBzACAALQBOAGEAbQBlACAAUgB1AHMAdABEAGUAcwBrACAALQBGAG8AcgBjAGUAIAA+ACAAbgB1AGwAbAAKAFMAdABhAHIAdAAtAFMAZQByAHYAaQBjAGUAIAAtAE4AYQBtAGUAIABSAHUAcwB0AEQAZQBzAGsAIAA+ACAAbgB1AGwAbAAKACAAIAB8ACAAQwBvAG4AdgBlAHIAdABUAG8ALQBKAHMAbwBuACAA" ' returned non-zero exit status 1.
Output:La ligne de commande est trop longue.


During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Program Files (x86)\wapt\common.py", line 4074, in install_wapt
    exitstatus = setup.install()
  File "C:\waptdev\ophlm-rustdesk_1.1.9_Windows_DEV\setup.py", line 47, in install
    run_powershell_from_file('WindowsAgentAIOInstall.ps1')
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 1120, in run_powershell_from_file
    return run_powershell(content, output_format, **kwargs)
  File "C:\Program Files (x86)\wapt\setuphelpers_windows.py", line 1092, in run_powershell
    raise CalledProcessErrorOutput(e.returncode, cmd, e.output)
waptutils.CalledProcessErrorOutput: Command 'powershell -NoLogo -NoProfile -NonInteractive -ExecutionPolicy Unrestricted -OutputFormat text -EncodedCommand "JABQAHIAbwBnAHIAZQBzAHMAUAByAGUAZgBlAHIAZQBuAGMAZQAgAD0AIAAiAFMAaQBsAGUAbgB0AGwAeQBDAG8AbgB0AGkAbgB1AGUAIgAKAAoAPAAjACAATABpAHMAdABlACAAZABlAHMAIABjAG8AbQBtAGEAbgBkAGUAcwAgAHMAaQBsAGUAbgBjAGkAZQB1AHMAZQAgADoAIABoAHQAdABwAHMAOgAvAC8AdwB3AHcALgByAGUAZABkAGkAdAAuAGMAbwBtAC8AcgAvAHIAdQBzAHQAZABlAHMAawAvAGMAbwBtAG0AZQBuAHQAcwAvAHoAcgAyAHcAdABxAC8AYQBsAGwAXwBpAG4AcwB0AGEAbABsAF8AcABhAHIAYQBtAGUAdABlAHIAcwBfAGkAXwBrAG4AbwB3AF8AYQBiAG8AdQB0AF8AcwBpAGwAZQBuAHQAaQBuAHMAdABhAG
Le script permet d'installation le client de télémaintenance "RustDesk" où l'on héberge un serveur chez nous. Le script a été anonymisé

Partie WAPT

Code : Tout sélectionner

def install():
    run_powershell_from_file('WindowsAgentAIOInstall.ps1')
Script powershell complet et anonymisé

Code : Tout sélectionner

$ErrorActionPreference= 'silentlycontinue'
#Run as administrator and stays in the current directory
if (-Not ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    if ([int](Get-CimInstance -Class Win32_OperatingSystem | Select-Object -ExpandProperty BuildNumber) -ge 6000) {
        Start-Process PowerShell -Verb RunAs -ArgumentList "-NoProfile -ExecutionPolicy Bypass -Command `"cd '$pwd'; & '$PSCommandPath';`"";
        Exit;
    }
}


$rustdesk_url = 'https://github.com/rustdesk/rustdesk/releases/latest'
$request = [System.Net.WebRequest]::Create($rustdesk_url)
$response = $request.GetResponse()
$realTagUrl = $response.ResponseUri.OriginalString
$rustdesk_version = $realTagUrl.split('/')[-1].Trim('v')
Write-Output("Installing Rustdesk version $rustdesk_version")

function OutputIDandPW([String]$rustdesk_id, [String]$rustdesk_pw) {
  Write-Output("######################################################")
  Write-Output("#                                                    #")
  Write-Output("# CONNECTION PARAMETERS:                             #")
  Write-Output("#                                                    #")
  Write-Output("######################################################")
  Write-Output("")
  Write-Output("  RustDesk-ID:       $rustdesk_id")
  Write-Output("  RustDesk-Password: $rustdesk_pw")
  Write-Output("")
}

If (!(Test-Path $env:Temp)) {
  New-Item -ItemType Directory -Force -Path $env:Temp > null
}

If (!(Test-Path "$env:ProgramFiles\Rustdesk\RustDesk.exe")) {

  cd $env:Temp

  If ([Environment]::Is64BitOperatingSystem) {
    $os_arch = "x64"
  } Else {
    $os_arch = "x32"
  }

  Invoke-WebRequest https://github.com/rustdesk/rustdesk/releases/download/$rustdesk_version/rustdesk-$rustdesk_version-windows_$os_arch.zip -Outfile rustdesk.zip

  Expand-Archive rustdesk.zip
  cd rustdesk
  Start-Process "rustdesk-$rustdesk_version-putes.exe" -argumentlist "--silent-install" -wait

  # Set URL Handler
  New-Item -Path "HKLM:\SOFTWARE\Classes\RustDesk" > null
  Set-ItemProperty -Path "HKLM:\SOFTWARE\Classes\RustDesk" -Name "(Default)" -Value "URL:RustDesk Protocol" > null
  New-ItemProperty -Path "HKLM:\SOFTWARE\Classes\RustDesk" -Name "URL Protocol" -Type STRING > null

  New-Item -Path "HKLM:\SOFTWARE\Classes\RustDesk\DefaultIcon" > null
  Set-ItemProperty -Path "HKLM:\SOFTWARE\Classes\RustDesk\DefaultIcon" -Name "(Default)" -Value "RustDesk.exe,0" > null

  New-Item -Path "HKLM:\SOFTWARE\Classes\RustDesk\shell" > null
  New-Item -Path "HKLM:\SOFTWARE\Classes\RustDesk\shell\open" > null
  New-Item -Path "HKLM:\SOFTWARE\Classes\RustDesk\shell\open\command" > null
  $rustdesklauncher = '"' + $env:ProgramFiles + '\RustDesk\RustDeskURLLauncher.exe" %1"'
  Set-ItemProperty -Path "HKLM:\SOFTWARE\Classes\RustDesk\shell\open\command" -Name "(Default)" -Value $rustdesklauncher > null

  Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force > null
  Install-Module ps2exe -Force > null

$urlhandler_ps1 = @"
  `$url_handler = `$args[0]
  `$rustdesk_id = `$url_handler -creplace '(?s)^.*\:',''
  Start-Process -FilePath '$env:ProgramFiles\RustDesk\rustdesk.exe' -ArgumentList "--connect `$rustdesk_id"
"@

  New-Item "$env:ProgramFiles\RustDesk\urlhandler.ps1" > null
  Set-Content "$env:ProgramFiles\RustDesk\urlhandler.ps1" $urlhandler_ps1 > null
  Invoke-Ps2Exe "$env:ProgramFiles\RustDesk\urlhandler.ps1" "$env:ProgramFiles\RustDesk\RustDeskURLLauncher.exe" > null

  # Cleanup Tempfiles
  Remove-Item "$env:ProgramFiles\RustDesk\urlhandler.ps1" > null
  cd $env:Temp
  Remove-Item $env:Temp\rustdesk -Recurse > null
  Remove-Item $env:Temp\rustdesk.zip > null
}

# Write config
$RustDesk2_toml = @"
rendezvous_server = 'rustdesk.mon_entreprise.com'
nat_type = 1
serial = 0

[options]
custom-rendezvous-server = 'rustdesk.mon_entreprise.com'
key =  '****='
relay-server = 'rustdesk.mon_entreprise.com'
api-server = 'https://rustdesk.mon_entreprise.com'
enable-audio = 'N'
"@

# Permettre le partage de session RDP
Set-ItemProperty -path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\RustDesk' -Name 'share_rdp' -value 'true'

If (!(Test-Path $env:AppData\RustDesk\config\RustDesk2.toml)) {
  New-Item $env:AppData\RustDesk\config\RustDesk2.toml > null
}
Set-Content $env:AppData\RustDesk\config\RustDesk2.toml $RustDesk2_toml > null

If (!(Test-Path $env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk2.toml)) {
  New-Item $env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk2.toml > null
}
Set-Content $env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk2.toml $RustDesk2_toml > null

$random_pass = (-join ((65..90) + (97..122) | Get-Random -Count 8 | % {[char]$_}))
Start-Process "$env:ProgramFiles\RustDesk\RustDesk.exe"  -argumentlist "--password $random_pass" -wait

# Get RustDesk ID
If (!("$env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk.toml")) {
  $rustdesk_id = (Get-Content $env:AppData\RustDesk\config\RustDesk.toml | Where-Object { $_.Contains("id") })
  $rustdesk_id = $rustdesk_id.Split("'")[1]
  $rustdesk_pw = (Get-Content $env:AppData\RustDesk\config\RustDesk.toml | Where-Object { $_.Contains("password") })
  $rustdesk_pw = $rustdesk_pw.Split("'")[1]
  Write-Output("Config file found in user folder")
  OutputIDandPW $rustdesk_id $rustdesk_pw
} Else {
  $rustdesk_id = (Get-Content $env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk.toml | Where-Object { $_.Contains("id") })
  $rustdesk_id = $rustdesk_id.Split("'")[1]
  $rustdesk_pw = (Get-Content $env:WinDir\ServiceProfiles\LocalService\AppData\Roaming\RustDesk\config\RustDesk.toml | Where-Object { $_.Contains("password") })
  $rustdesk_pw = $rustdesk_pw.Split("'")[1]
  Write-Output "Config file found in windows service folder"
  OutputIDandPW $rustdesk_id $rustdesk_pw
}

Stop-Process -Name RustDesk -Force > null
Start-Service -Name RustDesk > null

Re: Script PowerShell : la ligne de commande est trop longue

Posté : 06 juin 2023 - 09:56
par dcardon
Bonjour Joël,

Quand on lance les commandes powershell elles sont encodé en base64 pour éviter les problèmes de caractères spéciaux (il y a plein de cas où ça buge). Le soucis c'est que l'on ne devrait pas faire cela quand on charge un fichier. Dans votre cas le fichier est un peu long, et quand il est converti en base64 et passé en ligne de commande, on dépasse la longueur maximale de la ligne de commande sous Windows...

Ceci dit, il y a plein de ligne dans le fichier qui pourrait être facilement et avantageusement remplacé par des appels setuphelpers... Donc en attendant vous pouvez le couper en deux ou en remplacer une partie par du python.

J'ouvre un ticket en interne pour ce problème.

Cordialement,

Denis

Re: Script PowerShell : la ligne de commande est trop longue

Posté : 06 juin 2023 - 14:55
par jlatieule
Merci Denis pour le retour

À l'occasion je suis intéressé pour un paquet rustdesk généré par Trankil It

Ça remplace très très avantageusement teamviewer et anydesk. L'utilisation des serveurs publique est possible tout comme l'usage par un serveur rustdesk maison.
J'ai trouvé que le résultat était bien supérieur à teamviewer pour plus de simplicité, un poids plus petit et des performances moins gourmande.

J'ai mis tout mes arguments dans la balance et je n'ai rien exagéré 😁
A quand un paquet maison ? 😇😇😇