troubleshooting Question

Need help with powershell script to batch update specific shortcut targets and their actual filenames (ie: .LNK shortcut files)

Avatar of sylar
sylarFlag for United States of America asked on
Scripting LanguagesPowershell* Coding
15 Comments1 Solution456 ViewsLast Modified:
Please help! - I need your assistance with coding a powershell script to batch update specific .LNK shortcut targets as well as their .LNK filenames.  I would also like for the powershell script to request input from the admin such as filename / filetype, Folder Target, OldserverName and NewServerName to pass to application via dialogue input boxes and use this input to take action otherwise it should allow to pass options to script via cmdline as well as provide the option of hardcoding into script to run automated.

For example; I have a TEST parent folder which has many .LNK shortcut files scattered in "D:\TEST" parent folder along with sub-folders which also contain misc. LNK files along with many other file types.

I would like for the powershell script to search specifically for the filename in question in our case "*.LNK" in "D:\TEST" and be recursive and identify any .LNK that contain the OldServerName in this example "\\oldserver" and *Replace* it with NewServerName such as "\\newserver".  Currently the below script will accomplish this without any issues.

However; If one of the files for example was called "Accounting on OldServer.lnk" and had a target of "\\oldserver\accounting" - the above script would currently update the file to point to "\\newserver\accounting" however - the name currently would remain as "Accounting on OldServer.lnk" instead of changing to "Accounting on NewServer.lnk"

I would also like for the script to audit every change it makes in an HTML LOG file.  So I would have an audit log of what changes it has completed.  It would be nice if it can create an HTML FILE that would have the below information:

Old Filename                                  New Filename                                  Old Target Path                                       New Target Path
Accounting on OldServer.lnk       Accounting on NewServer.lnk           \\oldserver\accounting                          \\newserver\accounting
HR on Oldserver.lnk                     HR on NewServer.lnk                       \\oldserver.domain.local\hr                \\newserver.domain.local\hr


It would be nice to have some type of error protection should below be example be encountered:

Old Filename:        IS on Oldserver.lnk
Old Target Path:   \\oldserver.domain.local\IS

Old Filename:        Sales on Oldserver.lnk
Old Target Path:    \\oldserver\Sales

And I type in for old server name "\\oldserver" and for new server name I type in "newserver.domain.local" - below would be the result which would cause a problem with "IS" shortcut.

New Filename:         IS on NewServer.lnk
New Target Path:    \\newserver.domain.local.domain.local\IS             --->  This would be invalid - due to no error protection.

New Filename:        Sales on NewServer.lnk
New Target Path:    \\newserver.domain.local\Sales


Below is script I currently have:
$fileName ="*.LNK" 
$folder = "D:\TEST" 
[string]$fromunc = "\\oldserver" 
[string]$tounc = "\\newserver" 
$list = Get-ChildItem -Path $folder -Filter $fileName -Recurse  | Where-Object { $_.Attributes -ne "Directory"} | select -ExpandProperty FullName 
$obj = New-Object -ComObject WScript.Shell 
 
ForEach($lnk in $list) 
      { 
      $obj = New-Object -ComObject WScript.Shell 
      $link = $obj.CreateShortcut($lnk) 
      [string]$path = $link.TargetPath  
      [string]$path = [string]$path.Replace($fromunc.tostring(),$tounc.ToString()) 
      $link.TargetPath = [string]$path 
      $link.Save()
  }
ASKER CERTIFIED SOLUTION
Qlemo
"Batchelor", Developer and EE Topic Advisor
Join our community to see this answer!
Unlock 1 Answer and 15 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 15 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros