Outputting to TXT problem

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?

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

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.

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

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.

Thanks for the responses guys.

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

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

$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: (:slight_smile: [], ParentContainsErrorRecordEx
      ception
    • FullyQualifiedErrorId : EmptyPipeElement

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

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 :slight_smile:

Also Andrew… you changed what I sent you?

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

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!