Solved

Modify my little PowerShell script

Posted on 2014-03-11
7
672 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Comparison of Amazon Drive, Google Drive, OneDrive

What is Best for Backup: Amazon Drive, Google Drive or MS OneDrive? In this free whitepaper we look at their performance, pricing, and platform availability to help you decide which cloud drive is right for your situation. Download and read the results of our testing for free!

 

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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

739 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