Author Posts

November 25, 2015 at 3:31 am

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?

November 25, 2015 at 4:08 am

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

November 25, 2015 at 4:21 am

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.

November 25, 2015 at 6:20 am

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

November 25, 2015 at 9:30 am

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.

November 26, 2015 at 1:07 am

Thanks for the responses guys.

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

November 26, 2015 at 4:17 am

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

November 30, 2015 at 12:41 am


$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

November 30, 2015 at 12:44 am

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

November 30, 2015 at 12:49 am

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 🙂

November 30, 2015 at 12:50 am

Also Andrew.. you changed what I sent you?

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

December 1, 2015 at 12:56 am

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!