Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Saturday, May 21, 2011 9:20 AM
Hi,
I am trying to remotely execute a powershell script through ssh, and I found the output of my script is only 80 width, a carriage-return and a line-feed are inserted after 79th column, and continue presenting the 80th value of each row onto another line. I want it greater than 80, such as 512.
The code to print the output in my script is like:
$allInfo | ConvertTo-Xml -as Stream -NoTypeInformation -Depth 1
Any help will be really appreciated!
All replies (10)
Sunday, May 22, 2011 10:33 PM âś…Answered | 1 vote
I believe this article explains how to do exactly what you want. Let us know if it works.
Saturday, May 21, 2011 8:02 PM
I'm not sure how to control the width of the output when it's going to a console though I'm guessing it has something to do with the console width itself. I believe I've read how to directly manipulate the Powershell console options somewhere.
However, if you redirect output to a file with the out-file command, it has a -width option that lets you explicitly specify how wide the file should be. Perhaps that's all you need.
$allInfo | ConvertTo-Xml -as Stream -NoTypeInformation -Depth 1 | out-file -width 512
Sunday, May 22, 2011 1:33 AM
Thanks for you reply!
But I do not want to output to a file. What I am doing is, in a C program, I execute a powershell script remotely through SSH, and then get all its output back.
So I guess maybe I can use "out-string -width 512", then I tried, but no luck.
I am very confused with this behavior because if I put all the output of that powershell script in a .txt file, and in my C program, execute the command "type ***.txt", all the output can be gotten correctly (no additional carriage-return and a line-feed are inserted), so that means the normal Windows command is fine, but what is the problem with Powershell script? Making one line into two line is unacceptable, why does PowerSheel do like that?
Sunday, May 22, 2011 5:17 PM
The window size is 80 by default. What happens if you increase the window size value? Andreas Hultgren
MCTS, MCITP
http://ahultgren.blogspot.com/
Monday, May 23, 2011 2:00 AM
Thanks! That article really help me out, after putting the following line into my powershell script, no addtional carriage-return and a line-feed are inserted anymore.
$Host.UI.RawUI.BufferSize = New-Object Management.Automation.Host.Size (500, 25)
But when I run the powershell script manually, I get the following error message, I do not know why, but anyway, my problem is resolved.
Exception setting "BufferSize": "Cannot set the buffer size because the size specified is too large or too small.
Parameter name: value
Actual value was 512,25."
At C:\Users\qzhang\Documents\GetVM.ps1:30 char:16
- $Host.UI.RawUI. <<<< BufferSize = New-Object Management.Automation.Host.Size (512, 25)
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Monday, May 23, 2011 12:09 PM | 1 vote
But when I run the powershell script manually, I get the following error message, I do not know why, but anyway, my problem is resolved.
Exception setting "BufferSize": "Cannot set the buffer size because the size specified is too large or too small.
Parameter name: value
Actual value was 512,25."
At C:\Users\qzhang\Documents\GetVM.ps1:30 char:16
- $Host.UI.RawUI. <<<< BufferSize = New-Object Management.Automation.Host.Size (512, 25)
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
Yeah, that's a good question. I get the same thing here. The longer solution the post covers which I've included below does work interactively, and looking at the differences it appears to be related to the buffer height. My guess is when the existing console window already has more rows in the buffer than the 25 rows in height the command tries to define, this error occurs.
if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
$rawUI = $Host.UI.RawUI
$oldSize = $rawUI.BufferSize
$typeName = $oldSize.GetType( ).FullName
$newSize = New-Object $typeName (500, $oldSize.Height)
$rawUI.BufferSize = $newSize
}
Tuesday, May 24, 2011 1:06 AM
Thanks! That longer solution does work, the error never happens after I include it in my powershell script.
BTW, you said that it does work "interactively", so what about NonInteractive powershell script? Will there be any problems? My powershell script is NonInteractive, and everything looks fine so far.
Tuesday, May 24, 2011 2:52 PM
Thanks! That longer solution does work, the error never happens after I include it in my powershell script.
BTW, you said that it does work "interactively", so what about NonInteractive powershell script? Will there be any problems? My powershell script is NonInteractive, and everything looks fine so far.
I don't think so, no. I think the only reason the one-liner fails when you run it interactively is that you're doing so with a buffer that already exceeds the 25 row height. My guess is if you cleared the buffer first it would work fine. The longer solution works when you run it interactively simply because it preserves the existing buffers height.
Either solution should work fine for you. The second one is coded to work in more scenarios, such as from an interactive shell that has a larger buffer and has some code that I believe is there to improve it's chances of working within other PowerShell consoles. I'm not sure exactly why it goes to all the trouble it does to determine the type of the new buffersize object, but perhaps the author works with a 3rd party console that they know to use a different type. In any event, it sounds like you're good to go either way.
Thursday, April 23, 2015 5:35 PM
Another possible reason for the error: BufferSize cannot be made smaller than WindowSize.
Thursday, August 17, 2017 8:17 PM
It does not work in PowerShell ISE command window for the command:
tf.exe merge /candidate /preview /recursive "$/net framework/main/source/myprog" "$/net framework/main/tests/myprog"
The output is truncated to 80 characters.