Powershell output capturing and text wrapping… again…

A while a go I wrote a post “Powershell output capturing and text wrapping: strange quirks... solved!” on preventing output wrapping in PowerShell when capturing the output. In this article I wrote that I used the following way to capture the output with less probability of wrapping:

PowerShell -Command  "`$host.UI.RawUI.BufferSize = new-object System.Management.Automation.Host.Size(512,50); `"c:\temp\testoutputandcapture.ps1`" -argument `"A value`"" >c:\temp\out.txt 2>&1

In the above situation I start a PowerShell script, but before doing that I set the buffer size.

I had some issues with this lately that my values in setting the buffer size where wither to small or too large. The more defensive approach described in the StackOverflow question http://stackoverflow.com/questions/978777/powershell-output-column-width works better for me.

I use it as follows at the top of my PowerShell script file:

Prevent text wrapping
  1. set-psdebug -strict -trace 0 #variables must have value before usage
  2. $global:ErrorActionPreference = "Continue" # Stop on errors
  3. $global:VerbosePreference = "Continue" # set to SilentlyContinue to get no verbose output
  4.  
  5. # Reset the $LASTEXITCODE, so we assume no error occurs
  6. $LASTEXITCODE = 0
  7.  
  8. # Update output buffer size to prevent output wrapping
  9. if( $Host -and $Host.UI -and $Host.UI.RawUI ) {
  10. $rawUI = $Host.UI.RawUI
  11. $oldSize = $rawUI.BufferSize
  12. $typeName = $oldSize.GetType( ).FullName
  13. $newSize = New-Object $typeName (512, $oldSize.Height)
  14. $rawUI.BufferSize = $newSize
  15. }
Published Sunday, February 14, 2010 2:10 AM by svdoever
Filed under: ,

Comments

Thursday, June 10, 2010 9:14 AM by Ismail Ahmed Syed

# re: Powershell output capturing and text wrapping… again…

I am appending a sample script  of what we are trying to do.

1.we are using powershell 2.0 new remoting  feature  with fan-out model.

2.The issue we are facing is how to check if the remote command (invoke-command) has successfully executed the function on the remote computers. we want to check each of the computer independently and then log the error(including the machine name it failed on and error message) ,stop the script etc....

3.The function we have written for this purpose(checkforerrors) in the appended file is randomly working sometimes and not sometimes.

I would like to know if their is anyway we can achieve this.(it can be even without using that function). If you can get me a suggestion on this, that would be great.

function CreateAppPool {

param (

[string]$domain,

   [string]$poolName

)

Add-PSSnapIn webAdministration;

if (-not (test-path IIS:\AppPools\$poolName)) {

New-WebAppPool -name $poolName

Set-ItemProperty IIS:\AppPools\$poolName -name managedPipelineMode  -value 0 ### 0=Integrated, 1=Classic

Set-ItemProperty IIS:\AppPools\$poolName -name processModel -value @{userName="$domain\xxxxx";password="yyyyyy";identitytype=3}

}

}

function checkforErrors {

param (

[string]$allsession,  

[string]$errorMsg

)

foreach ($session in $allsession)

{

if($errorMsg -ne $null)

{

$Machineinfo = Get-PSSession ($machine)

#$errorlist = $Machineinfo += $errorMsg

tee-object -file "e:\temp\debug.ps1" -InputObject $errorlist

Remove-PSSession *

write-host("exited out because of error")

exit 1

}

else{

Write-Host("Success")

   }

}

                      }

################Start place for execution of script##################

##############

###  Main#####

##############

#These will be the machines on which "CreateAppPool" function will be executed remotely

$allmachines = "computer1,computer2"

#----Here we are adding the sessions created for each machine in allsession variable

foreach ($machine in $allmachines) {

# creating the persistent sessions for all the machines

       #$allsession = New-PSSession -ComputerName $target -Name $target

$allsession = New-PSSession -Name $target -ComputerName $target

}

#####IMPORTANT#######

#Powershell remoting using fan-out model

Invoke-Command -Session $allsession -ScriptBlock $function:CreateAppPool -ArgumentList $domain,$poolName -ErrorVariable errorMsg

##This is the function we are plannig to use if the execution of the above function errored out while execution on remote machines

checkforErrors $allsession $errorMsg

---ismailahmed.syed@gmail.com

Sunday, April 10, 2011 1:28 AM by solar water heater

# re: Powershell output capturing and text wrapping… again…

Yeah you could walk the streets in NYC any day and see his "fans" out and about.

Leave a Comment

(required) 
(required) 
(optional)
(required)