none
how to output result to txt file with not truncated data in remote powershell session RRS feed

  • Question

  • Hello Expert,

    My intention is to export results from a group of servers to TXT or CSV file. 

    But when I use the below cmdlet in PSSession, i still got truncated results. I tried to have this command in and out of foreach loop  but no luck. can you help me. 

    #Define the server group by adding computer name here:
    
    $ServerName=Import-Csv "D:\scripts\LocalAdminReport\Serverlist.csv"
    $FormatEnumerationLimit=-1
    
    #Create a new folder named as today's date:
    $usedate = (Get-Date).ToString('yyyy-MM-dd')
    New-Item -Name $usedate -ItemType Directory -path "\"C:\temp\$usedate""
    
    #Invoke-command:
    
    Foreach($one in $ServerName)
    {
    $NewSession= New-PSSession -Computer $one.Name
    invoke-command -Scriptblock {
    $members = net localgroup administrators | where {$_ -AND $_ -notmatch "command completed successfully"} | Select -skip 4
    New-Object PSObject -Property @{
     Computername = $env:COMPUTERNAME
     Group = "Administrators"
     Members=$members}
    } -Session $NewSession -HideComputerName  | ft Computername,Members -Wrap -AutoSize | Out-File "C:\temp\$usedate\$one.txt"
    
    #Close sessions:
    
    Remove-PSSession -Session $NewSession
    }


    Tianchen Qin
    MCSE 2003 & CCNP


    • Edited by Tianchen Qin Monday, July 9, 2018 7:16 AM change the format
    • Moved by Bill_Stewart Monday, July 29, 2019 7:22 PM User answered own question
    Thursday, July 5, 2018 9:31 AM

All replies

  • Please post your code correctly using the code posting tool provided.


    \_(ツ)_/

    Thursday, July 5, 2018 9:37 AM
  • This is how to do this:

    $sb = {
        $members = net localgroup administrators | 
            where {$_ -AND $_ -notmatch "command completed successfully"} | Select -skip 4
        [pscustomobject]@{
            Computername = $env:COMPUTERNAME
            Group        = 'Administrators'
            Members      = $members
        }
    }
    
    Foreach($one in $ServerName){
        invoke-command -Scriptblock $sb -ComputerName $one.Name  -HideComputerName  | 
            Format-Table Computername,Members -Wrap -AutoSize |
            Out-String -Width 120 |
            Out-File "C:\temp\$usedate\$one.txt"
    } 


    \_(ツ)_/

    • Proposed as answer by jrv Monday, July 9, 2018 8:45 AM
    Thursday, July 5, 2018 9:45 AM
  • Hi,

    I changed the powershell format


    Tianchen Qin
    MCSE 2003 & CCNP

    Monday, July 9, 2018 7:17 AM
  • I also tried the solution in this link. However, -Auto and -Width doesn't work in my case

    http://community.idera.com/powershell/ask_the_experts/f/powershell_for_windows-12/5438/how-to-redirect-powershell-output-from-a-script-run-by-taskscheduler-and-override-default-width-of-80-characters/8786#8786


    Tianchen Qin
    MCSE 2003 & CCNP

    Monday, July 9, 2018 8:38 AM
  • The problem is solved by running the script on another machine's Task Scheduler. 

    Tianchen Qin
    MCSE 2003 & CCNP

    Monday, July 9, 2018 12:01 PM