Solved

Modify my little PowerShell script

Posted on 2014-03-11
7
669 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 35

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 35

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

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 35

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 35

Expert Comment

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

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

How to resolve IMCEAEX NDRs in Exchange or Exchange Online related to invalid X500 addresses.
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.
In a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

761 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