Photo Gallery Member List Search Calendars FAQ Ticket List Log Out


Using Powershell to Check Files

 
Logged in as: Guest
arrSession:exec spGetSession 2,5,53557
 Active Users: There are 0 members and 0 guests.
 Users viewing this topic: none
 

 

 
  
  Printable Version
All Forums >> [Scripting] >> Windows PowerShell >> Using Powershell to Check Files
  Do you like VisualBasicScript.com? Link to us and help spread the word about our forum. Thanks!
Page: [1]
Login
Message << Older Topic   Newer Topic >>
 Using Powershell to Check Files - 11/14/2007 8:30:37 PM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi all,

I am trying to write a script using powershell that will check a folde containing different files and depending on the file name and file type move it somewhere else.  As im new to this I have got very far but guess I need to learn regular expressions.  Would anyone beable to show me what needs to be done or point me in the right directions.  

So far I have got this far.

cls
# Powershell script to list the .doc files under the test folder
$Dir = get-childitem C:\test -recurse
#$Dir |get-member
$List = $Dir | where {$_.extension -eq ".doc"}
$List |ft fullname |out-file C:\test\log.txt
$List | format-table name

At the moment this script only checks for the doc files but I would like it to check the file name as well.  Is this easy to do?

Thanks for your help

_____________________________

Regards
Spooter
 
 
Post #: 1
 
 RE: Using Powershell to Check Files - 11/19/2007 7:04:21 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
You don't necessarily have to use a regular expression. You can use wild cards.

PS C:\> gci -filter "micro*.pdf"

This, for example, will find all pdf files that start with 'micro'.  What you are attempting should be a pretty easy one-line command.  Actually, you could simply use a filter or include with the Move-item cmdlet:

PS C:\> move micro*.pdf c:\backup

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 2
 
 RE: Using Powershell to Check Files - 11/19/2007 7:10:38 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
I forgot you wanted to recurse. So use an expression like this:

dir micro*.pdf -recurse | move -dest c:\backup

What file pattern are you trying to create?

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 3
 
 RE: Using Powershell to Check Files - 11/20/2007 2:22:07 AM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi all,

Thanks for the reply that is useful to know.

At the moment my script looks like this, but I plan on expanding it to be more complex when doing the regular expressions so that is sends  a email informing me of the files not accepted as well.  I would like it so I could extract the file owner name from the log file and lookup the name in Active Directory to resolve their email address and add it to the CC field so they recieve the notification email as well.

#Scan Folder Test and ALL Subfolders
$Dir = get-childitem C:\Test

#Scan Variable $Dir for accepted file extensions .xls and .doc
$List = $Dir | where {$_.extension -eq ".xls";".doc"}

#Output accepted file extensions to log.txt
$List |ft name |out-file C:\Test\log.txt

#Scan files listed in the text file to meet the following criteria
#The first character is a number, 1 through 9
#The fifth, sixth, and seventh characters are GRP
#Stores Pattern Matched Files in $accepted
$accepted = Get-Childitem C:\Test\log.txt | select-string -pattern ^[1-9]...GRP | foreach {$_.line}

#Gets the owner security descriptor for files stored in $accepted variable
Get-acl $accepted | out-file C:\Test\log.txt
$results = Get-content C:\Test\log.txt
$results | format-list Path, Owner

#########################################Sending SMTP Email#########################################

#Sending SMTP Email Code
  $smtpServer = “SMTPSERVER”
  $msg = new-object Net.Mail.MailMessage
  $att = new-object Net.Mail.Attachment($filename)
   
  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  $msg.From = “”
  $msg.To.Add(””)
  $msg.Cc.Add(””)
  $msg.Subject = “Files Ready for Upload”
  $msg.Body = “The listed files have been accepted for upload - $results”
  $msg.Attachments.Add($att)
  $smtp.Send($msg)
 
############################Inform User of Files which failed the check############################

#Moves Accepted File type and Patten matched Document to folder for Transfer
  Move-Item $accepted C:\Test\accepted_files

#Scan Folder Test 2nd Time and ALL Subfolders
   $Failed = get-childitem C:\Test
  
#Display Unaccepted Files

write "The below files either do not meet the required naming convention or permitted file extension"

   $Failed | format-table name

Please let me know if im doing this  in a very long winded way or any improvements I should make, I have no expereince of programs so have been putting this together using the help files and online guides.  At the moment I also get a error message if there is know files that match when it tries to move the files.

Thanks

(in reply to SAPIENScripter)
 
 
Post #: 4
 
 RE: Using Powershell to Check Files - 11/20/2007 3:10:39 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
I'm still looking at the rest of your code and what you are trying to do. But here is a one liner that should get the files you need and create a text file

PS C:\> dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} | get-acl | format-table  path,owner -auto | out-file report.txt

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 5
 
 RE: Using Powershell to Check Files - 11/20/2007 3:24:36 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
I think I have a better idea now.  Here's a tweak on my last post:

PS C:\> dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted | get-acl | format-table  path,owner -auto | out-file report.txt

Now you'll have $accepted which is the list of files that match your filter.

I'd also turn your mail code into a function and put it in your profile. 

Function Send-SMTPMail {
Params (
[string]$To="Admin@company.com",[string]$From="Admin@company.com",[[string]$CC="",[string]$Subject,[string],[string]$body,[fileinfo]$filename
)
#Sending SMTP Email Code
  $smtpServer = “SMTPSERVER”
  $msg = new-object Net.Mail.MailMessage
  $att = new-object Net.Mail.Attachment($filename)
   
  $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  $msg.From = $from
  $msg.To.Add($to)
  $msg.Cc.Add($cc)
  $msg.Subject = $subject
  $msg.Body = $body
  $msg.Attachments.Add($att)
  $smtp.Send($msg)
}

PS C:\> Send-SMTPMail "me@company.com" "server@company.com" "" "Files ready for upload" The listed files have been accepted for upload $results"  "report.txt"

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to SAPIENScripter)
 
 
Post #: 6
 
 RE: Using Powershell to Check Files - 11/20/2007 8:05:56 PM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi SAPIENScripter

Thanks very much for your help that certainly makes things simpler.  I have a couple of other questions if that ok.

Now that I have the variable $accepted and the results.txt file am I able to use the Get-Content Cmdlet to select the file owner listed against each files and then using that information do a lookup on active directory to find the owners email address?

At the moment it sends me a email so I can inform the user if a file was/wasnt accepted but it would be even better if I could make that part automatic.

Please let me know if you have any questions

Regards
Spooter
 
 

(in reply to SAPIENScripter)
 
 
Post #: 7
 
 RE: Using Powershell to Check Files - 11/20/2007 11:48:55 PM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
You can still pipe $accepted to Get-ACL to get owner information. This means you could do something like this:

$accepted | get-acl | foreach { write-host $_.owner $_.path}

Instead of Write-host, insert the name of a function you have that looks up the user and sends the email. You might want the function to accept parameters for name and file. Or you may want to do something else entirely, but you have options.

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 8
 
 RE: Using Powershell to Check Files - 11/21/2007 1:08:56 AM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
I will give this a try and let you know how I get on.

(in reply to SAPIENScripter)
 
 
Post #: 9
 
 RE: Using Powershell to Check Files - 12/9/2007 9:31:38 PM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi,
I have a questions regarding the results outputted using the line

dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted | Get-Acl | Select-Object owner -OutVariable fileown | Format-Table owner | Out-File "c:\files\report.txt"


My question is regarding the acl output to variable $accepted and the text file. I would like to just have the owner username outputted but I get the below output. Is there anyway I can get this or use the something to remove the unwanted text for the file.

Output Text (Report.txt)


Owner
-----
domain\userid
domain\userid
domain\userid


But ideally I would like the output to be, so I can read it into a function.

Userid
Userid
Userid
 
Thanks

< Message edited by spooter -- 12/9/2007 9:32:43 PM >

(in reply to spooter)
 
 
Post #: 10
 
 RE: Using Powershell to Check Files - 12/9/2007 10:26:54 PM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
You don't really need format-table then and you can't use Select.  I think something like this will give you what you want:

dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted | foreach { (Get-Acl).Owner }  | Out-File "c:\files\report.txt"

You lose your fileown variable this way though, although you could do:
dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted | foreach { (Get-Acl).Owner }  | Tee-object "c:\files\report.txt" -outvariable fileown

But I don't think you need to do any of this really.  You could still have a function that can accept the input from the variable. There are a few ways you could do this.  Here's one:

function Test-me {
Param ([string]$var="foo")
write $var
}

$fileown | %{ test-me $_.owner}

Don't feel you have to make your variables like text files.  PowerShell is object based which actually makes automation easier once you make the paradigm shift.




_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 11
 
 RE: Using Powershell to Check Files - 12/9/2007 10:44:11 PM   
  marcussmith

 

Posts: 5
Score: 0
Joined: 1/30/2006
Status: offline
Mistake, please ignore

