Get-Ryan

Public-ModifyShortcutsV2

Oct 29th, 2015
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Description
  3.  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"'.
  4.  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.
  5. .Notes
  6.  AUTHOR: Ryan Simeone
  7.  LASTEDIT: 10/29/2015
  8. #>
  9.  
  10. Function Search-LinkFilesSingle(){
  11.  
  12.     $oldPath = "\\<oldServer>\*"
  13.     $newPath = "\\<newServer>\"
  14.     $shell = New-Object -ComObject WScript.Shell
  15.     $colFiles = New-Object System.Collections.ArrayList
  16.  
  17.     $shortcutsC = Get-ChildItem -Path "C:\Users\*\Desktop" -Recurse -ErrorAction SilentlyContinue | Where-Object {$_.Extension -eq ".lnk"}
  18.  
  19.     If($shortcutsC -eq $null){Return}
  20.  
  21.     ForEach($file in $shortcutsC) {
  22.  
  23.         $link = $shell.CreateShortcut($file.FullName)
  24.         $currentPath = $link.TargetPath
  25.  
  26.         If($currentPath -like $oldPath) {
  27.  
  28.             $splitPaths = (($link.TargetPath).Remove(0,2)).Split("\\",2)
  29.             $modifiedPath = $newPath+$splitPaths[1]
  30.  
  31.             <###### Remove comment to commit changes, else changes will only be reported, not saved
  32.             $link.TargetPath = $modifiedPath
  33.             $link.Save()
  34.             ######>
  35.            
  36.             If($modifiedPath -like "*.local\*.*"){
  37.                 $Test = [System.IO.File]::Exists($modifiedPath)
  38.             }
  39.             Else{
  40.                 $Test = [System.IO.Directory]::Exists($modifiedPath)
  41.             }
  42.  
  43.             #Prevent false positives for periods in folder names
  44.             If(!$Test){
  45.                 $Test = [System.IO.Directory]::Exists($modifiedPath)
  46.             }
  47.  
  48.             #Generate objects to report changes
  49.             $properties = @{"PC Host Name" = $env:COMPUTERNAME;
  50.                             "Old Path" = $currentPath;
  51.                             "New Path" = $modifiedPath;
  52.                             "Directory Path" = $file.FullName;
  53.                             "Check New Path Exists" = $Test}
  54.             $temp = New-Object -TypeName PSCustomObject -Property $properties
  55.             $colFiles.Add($temp) | Out-Null
  56.  
  57.         }
  58.        
  59.     }
  60.  
  61.     $colFiles
  62.  
  63. }
  64.  
  65. Function Modify-Shortcuts(){
  66.    
  67.     Write-Host "Getting online workstations..."
  68.     $PCs = Get-OnlineComputers
  69.     Write-Host "Complete!"
  70.  
  71.     $colFiless = New-Object System.Collections.ArrayList
  72.  
  73.     ForEach($computer in $PCs){
  74.  
  75.         $fileJob = Invoke-Command -ComputerName $computer.Name -ScriptBlock ${function:Search-LinkFiles} -AsJob
  76.         $computer | Add-Member -MemberType NoteProperty -Name "FileJob" -Value $fileJob -Force
  77.  
  78.     }
  79.  
  80.     Get-Job | Wait-Job | Out-Null
  81.  
  82.     ForEach($computer in $PCs){
  83.         $temp = Receive-Job $computer.FileJob
  84.         ForEach($result in $temp){
  85.             $colFiless.Add($result) | Out-Null
  86.         }
  87.     }
  88.    
  89.     Get-Job | Remove-Job
  90.  
  91.     $colFiless | Select "PC Host Name", "Directory Path", "Old Path", "New Path" | Out-GridView -Title "Shortcuts Changed" -OutputMode None
  92.  
  93. }
  94.  
  95. Modify-Shortcuts
Advertisement
Add Comment
Please, Sign In to add comment