<# .NOTES =========================================================================== Author: Jared Shippy Created: 09/19/2014 =========================================================================== .DESCRIPTION Script for updating shortcuts for new file server location .PARAMETER Path Path to target folder. .PARAMETER Recurse Updates shortcuts through subfolders if used. .PARAMETER Whatif Writes no changes, displays files that would be affected. .EXAMPLE # See which shortcuts would be affected in c:\testFolder (subfolders included) lnkUpdate -path c:\testFolder -recurse -whatif .EXAMPLE # Make the perform the change in c:\testfolder ONLY (no subfolders) lnkUpdate -path c:\testfolder #> param ( # Path to root of Targeted Folder [Parameter(Mandatory=$true)] [string]$path, # Perform Recursive? [switch]$recurse, # Does nothing, shows what it *would* do [switch]$whatif ) # Initialization Variables $shell = New-Object -ComObject wscript.shell $from = "oldServer" #The name of the old file server $to = "newServer" #The name of the new file server # Targeting Logic - To recurse or not recurse $lnkFiles = $null if ($recurse -eq $true) { $lnkFiles = Get-ChildItem -Recurse (Join-Path $path \*.lnk) } else { $lnkFiles = Get-ChildItem (Join-Path $path \*.lnk) } foreach ($file in $lnkFiles) { if (($shell.createShortcut($file.fullname)).targetPath -match "$from" -or ($shell.createShortcut($file.fullname)).workingdirectory -match "$from" -or ($shell.createShortcut($file.fullname)).arguements -match "$from") { Write-Host "Match on $($file.fullname)" -ForegroundColor 'DarkCyan' if ($whatif -eq $true) { Write-Host "WHATIF: WOULD replace path here" } else { $fixThis = $shell.createShortcut($file.fullname) $fixThis.targetPath = $fixThis.targetPath -replace "$from", "$to" $fixThis.workingDirectory = $fixThis.workingDirectory -replace "$from", "$to" $fixThis.arguments = $fixThis.arguments -replace "$from", "$to" $fixThis.Save() } } } # check and update links foreach ($shortcut in $shortcuts) { # Find the correct property for the next line: If ($shortcut.property -match "$from") { $shortcut.property -replace "$from", "$to" } else { Write-Host "No $from in this Shortcut" -ForegroundColor Green } }
Wednesday, October 1, 2014
Updating Shortcuts for a File Server replacement
When retiring an older File Server, one has to account for the many possible uses and references to the old file server name. Shortcuts is one that can be addressed via Powershell. The following script finds all shortcuts that have the $from server, and update the references to point to the $to server (these are set in the script beforehand). The only catch is that I didn't build in long file name support, so if you have paths over 255 characters, some additions will be required.
Labels:
automation,
powershell
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment