Solved

Modify my little PowerShell script

Posted on 2014-03-11
7
642 Views
Last Modified: 2014-03-11
Hi guys,

I have the following code, works butter smooth. Copies a few files/folders from source to destination then applies ACL's. See that 3rd "copy-item" which gets a .nk2 and copies it over? Well in the source there are MULIPLE .nk2 files (with different names), how can I get it to scan that \outlook folder and copy the biggest (file size) .nk2 item and rename is outlookprofile.nk2?

gc folderlist.txt | % {
$user = "$_"
Import-Csv "C:\TEMP\Favorites\input.csv" | ForEach {
If ($_.FQDN -eq $env:USERDNSDOMAIN) { $file_server = $_.'Server IP' } 
}
$FavAcl = Get-Acl -path \\$file_server\profiles$\$user\Favorites\
$SigAcl = Get-Acl -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures\"

if (Test-Path -Path "\\$file_server\profiles$\$user\Favorites")
{
copy-item -path  \\$file_server\profiles$\$user\Favorites -destination \\$file_server\userhome$\$user\Favorites -recurse -force 
copy-item -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures" -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Signatures\ -recurse -force -ErrorAction SilentlyContinue
copy-item -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook\outlook.nk2" -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlook.nk2  -ErrorAction SilentlyContinue
Get-ChildItem -path \\$file_server\userhome$\$user\Favorites -Recurse -Force | Set-Acl -AclObject $FavAcl
Get-ChildItem -path \\$file_server\userhome$\$user\AppData -Recurse -Force | Set-Acl -AclObject $SigAcl
Get-Acl -path \\$file_server\profiles$\$user\Favorites\ | Set-ACL -path \\$file_server\userhome$\$user\Favorites\
Get-Acl -path  "\\$file_server\profiles$\$user\Application Data\Microsoft\Signatures\" | Set-Acl \\$file_server\userhome$\$user\AppData\
remove-item -path \\$file_server\profiles$\$user -recurse -force
}                                            
}

Open in new window



I'll need it to keep the ACL from source to destination as well.
Thanks guys.
0
Comment
Question by:levertm
  • 4
  • 3
7 Comments
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39920525
Replace line 13 with these 2 lines:
$bigNK2 = Get-ChildItem -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook" -Filter "*.nk2" | sort Length -Descending | select -First 1
copy-item -path  $bigNK2.FullName -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2  -ErrorAction SilentlyContinue

Open in new window

The first will get the largest nk2 file in that folder and the second will copy it as outlookprofile.nk2 in the destination folder.

HTH,
Dan
0
 

Author Comment

by:levertm
ID: 39920679
Hi Dan,

I''m getting an error:
Copy-item : Could not find a part of the path '\\10.x.x.x\userhome$\username\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2'.
Should it not be creating that folder if it doesnt exist? Which it may exist or may not exist.. What is strange is the \Signature folder doesnt exists either in the destination yet that copies without an errors.
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39920715
Yes, it should. If the script was running properly before, it should run the same now...

Can you add this between those 2 lines:
write-output $bigNK2.FullName

That's to see what is the name of the file being copied, maybe that's the problem.
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 

Author Comment

by:levertm
ID: 39920736
Two things:

1) The name of the file is shown and it's correct, I verified both path and file name and it does pick up the biggest file size in the source folder.

2) if the destination folder \outlook exists your additions work, the biggest .NK2 file gets copied and renamed to outlookprofile.nk2 in the destination folder. If the destination folder \outlook does not it exists, your additions do not work.

Strange huh.. yet the signature folder copies over without any hiccups, even if it does not exist.
0
 
LVL 34

Accepted Solution

by:
Dan Craciun earned 500 total points
ID: 39920774
Yup.
Just checked the manual and copy-item expect the path to exist if you copy a file.
If the source is a folder, it will create the path.

So:
$bigNK2 = Get-ChildItem -path "\\$file_server\profiles$\$user\Application Data\Microsoft\Outlook" -Filter "*.nk2" | sort Length -Descending | select -First 1
if (!(Test-Path \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook)) {
    New-Item \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook -itemtype directory
}
copy-item -path  $bigNK2.FullName -destination \\$file_server\userhome$\$user\AppData\Roaming\Microsoft\Outlook\outlookprofile.nk2  -ErrorAction SilentlyContinue

Open in new window

0
 

Author Comment

by:levertm
ID: 39920853
That did it Dan, thank you very much.

:)
0
 
LVL 34

Expert Comment

by:Dan Craciun
ID: 39920870
Glad I could help!
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
Follow this checklist to learn more about the 15 things you should never include in an email signature from personal quotes, animated gifs and out-of-date marketing content.
Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

757 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now