none
how to send 150 line of block in Invoke-command script block on remote machine RRS feed

  • Question

  • Hello,

    I am trying to execute the invoke-command on remote machine but getting the error command line is too long while sending around 150 line of code in invoke-command -scriptblock{script contains  150 lines}

    remote server is mapped with client certificate small piece of code is executing on remote machine using invoke command without any error.

    Please suggest


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Wednesday, November 16, 2016 8:34 AM

Answers

  • Hi Prashant,

    odds are, your tool encodes the script and passes it as parameter to PowerShell.exe on the target computer. Which makes it pass a long piece of text as parameter to a process. In Windows 7 / Server 2008 R2 Microsoft extended the maximum parameter length, however in Vista / 2008 this was never retrofitted.

    This is an OS limitation and there is no fix for it short of

    • Shortening to code you send at a time

      or

    • Use another way to invoke remote commands

      or

    • Redesign how your script queries information.

    Cheers and good luck with your script,
    Fred


    There's no place like 127.0.0.1

    Friday, November 18, 2016 7:49 AM

All replies

  • You need to post the complete error message.


    \_(ツ)_/

    Wednesday, November 16, 2016 8:47 AM
  • I am executing the script on from local only one message iam getting in the error

    The command line is too long.

    apart from above message i am not getting any thing 



    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Wednesday, November 16, 2016 10:57 AM
  • Then the problem is not a PowerShell problem.  Shell commands have a line length limit. 

    Without the exact line causing the problem we cannot help you.


    \_(ツ)_/

    Wednesday, November 16, 2016 11:17 AM
  • any option to extend the length limit

    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Thursday, November 17, 2016 9:38 AM
  • Hi Prashant,

    the commandline parameter limit is defined by the OS and cannot be fixed.

    • What OS are you invoking from
    • What OS are you trying to invoke the commands on?
    • What's your script?

    It may be a big scriptblock, but if you can show us how you try to invoke it and what it is you are trying to invoke, we may be able to help you more specifically.

    Without specifics ... well, let's just say my crystal ball is clouded today and I discarded the tea leaves.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, November 17, 2016 9:48 AM
  • hi I am trying to pull the health parameter from remote machine through invoke command OS win2k8 target win2k8 i have certain limitation i cannot keep the PS file on the target server so entire code i am giving inside the script block but it encounters message (The command line is too long )

    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Thursday, November 17, 2016 9:58 AM
  • Hi Prashant,

    yeah, you have more stringent limitations on plain W2k8.

    Alright, workaround time:

    You have an open Session? Just split your scriptblock where you can and send it over sequentially. It's a session, it has a session state, things will work out.

    If the issue however is in a specific part of your 150-lines of scriptblock, we can't friggin' tell until you show us.

    Cheers,
    Fred

    PS: I'd recommend to migrate to a more modern OS, but yeah, I know reality. If us IT guys could just go ahead and do it, all the world would be running modern OS ...


    There's no place like 127.0.0.1

    Thursday, November 17, 2016 10:14 AM
  • I don't know exactly what 'health parameters' you're talking about but ....

    You might take a look to the CIM Cmdlets.

    OR ... if you're not willing or able to install PS 3.0 you could use Get-WMIObject to get some information about your servers.


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''


    • Edited by BOfH-666 Thursday, November 17, 2016 10:26 AM
    Thursday, November 17, 2016 10:21 AM
  • cpu,critical services,latest security hot fixes,Performance counter,uptime,etc are health parameter FYI cmdlets already and working fine for local machine but same i want to execute remotely

    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Thursday, November 17, 2016 10:27 AM
  • As you're still not showing any code  ....

    Do you know that Get-WMIObject and Get-CIMInstance have a paramter called -ComputerName


    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 17, 2016 10:32 AM
  • please check the below url entire scripts i have execute on remote machie

    https://gallery.technet.microsoft.com/Windows-Server-Health-4414851b


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Thursday, November 17, 2016 11:16 AM
  • Wow  .... sorry, but that's is just too much and just too badly formatted to review it here completely. But after a short superficially review I could see that almost all cmdlets you use support the parameter -ComputerName. So you could easily make your script running against remote computers as well. If you have a little time - here is a great source of inspiration for you: Don Jones Toolmaking. Don Jones illustrates perfectly how to make code re-usable. I beliebve if you watch all 3 parts to the end you will be a better scripter. ;-)

    Grüße - Best regards

    PS:> (79,108,97,102|%{[char]$_})-join''

    Thursday, November 17, 2016 12:19 PM
  • Hi Prashant,

    have to agree with BOfH about this: All of those data gathering commands support the ComputerName parameter, you do not need a remoting session in order to get all the information.

    Also you need to give it a better structure - it's incredibly hard to read and there are pieces of information that you retrieve but do not use.

    General advice on structure:

    • Parameterization should be done as PowerShell parameters at the top of the script. Editing the script should not be necessary (see Services retrieved for an example of where you do that).
    • Separate data retrieval and output formatting. You should only convert objects into html in the final stage of your script.
    • Give multiple choices for output: Raw Data should also be an option.
    • When building a html file, do it all in one multiline string, adding in the pieces of data. This makes it a lot easier to troubleshoot and change later.
    • ConvertTo-Html has a fragment parameter, allowing you to get pure table html, rather than nesting <html>-Tags.

    Basically, you can skip the remoting. If you insist on remoting, get the data and do the formatting locally.

    Cheers,
    Fred


    There's no place like 127.0.0.1

    Thursday, November 17, 2016 1:23 PM
  • please check the below url entire scripts i have execute on remote machie

    https://gallery.technet.microsoft.com/Windows-Server-Health-4414851b


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Place the code in a local file and use Invoke-Command with the "file" parameter.

    There are no lines that are too long in the file:

    Invoke-Command -File myscript.ps1 -Computer remotepc.


    \_(ツ)_/

    Thursday, November 17, 2016 5:37 PM
  • I am very much thankful for all your inputs 

    I am new in this script is written in a bad format i am totally agree   I ll try to format the code as per your inputs and also in structured way and in future also. but my query is extend the length of scriptsblock

    this option not matching as per the client environment Invoke-Command -File myscript.ps1 -Computer remotepc. 

    above option i have tried but no luck as i have a limitation . I have to do this via orchestration tool to automate the script so i have only one option i.e invoke-command -scriptblock{} this i am performing via jumping server & jumping server is non windows flavor the only option left i have to reduce the line of code and send the script in small piece of chunks then i have to combine the output .

    There is no option to extend the length of scriptsblock . If yes please suggest.


    Thanks for all your support  


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Friday, November 18, 2016 6:44 AM
  • You will have to contact the vendor of teh non-Windows server for assistance.  The issue is not a PowerShell issue.  We cannot help you.


    \_(ツ)_/

    Friday, November 18, 2016 6:55 AM
  • I am able to execute the script of 50 to 60 line which is executing fine and i am getting the output also.

    It not a issue.it a limitation of invoke-command.I hope so.


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Friday, November 18, 2016 7:07 AM
  • There is no limit to the size of a script block.  The issue is with the third party server you are using.  The tool has a command  buffer and that is likely limiting the size of a command that it can execute.

    \_(ツ)_/


    • Edited by jrv Friday, November 18, 2016 7:10 AM
    Friday, November 18, 2016 7:09 AM
  • To be clear.  I loaded you script and ran it as a script block with no issues.  It is much smaller than most scripts I have run.

    To bypass the limit of your tool do what was suggested above and create a session and execute the script in pieces.


    \_(ツ)_/

    Friday, November 18, 2016 7:16 AM
  • In less than five minutes I was able to reduce your code to half its size by just getting rid of all redundant lines and all useless comments.

    You have pasted together a lot of code from various places.  If you actually spend time designing a clean script you would find that it would be much, much smaller.


    \_(ツ)_/


    • Edited by jrv Friday, November 18, 2016 7:32 AM
    Friday, November 18, 2016 7:32 AM
  • there is no issue with the tool same i tried manually from local machine to target in windows environment but no luck. I ll try to find out some workaround 


    Thanks for all your inputs. 


    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    Friday, November 18, 2016 7:35 AM
  • Hi Prashant,

    odds are, your tool encodes the script and passes it as parameter to PowerShell.exe on the target computer. Which makes it pass a long piece of text as parameter to a process. In Windows 7 / Server 2008 R2 Microsoft extended the maximum parameter length, however in Vista / 2008 this was never retrofitted.

    This is an OS limitation and there is no fix for it short of

    • Shortening to code you send at a time

      or

    • Use another way to invoke remote commands

      or

    • Redesign how your script queries information.

    Cheers and good luck with your script,
    Fred


    There's no place like 127.0.0.1

    Friday, November 18, 2016 7:49 AM
  • Thanks for your valuable inputs 

    I am trying to break the script and this is the only option i have . Hope i will get the success.



    Prashant Dev Pandey LIVE IN YOUR OWN WAY pdppandey@hotmail.com 91-7795618301

    • Proposed as answer by Ekta_Junior Friday, December 23, 2016 6:02 AM
    Tuesday, November 22, 2016 6:34 AM