< Message edited by marcussmith -- 12/9/2007 10:46:47 PM >

(in reply to SAPIENScripter)
 
 
Post #: 12
 
 RE: Using Powershell to Check Files - 12/9/2007 10:49:46 PM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
That  make things easier but the output to the results.txt file is still domainname\userid, is there anyway I can just get the userid?  or will I have to use a separte line of code that removes the domainname\ part?  Maybe using set-content?

Thanks for your quick replys, think im gonna have to order a book.  Also is there a free editor you recommend for powershell?  I normally use SCITE but it doesnt recognise the powershell syntax so I have started using the trial version of PrimalScript.

(in reply to marcussmith)
 
 
Post #: 13
 
 RE: Using Powershell to Check Files - 12/9/2007 10:57:08 PM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
The Owner property is stored in that format. But you could easily parse it, even in your function.

$var="domain\username"
$domain=$var.split("\")[0]
$user=$var.split("\")[1]

Again, think objects.  And I'm not aware of any freeware editors that support PowerShell. There is the older PowerShell IDE which might work for you.  I'd resort to googling "freeware powershell editor"

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 14
 
 RE: Using Powershell to Check Files - 12/10/2007 12:33:18 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
I forgot that fellow PowerShell MVP Karl Prosser is making his PowerShell Plus tool available for free for personal user:

http://powershelllive.com/blogs/shelltools/default.aspx

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 15
 
 RE: Using Powershell to Check Files - 12/10/2007 1:33:30 AM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi Sapien

If I used the line of code below to output the owners to $fileown how would I read each line for the "domain\" part and remove it?

dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted | foreach { (Get-Acl).Owner }  | Tee-object "c:\files\report.txt" -outvariable fileown

I have tried using...

$fileown | foreach { $_ -replace 'domain', '' }

but this doesnt allow me to remove the "\" and it complains if I try to include it, I presume because its not text.


Regards

(in reply to SAPIENScripter)
 
 
Post #: 16
 
 RE: Using Powershell to Check Files - 12/10/2007 1:44:13 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
$fileown | % { $_.Split("\")[1] }

this will give you just names.  You could send this to a file, or another variable

$fileowners = $fileown | % { $_.Split("\")[1] }

Or simply pipe it to another cmdlet or function that accepts pipeline input

$fileown | % { $_.Split("\")[1] } | Send-OwnerMail

_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 17
 
 RE: Using Powershell to Check Files - 12/11/2007 2:40:55 AM   
  spooter

 

Posts: 15
Score: 0
Joined: 11/14/2007
Status: offline
Hi all,

I am trying to format the text displayed in a email using the variable taken from the code below.

dir c:\files -include *.xls,*.doc -rec | where {($_.Name).toUpper() -match [regex]"^[1-9]...GRP"} -outvariable accepted
 
I am trying to use the accepted variable to display the contents in a email from the line of code below.

$body = "Accepted Files `n $accepted
`n
Regards"


The problem is it displays it in the email like


Accepted Files
C:\files\upload\filename1.doc C:\files\upload\filename2.doc
 
Is there anyway I can list the names in the email?  just like using format-list or format-table? and make text underlined or bold?


Thanks and Regards





(in reply to spooter)
 
 
Post #: 18
 
 RE: Using Powershell to Check Files - 12/11/2007 3:40:15 AM   
  SAPIENScripter


Posts: 261
Score: 2
Joined: 11/1/2006
From: SAPIEN Technologies
Status: offline
This gets tricky because $accepted is an collection of objects.  You might need to create a text file for the body and then use that as part of the message.

 "Accepted Files `n" | out-file body.txt
for ($i=0;$i -lt $accpeted.length;$i++) {$accepted[$i] | out-file body.txt -append}
"`n  Regards" | out-file body.txt -append

Now when you send the mail:
$body=Get-content body.txt


_____________________________

Jeffery Hicks
Windows PowerShell MVP
SAPIEN Technologies - Scripting, Simplified. www.SAPIEN.com

(in reply to spooter)
 
 
Post #: 19
 
 
 
  

If you found our site useful please link to us <a href="http://www.visualbasicscript.com">VisualBasicScript.com</a>.
All Forums >> [Scripting] >> Windows PowerShell >> Using Powershell to Check Files Page: [1]
Jump to:





New Messages No New Messages
Hot Topic w/ New Messages Hot Topic w/o New Messages
Locked w/ New Messages Locked w/o New Messages
 Post New Thread
 Reply to Message
 Post New Poll
 Submit Vote
 Delete My Own Post
 Delete My Own Thread
 Rate Posts