Login | |
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
 |
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
|
|
| |
|
|
|
|
|