Outputting to TXT problem

This topic contains 11 replies, has 6 voices, and was last updated by  Dr3w 1 year, 10 months ago.

  • Author
    Posts
  • #32246

    Dr3w
    Participant

    Hi All,

    I've been using this script to automate my weekly server disk space maintenance check, and have tried to output the results to a txt file, my attempts have not been sucessful. Would anybody be able to shed some light on my issue?

    Thanks in advance.


    clear
    $file = get-Content 'c:\Computers.txt' # List of Computers

    foreach ( $node in $file) {
    get-WmiObject win32_logicaldisk -ComputerName $node | ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
    }

    # HOW DO I OUTPUT THE ABOVE?

  • #32248

    Codeweavers
    Participant

    Can't you just do:

    foreach ( $node in $file) {
    get-WmiObject win32_logicaldisk -ComputerName $node | ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
    } | Out-File output.txt

  • #32249

    Matt Bloomfield
    Participant
    clear
    $file = get-Content 'c:\Computers.txt' # List of Computers
    
    foreach ( $node in $file) {
     get-WmiObject win32_logicaldisk -ComputerName $node | 
     ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize  | 
    
    Out-File C:\report.txt -append -noclobber } 
    

    Codeweavers is nearly there but you need to be outputting the data to the file at the end of each iteration of the foreach loop.

  • #32252

    Curtis Smith
    Participant

    Codeweaver's method works as well if you use the Foreach-Object cmdlet in the pipeline rather than the Foreach statement.

    get-Content 'c:\Computers.txt' |
    ForEach-Object {
        get-WmiObject win32_logicaldisk -computername $_ | ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
    } | 
    Out-File output.txt
    
  • #32271

    Steven Ayers
    Participant

    I wouldn't be putting it into a text file... its like saving image snapshots of your results hehe!

    Try this:

    clear
    $file = get-Content 'c:\Computers.txt' # List of Computers
    
    foreach ( $node in $file) {
     get-WmiObject win32_logicaldisk -ComputerName $node | 
     select SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }}   | 
    Export-CSV $home\Desktop\Report.csv -append }
    

    Everyone loves speadsheets.

  • #32287

    Dr3w
    Participant

    Thanks for the responses guys.

    I've tried all of the above methods but none have resolved the problem just yet!

  • #32303

    Peter Jurgens
    Participant

    Can you give us some detail as to what you're trying and what happens when you try? Error messages etc?

  • #32367

    Dr3w
    Participant


    $file = get-Content 'c:\Computers.txt' # List of Computers

    foreach ($node in $file) {
    get-WmiObject win32_logicaldisk -ComputerName $node | ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize
    Export-CSV $home\Desktop\Report.csv -append}
    – Steven Ayers

    Error:
    "cmdlet Export-Csv at command pipeline position 1
    Supply values for the following parameters:
    InputObject: " doesn't output

    Matt Bloomfields response
    At \\1sp01\f$\ITFiles\Documents\Powershell\WMC – Disk space check\Disk Space
    Check.ps1:6 char:1
    + | out-file C:\output.txt -append -noclobber}
    + ~
    An empty pipe element is not allowed.
    + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordEx
    ception
    + FullyQualifiedErrorId : EmptyPipeElement

  • #32368

    Dr3w
    Participant

    Curtis Smith:
    Export-CSV : You must specify either the -Path or -LiteralPath parameters, but
    not both.
    At \\1sp01\f$\ITFiles\Documents\Powershell\WMC – Disk space check\test.ps1:6
    char:1
    + Export-CSV -append
    + ~~~~~~~~~~~~~~~~~~
    + CategoryInfo : InvalidData: (:) [Export-Csv], InvalidOperationE
    xception
    + FullyQualifiedErrorId : CannotSpecifyPathAndLiteralPath,Microsoft.PowerS
    hell.Commands.ExportCsvCommand

  • #32369

    Steven Ayers
    Participant

    For Matt's reponse, but the "| out-file C:\output.txt -append -noclobber}" on the same line as the previous text.

    For Curtis, put your file path in speech marks 🙂

  • #32370

    Steven Ayers
    Participant

    Also Andrew.. you changed what I sent you?

    It's not going to work if you try making things up.

  • #32431

    Dr3w
    Participant

    I've managed with Mats response and have made a few ammendents to how the file name outputs.

    clear
    $file = get-Content 'C:\computers.txt' # List of Computers
    $fileName = "C:\" + (Get-Date -Format ddMMyyyy) + ".txt"

    foreach ( $node in $file) {
    get-WmiObject win32_logicaldisk -ComputerName $node |
    ft SystemName,DeviceID,@{Label="Total SIze";Expression={$_.Size / 1gb -as [int] }},@{Label="Free Size";Expression={$_.freespace / 1gb -as [int] }} -autosize | Out-File $filename -append -noclobber }

    Thanks a lot!

You must be logged in to reply to this topic.