Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Description
- This script sends out a script to multiple PCs to search a path for link files and updates the target path on those files and reports the results back to the calling PC. The calling PC utilizes background jobs with Invoke-Command to run the processes concurrently instead of sequentially to drastically speed up the process. This does require that the PCs to be edited already be configured to allow WinRM connections via 'Enable-PSRemoting'. For reference, you can remotely configure WinRM on target machines using PSExec via the command 'psexec \\<targetHostName> -u <domain\admin account> -p <password> -h powershell.exe "Enable-PSRemoting -Force"'.
- This script is technically another version of my Public-ModifyShortcuts, but the process was so different that I felt it warranted its own entry. Also note the Get-OnlineComputers call is another custom function of mine, but you can substitute any Get-ADComputer search.
- .Notes
- AUTHOR: Ryan Simeone
- LASTEDIT: 10/29/2015
- #>
- Function Search-LinkFilesSingle(){
- $oldPath = "\\<oldServer>\*"
- $newPath = "\\<newServer>\"
- $shell = New-Object -ComObject WScript.Shell
- $colFiles = New-Object System.Collections.ArrayList
- $shortcutsC = Get-ChildItem -Path "C:\Users\*\Desktop" -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.Extension -eq ".lnk"}
- If($shortcutsC -eq $null){Return}
- ForEach($file in $shortcutsC) {
- $link = $shell.CreateShortcut($file.FullName)
- $currentPath = $link.TargetPath
- If($currentPath -like $oldPath) {
- $splitPaths = (($link.TargetPath).Remove(0,2)).Split("\\",2)
- $modifiedPath = $newPath+$splitPaths[1]
- <###### Remove comment to commit changes, else changes will only be reported, not saved
- $link.TargetPath = $modifiedPath
- $link.Save()
- ######>
- If($modifiedPath -like "*.local\*.*"){
- $Test = [System.IO.File]::Exists($modifiedPath)
- }
- Else{
- $Test = [System.IO.Directory]::Exists($modifiedPath)
- }
- #Prevent false positives for periods in folder names
- If(!$Test){
- $Test = [System.IO.Directory]::Exists($modifiedPath)
- }
- #Generate objects to report changes
- $properties = @{"PC Host Name" = $env:COMPUTERNAME;
- "Old Path" = $currentPath;
- "New Path" = $modifiedPath;
- "Directory Path" = $file.FullName;
- "Check New Path Exists" = $Test}
- $temp = New-Object -TypeName PSCustomObject -Property $properties
- $colFiles.Add($temp) | Out-Null
- }
- }
- $colFiles
- }
- Function Modify-Shortcuts(){
- Write-Host "Getting online workstations..."
- $PCs = Get-OnlineComputers
- Write-Host "Complete!"
- $colFiless = New-Object System.Collections.ArrayList
- ForEach($computer in $PCs){
- $fileJob = Invoke-Command -ComputerName $computer.Name -ScriptBlock ${function:Search-LinkFiles} -AsJob
- $computer | Add-Member -MemberType NoteProperty -Name "FileJob" -Value $fileJob -Force
- }
- Get-Job | Wait-Job | Out-Null
- ForEach($computer in $PCs){
- $temp = Receive-Job $computer.FileJob
- ForEach($result in $temp){
- $colFiless.Add($result) | Out-Null
- }
- }
- Get-Job | Remove-Job
- $colFiless | Select "PC Host Name", "Directory Path", "Old Path", "New Path" | Out-GridView -Title "Shortcuts Changed" -OutputMode None
- }
- Modify-Shortcuts
Advertisement
Add Comment
Please, Sign In to add comment