Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

powershell to inventory a directory

Posted on 2014-08-11
13
Medium Priority
?
495 Views
Last Modified: 2014-09-05
Can any of you powersehll gurus provide a way to inventory a directory (windows server), and write the inventory to CSV file (including file name, full path, created, modified, accessed, and ideally MD5 hash). I would ideally like the results to be written to CSV/XLSX file, with a hyperlink to the files within the CSV file, so all the user needs to do is click the link within the spreadsheet that will open the file in the directory... Is this do-able?
0
Comment
Question by:pma111
  • 7
  • 5
13 Comments
 
LVL 10

Expert Comment

by:Prashant Girennavar
ID: 40252799
Are you referring to Active directory services from microsoft?

if yes , Do you want to query all the attributes of the objects in AD?

-Prashant Girennavar.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40253352
You can simply edit the fullname column to add the hyperlink in excel, I am looking at an easy way to do it.

$report = @()
gci C:\folder | where {$_.Psiscontainer -eq $false} | % {
write-host "Processing $_.Fullname"
$hashval = Get-FileHash $_.FullName | select -expa hash
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Name' -Value $_.Name
$item | Add-Member -type NoteProperty -Name 'Fullname' -Value $_.FullName
$item | Add-Member -type NoteProperty -Name 'CREATED' -Value $_.CreationTime
$item | Add-Member -type NoteProperty -Name 'ACCESSED' -Value $_.LastAccessTime
$item | Add-Member -type NoteProperty -Name 'MODIFIED' -Value $_.LastWriteTime
$item | Add-Member -type NoteProperty -Name 'FILEHASH' -Value $hashval
$report += $item
}

$report | Export-Csv report.csv -nti

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 40255099
>You can simply edit the fullname column to add the hyperlink in excel

Can you elaborate here as if you have hundreds of files that would take an age to manually hyperlink each file, or is there an automated way to do them in bulk?
0
Evaluating UTMs? Here's what you need to know!

Evaluating a UTM appliance and vendor can prove to be an overwhelming exercise.  How can you make sure that you're getting the security that your organization needs without breaking the bank? Check out our UTM Buyer's Guide for more information on what you should be looking for!

 
LVL 29

Expert Comment

by:becraig
ID: 40256315
Actually there is a really easy fix for this:

$report = @()
gci C:\users\v-craigw\Desktop | where {$_.Psiscontainer -eq $false} | % {
write-host "Processing $_.Fullname"
$hlink = '=HYPERLINK("'+$_.Fullname+'")'
$hashval = Get-FileHash $_.FullName | select -expa hash
$item = New-Object PSObject
$item | Add-Member -type NoteProperty -Name 'Name' -Value $_.Name
$item | Add-Member -type NoteProperty -Name 'Fullname' -Value $hlink
$item | Add-Member -type NoteProperty -Name 'CREATED' -Value $_.CreationTime
$item | Add-Member -type NoteProperty -Name 'ACCESSED' -Value $_.LastAccessTime
$item | Add-Member -type NoteProperty -Name 'MODIFIED' -Value $_.LastWriteTime
$item | Add-Member -type NoteProperty -Name 'FILEHASH' -Value $hashval
$report += $item
}

$report | Export-Csv report.csv -nti

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 40257762
Thanks becraig will give it a go shortly and feedback..
0
 
LVL 3

Author Comment

by:pma111
ID: 40258055
Is there anything I need to change in that ps1 or can it be run as is? I am new to powershell/scripting, but I get an error "unexpected token 'add-member' in expression or statement.
0
 
LVL 29

Expert Comment

by:becraig
ID: 40261127
Can you paste the exact error output when run please.

Also can you give me the output from the following command:
$PSVersionTable.PSVersion
0
 
LVL 3

Author Comment

by:pma111
ID: 40269734
sorry for the late reply I have been off sick

re the version
major.minor.build.revision
2.0.-1.-1
0
 
LVL 3

Author Comment

by:pma111
ID: 40269737
unexpected token 'add-member' in expression or statement.

at <path> file.ps1:7 char:18

Open in new window

0
 
LVL 29

Expert Comment

by:becraig
ID: 40270221
This should work for you:

$report = @()
gci C:\users\Administrator\Desktop | where {$_.Psiscontainer -eq $false} | % {
write-host "Processing $_.Fullname"
$hlink = '=HYPERLINK("'+$_.Fullname+'")'
$hashval = Get-FileHash $_.FullName | select -expa hash

    $item = New-Object PSObject -Property @{
    Name = $_.Name
    Fullname = $hlink
    CREATED = $_.CreationTime
    ACCESSED = $_.LastAccessTime
    MODIFIED = $_.LastWriteTime
    FILEHASH = $hashval          
    }
    $report += $item

}
$report | Export-Csv report.csv -nti
                                          

Open in new window

0
 
LVL 3

Author Comment

by:pma111
ID: 40285331
sorry not provided feedback will try this now
0
 
LVL 3

Author Comment

by:pma111
ID: 40285341
another error with the revised script

unexpected token 'add-member' in expression or statement.
at <removed> script.ps1:7 char:18
0
 
LVL 29

Accepted Solution

by:
becraig earned 2000 total points
ID: 40285795
No clue why this is failing for you and with very little to go on I will just use a different approach:

$report = @()
$report += "Name`tFullname`tCREATED`tACCESSED`tMODIFIED`tFILEHASH"
gci C:\users\Administrator\Desktop\scripts | where {$_.Psiscontainer -eq $false} | % {
$fname=$_.name;$created = $_.CreationTime;$laccess = $_.LastAccessTime;$lwrite = $_.LastWriteTime
write-host "Processing $fname"
$hlink = '=HYPERLINK("'+$_.Fullname+'")'
$hashval = Get-FileHash $_.FullName | select -expa hash
$report += "$fname`t$hlink`t$created`t$laccess`t$lwrite`t$hashval"
}
$report | out-file report.csv

Open in new window

0

Featured Post

Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

My attempt to use PowerShell and other great resources found online to simplify the deployment of Office 365 ProPlus client components to any workstation that needs it, regardless of existing Office components that may be needing attention.
In this post, I will showcase the steps for how to create groups in Office 365. Office 365 groups allow for ease of flexibility and collaboration between staff members.
Learn the basics of modules and packages in Python. Every Python file is a module, ending in the suffix: .py: Modules are a collection of functions and variables.: Packages are a collection of modules.: Module functions and variables are accessed us…
Loops Section Overview
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question