Advertisement
Guest User

Public-ModifyShortcuts

a guest
Aug 28th, 2015
230
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. .Description
  3.  This script obtains a list of computers from a csv file if present or queries AD for all enabled PC objects.
  4.  It then checks the connection to those PCs and separates those that responded vs those that didn't.
  5.  With those that responded, it obtains the .lnk files from all profile desktops and modifies them from an old file server
  6.  location to a new one.
  7. .Notes
  8.  AUTHOR: Ryan Simeone
  9.  LASTEDIT: 8/28/2015 4:50 PM UTC
  10. #>
  11.  
  12. Function Get-CSV() {
  13. #Check for existing csv containing remaining PC list and query AD for PCs if not found
  14.  
  15.     $csvPath = "C:\Scripts\Input\"
  16.     $csvNameFull = Get-Item -Path "$($csvPath)MyTest*.csv" | Select Name
  17.     $csvName = "MyTest"
  18.  
  19.     If($csvNameFull -eq $null) {
  20.        
  21.         $csvNewRuns = 1
  22.         $computers = Get-ADComputer -Filter * | Where {$_.Enabled -eq $true}
  23.         Get-PingTest      
  24.  
  25.     }
  26.    
  27.     Else {
  28.        
  29.         #The substring position will depend on your choice of csv file name length
  30.         $csvRuns = $csvNameFull.Substring(7,1)
  31.         [int]$csvNewRuns = 1 + $csvRuns
  32.         $computers = Import-Csv -Path $csvPath$csvNameFull
  33.         Get-PingTest
  34.  
  35.     }
  36.    
  37. }
  38.  
  39. Function Get-PingTest() {
  40. #Obtain list of PCs from csv or AD query and test connections. Place inactive PCs back in csv for later runs.
  41.    
  42.     $pingTest = $null
  43.     $offPCs = @()
  44.     $PCs = @()
  45.     $i = 1
  46.    
  47.     ForEach($computer in $computers) {
  48.  
  49.         Write-Progress -Activity "Pinging Computers..." -PercentComplete ($i/$computers.count*100)
  50.         $pingTest = Test-Connection -CN $computer.dnshostname -Count 1 -BufferSize 16 -Quiet
  51.         $i++
  52.  
  53.         If($pingTest -match 'False') {
  54.  
  55.             $offPCs += $computer
  56.  
  57.         }
  58.  
  59.         Else {
  60.  
  61.             $PCs += $computer
  62.  
  63.         }
  64.  
  65.     }
  66.  
  67. $Path = "{0}{1} Runs-{2} PCsLeft-{3}.csv" -f $csvPath,$csvName,$csvNewRuns,$offPCs.Count
  68. $offPCs | Select Name | Export-Csv -Path $Path -NoTypeInformation
  69. Search-Shortcuts
  70.  
  71. }
  72.  
  73. Function Search-Shortcuts() {
  74. #Obtain all .lnk files from all profile desktops and check for old server name and replace with new server name. Also create csv with before and after target paths.
  75.  
  76.     $credential = Get-Credential
  77.     $Field1 = @()
  78.     $Field2 = @()
  79.     $Field3 = @()
  80.     $colFields = @()
  81.  
  82.     ForEach($computer in $PCs) {
  83.  
  84.         $Field1 += $computer.Name
  85.  
  86.         New-PSDrive -Name $computer.Name -PSProvider FileSystem -Root \\$($computer.Name)\c$ -Credential $credential
  87.  
  88.         $oldPath1 = "\\<oldserver1>\*"
  89.         $oldPath2 = "\\<oldserver2>\*"
  90.         $newPath = "\\<newserver>\"
  91.         $shell = New-Object -ComObject WScript.Shell
  92.         $desktop = "$($computer.Name):\Users\*\Desktop"
  93.  
  94.         $shortcuts = Get-ChildItem -Path $desktop -Filter "*.lnk" -Recurse
  95.  
  96.         ForEach($file in $shortcuts) {
  97.            
  98.             $Field2 += $file.FullName
  99.             $link = $shell.CreateShortcut($file.FullName)
  100.             $currentPath = $link.TargetPath
  101.  
  102.             If(($currentPath -like $oldPath1) -or ($currentPath -like $oldPath2)) {
  103.  
  104.                 $splitPaths = (($link.TargetPath).Remove(0,2)).Split("\\",2)
  105.                 $modifiedPath = $newPath+$splitPaths[1]
  106.                 $link.TargetPath = $modifiedPath
  107.                 $link.Save()
  108.                 $Field3 += $modifiedPath
  109.             }
  110.  
  111.         }
  112.  
  113.     }
  114.    
  115.     $colFields = $Field1+$Field2+$Field3
  116.     $colFields | Export-Csv -Path "C:\Scripts\Output\<DesiredFilename>.csv" -NoTypeInformation
  117.  
  118. }
  119.  
  120. Get-CSV
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement