Solved

Modify my little PowerShell script

Posted on 2014-03-11
7
659 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 

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

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

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

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Windows 10 came with  a lot of built in applications, Some organisations leave them there, some will control them using GPO's. This Article is useful for those who do not want to have any applications in their image (example:me).
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

829 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