by Milo at 2013-02-22 05:22:13
I’m struggeling with following issue:by Klaas at 2013-02-22 05:48:29
Someone requested me a logfile in Excel containing information parsed out several Shared folders.
BaseName,"Extension","Length","CreationTime","LastWriteTime","FolderlName"
last modified by & created by were also requested but i know these objects cannot be retrieven or am i wrong?
Localy this works, getting following output:
BaseName,"Extension","Length","CreationTime","LastWriteTime","FullName"
TempBadges1day,"","21/02/2013 10:45:15","21/02/2013 10:45:16","C:\test\TempBadges1day"
batchfile.csv,".log","91","21/02/2013 10:45:34","3/10/2012 11:30:02","C:\test\batchfile.csv.log"
But when I try this on server I get no output…
this is my code:cls
$Logfile = "c:\script\log999.csv"
if (Test-Path $Logfile)
{
Remove-Item $Logfile
}
New-Item -ItemType File -Path $Logfile
function GetFiles
{
param ( [Parameter(Mandatory=$true)] [String]$Path )
foreach ($item in Get-ChildItem $path -recurse -ErrorAction SilentlyContinue)
{
write-host $item
$item | select Basename,Extension,Length,CreationTime,LastWriteTime,FullName
}
}
GetFiles| export-csv -Path $Logfile
Can I get some help please???
Because I cannot sort it out anymore
How exactly do you ‘try this on server’?by Milo at 2013-02-22 06:03:15
On what computer did you save the script and on what computer do you execute what command?
I saved & runned this from my computer.by Klaas at 2013-02-22 06:27:19
I mapped the unc path as a Shared drive (Z:) for instance
when I perform this codeGet-ChildItem z:\ -recurse | ForEach-Object { $.Basename,$.Extension,$.Length,$.CreationTime,$.LastWriteTime,$.FullName} > c:\script\TEST1234.TXTthen I get the output instantly, but not in the format I want it…
by Milo at 2013-02-22 07:02:36Get-ChildItem z:\ -recurse | Select Basename, Extension, Length, CreationTime, LastWriteTime, FullName | Export-csv C:\script\Filelist.csv -NotypeInformation
You probably want to add a filter to collect either files or directories.
[quote="Klaas"]by Klaas at 2013-02-22 07:50:29Get-ChildItem z:\ -recurse | Select Basename, Extension, Length, CreationTime, LastWriteTime, FullName | Export-csv C:\script\Filelist.csv -NotypeInformation
You probably want to add a filter to collect either files or directories.[/quote]
This would be a great feature!
So my mistake was not adding -NotypeInformation to the export ?
not exactlyby Klaas at 2013-02-22 07:54:43
You did not use Select, so the default action in the foreach loop was to send everything to out-default,which you then redirected to a text file as a collection of strings.The foreach is not necessary here because all objects are sent through the pipeline and Select accepts this pipeline input.
The export-csv both treats the input as objects and writes it as a table to a .csv file. The -NoTypeInformation just deletes an informational row above the records.
You can filter by adding the -File switch (or -Directory if you want those):by Milo at 2013-02-24 23:59:24Get-ChildItem z:\ -recurse -File | Select Basename, Extension, Length, CreationTime, LastWriteTime, FullName | Export-csv C:\script\Filelist.csv -NotypeInformation
If you want more options you can also add a -Filter parameter.
TryGet-Help Get-ChildItem -Full | moreto learn more about the possibilities.
Huge Thanks co-Belgian
In fact, Get-Childitem does not know -File as switch (I’m still using v2)
I had to change this into|Where-Object {$_ -is [IO.FileInfo]}|
I also build a little automatisation into it …cls
function GetFiles
{
#Calling in the parameters used for the further script
param ( [Parameter(Mandatory=$true, Position=0)][String]$Path,
[Parameter(Mandatory=$true, Position=1)][string]$csv)
Get-ChildItem $Path -recurse|Where-Object {$_ -is [IO.FileInfo]}| Select Basename, Extension, Length, CreationTime, LastWriteTime, FullName | Export-csv $csv -NotypeInformation
}
GetFiles
In this way we can copy this part into further scripts if needed
Once again… thanks to help me on this struggle
For me this topic may be closed, as my question has been answered, and I did learn new things (again :))