Powershell placing line breaks in inappropriate output locations

I have a PowerShell script, but part of the output is cut out at an arbitrary point before continuing on to the next line. This is incredibly annoying.

For example, I can use Write-Host, and the line will continue as long as I want to (note that prefix information has been added to Team City and Team City, however, you can observe the same effect, output to a file):

[10:04:45] [Step 5/7]  - Found Windows Service at G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsService.DummyService.exe

In other cases, the output will seem artificially truncated at an arbitrary point, for example, it wraps inside a window (which is not).

So this line:

Copy-Item -Path $fullSourcePath -Destination $destPath -Recurse -Verbose -ErrorAction Stop

Produces this output in Team City (which adds some information about prefixes):

[10:04:46] [Step 5/7] VERBOSE: Performing the operation "Copy File" on target "Item: 
[10:04:46] [Step 5/7] G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsServi
[10:04:46] [Step 5/7] ce.DummyService.exe Destination: 
[10:04:46] [Step 5/7] \\SERVER001\scheduled-tasks\ProductFolder\Dev\DummyWindowsService\WindowsService.DummyService.exe".
[10:04:46] [Step 5/7] VERBOSE: Performing the operation "Copy File" on target "Item: 
[10:04:46] [Step 5/7] G:\TeamCityData\TeamCityBuildAgent-1\work\282b8abc9094651e\Artefacts\windows-services\WindowsService.Dummy\WindowsServi
[10:04:46] [Step 5/7] ce.DummyService.exe.config Destination: 
[10:04:46] [Step 5/7] \\SERVER001\scheduled-tasks\ProductFolder\Dev\DummyWindowsService\WindowsService.DummyService.exe.config".

? , , .

UPDATE

, , TeamCity . , PowerShell .

:

copy-item -Path F:\logs -Destination .\ -Recurse -Verbose *> F:\logs\copy-item-output-1.txt

:

Performing the operation "Copy File" on target "Item: F:\logs\20161103-140649-ProductName.Program.log 
Destination: F:\Destination\1234567890abcdefghijklmnopqrstuvwxyz\this-is-a-long-path-name-to-show-wrapping-issues-with-copy-it
em\logs\20161103-140649-ProductName.Program.log".

, , , .

+4
2

( 10 , ?) PowerShell, PowerShell ISE:

$pshost = get-host
$pswindow = $pshost.ui.rawui
$newsize = $pswindow.buffersize
### do not change $newsize.height
$newsize.width = 3000               ### [int] type; max. value unknown at present
$pswindow.buffersize = $newsize
+2

, :

& {
    #Script code here
    Copy-Item -Path $fullSourcePath -Destination $destPath -Recurse -Verbose -ErrorAction Stop
} *>&1 | % {
    function IsStreamType {
        param($Object, $Type)
        $Property = $_.PSObject.Properties[$Type]
        $null -ne $Property -and $Property.Value -is [bool] -and $Property.Value
    }
} {
    switch(,$_) {
        {
            $_ -is [System.Management.Automation.DebugRecord] -and
            (IsStreamType $_ WriteDebugStream)
        } {
            "Debug message: $($_.Message)"
        }
        {
            $_ -is [System.Management.Automation.VerboseRecord] -and
            (IsStreamType $_ WriteVerboseStream)
        } {
            "Verbose message: $($_.Message)"
        }
        {
            $_ -is [System.Management.Automation.WarningRecord] -and
            (IsStreamType $_ WriteWarningStream)
        } {
            "Warning message: $($_.Message)"
        }
        default { ,$_ }
    }
}

,$_ plain $_ , PowerShell , $_ .

+1

Source: https://habr.com/ru/post/1665986/


All Articles