question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Run Powershell script results in `The command line is too long.`

See original GitHub issue

I’m trying to run a .ps1 script on a remote machine and it just throws an error saying “The command line is too long.” My python code is:

import winrm
myScriptSource = open('script.ps1', 'r').read()
session = winrm.Session('myHost', auth=('myUser','myPass'))
result = session.run_ps(myScriptSource)
print(result)

The script.ps1 has 70 lines and runs fine on Powershell.

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:2
  • Comments:9

github_iconTop GitHub Comments

4reactions
killjoy1221commented, Nov 4, 2020

This is caused by cmd having a length limit of ~8K. You can get around this by using low-level api and sneaking the script into an environment variable, which afaik has a much higher limit.

Update 11/4/2020

This should be considered a temporary solution. Windows has a shared pool of 2^15 characters for all environment variables, so this won’t work for all scripts. A better solution is mentioned here

Original solution

import winrm
from base64 import b64encode

script = """
# Really long script >8kb goes here
"""
cmd = """
# Load script from env-vars
. ([ScriptBlock]::Create($Env:WINRM_SCRIPT))
"""
encoded_cmd = b64encode(cmd.encode('utf_16_le')).decode('ascii')

p = winrm.protocol.Protocol(url, username="user", password="secret")

# Load script to env vars.
shell = p.open_shell(env_vars=dict(WINRM_SCRIPT=script)
command = p.run_command(shell, "powershell -EncodedCommand {}".format(encoded_cmd))

rs = winrm.Response(p.get_command_output(shell, command)

p.cleanup_command(shell, command)
p.close_shell(shell)

print(str(rs.std_out, 'ascii'))
0reactions
jborean93commented, Aug 26, 2021
Read more comments on GitHub >

github_iconTop Results From Across the Web

PowerShell Error The command line is too long
I am using this in ServiceNow Run PowerShell script. That value of the parameter is passed by ServiceNow using a comma seperated value....
Read more >
Run Powershell script results in `The command line is too ...
Run Powershell script results in `The command line is too long.`
Read more >
How to Handle Long PowerShell Statements
When PowerShell expects a line to continue to a second line, it uses a multiline prompt for that line. You then type the...
Read more >
Run PowerShell Script From the Command Line and More
Learn how to get started with PowerShell and how to run PowerShell script in this handy tutorial covering only what you need to...
Read more >
Run scripts in a Windows VM in Azure using action ...
Output is limited to the last 4,096 bytes. · The minimum time to run a script is about 20 seconds. · Scripts run...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found