Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Foreach output formatting

Status
Not open for further replies.

threesocks

Technical User
Feb 9, 2009
1
US
I'm working on the below script but can't find a way to format the results how I want. Basically I want to have the script send the results to either an csv or text file with the header information not inline with the results as I have it below (i.e. Filename would only display once as a column header with all DirectoryName's below it.

$computers = Get-Content "c:\scripts\servers.txt"
$targetDate = '9-29-2010'
$sourceRoot = 'c:\scripts2\'

$inner = ForEach ($computer in $computers) {Get-ChildItem -LiteralPath $sourceRoot -Force -Recurse `
| where-object {$_.lastWriteTime -gt $targetDate} `
| ForEach-Object {
# Echo the changes to screen so you can see what happened
#
$writetime=$_.lastwritetime
$fullname=$_.FullName
$accesstime=$_.lastaccesstime
$creationtime=$_.creationtime
$directoryname=$_.directoryname
"Filename: $_, DirectoryName: $directoryname, CreationTime: $CreationTime, LastWriteTime: $writetime, LastAccessTime: $accesstime, Computer: $computer"}
} $inner | out-file C:\Scripts2\OUTPUT3.TXT

Thanks,

Sean
 
Hi Sean,

Not entirely sure I understand the current situation. But at a first glance I would say your problem lies in the fact that everything you are outputting to the file is in the same loop. If you don't want everything looped, pull what you don't want outside the loop (i.e. Filename).

I'm not also not so sure that declaring the entire script into one variable is necessary. Variables in general should be used to store data to be used in the script, not the entire script itself... may want to test it, but I think you should just be able to pipe the the script into "Out-File" without the $inner variable (seeing as the whole script is one long command).

If you need to add information to a file, just use the -append parameter (i.e. Out-File -Append output3.txt). So you can add text as and when you need it.

Let me know if I've misunderstood the issue!

Thanks,

Nick
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top