Get-Ryan

Public-ModifyShortcuts

Aug 28th, 2015
248
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: 9/8/2015 10:00 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 {Enabled -eq $true} -SearchBase "OU=<ouName>,DC=<domainName>,DC=<domainSuffix>" -SearchScope Subtree
  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.Name.Substring(12,1)
  31.         [int]$csvNewRuns = 1 + $csvRuns
  32.         $computers = Import-Csv -Path $csvPath$($csvNameFull.Name)
  33.         Remove-Item -Path $csvPath*.csv
  34.         Get-PingTest
  35.  
  36.     }
  37.    
  38. }
  39.  
  40. Function Get-PingTest() {
  41. #Obtain list of PCs from csv or AD query and test connections. Place inactive PCs back in csv for later runs.
  42.    
  43.     $Collection = @()
  44.     $pingTest = $null
  45.     $offPCs = New-Object System.Collections.ArrayList
  46.     $PCs = New-Object System.Collections.ArrayList
  47.    
  48.     Write-Host "Pinging computers..."
  49.     ForEach($computer in $computers) {
  50.  
  51.         $pingTest = Test-Connection -ComputerName $computer.Name -Count 1 -BufferSize 16 -AsJob
  52.         $temp = New-Object PSObject -Property @{Name=$computer.Name;
  53.                                                 Job=$pingTest;
  54.                                                 Return=$null}
  55.         $Collection += $temp
  56.  
  57.  
  58.     }
  59.  
  60.     Get-Job | Wait-Job | Out-Null
  61.  
  62.     Write-Host "Pinging complete!"
  63.     ForEach($col in $Collection){
  64.  
  65.         If($col.Return -eq $null){
  66.  
  67.             $col.Return = Receive-Job $col.Job
  68.            
  69.             If($col.Return.StatusCode -eq 0){
  70.                 $PCs.Add($col) | Out-Null
  71.             }
  72.             Else{
  73.                 $offPCs.Add($col) | Out-Null
  74.             }
  75.         }
  76.  
  77.     }
  78.  
  79.     $Path = "{0}{1} Runs-{2} PCsLeft-{3}.csv" -f $csvPath,$csvName,$csvNewRuns,$offPCs.Count
  80.     #$offPCs | Select Name | Export-Csv -Path $Path -NoTypeInformation
  81.     Get-Job | Remove-Job
  82.     #Search-Shortcuts
  83.     Check-DriveC
  84.  
  85. }
  86.  
  87. Function Check-DriveC(){
  88.  
  89.     $jobs = New-Object System.Collections.ArrayList
  90.     $i = 1
  91.  
  92.     Write-Host "Checking C drive accessibility"
  93.     ForEach($computer in $PCs){
  94.        
  95.         Write-Progress -Activity "Checking for C drive" -PercentComplete (($i/$PCs.Count)*100)
  96.         $TestDrive = [System.IO.Directory]::Exists("\\$($computer.Name)\c$")
  97.         Add-Member -NotePropertyName "Result" -NotePropertyValue $TestDrive -InputObject $PCs[$PCs.IndexOf($computer)] -Force
  98.         $i++
  99.  
  100.     }
  101.  
  102.     #$PCs | Select Name, Result | Out-GridView -Title Results -OutputMode None
  103.     Check-DriveD
  104. }
  105.  
  106. Function Check-DriveD(){
  107.    
  108.     $colPCs = New-Object System.Collections.ArrayList
  109.     $i = 1
  110.  
  111.     Write-Host "Checking D drive accessibility..."
  112.     ForEach($computer in $PCs){
  113.            
  114.         Write-Progress -Activity "Checking for D drive" -PercentComplete (($i/$PCs.Count)*100)
  115.         $TestDrive = [System.IO.Directory]::Exists("\\$($computer.Name)\d$")
  116.         Add-Member -NotePropertyName "Result" -NotePropertyValue $TestDrive -InputObject $PCs[$PCs.IndexOf($computer)] -Force
  117.         $i++
  118.  
  119.     }
  120.  
  121.     Write-Host "D drive check complete!"
  122.  
  123.     If($computer.Result -eq $false){
  124.  
  125.         $colPCs.Add($computer) | Out-Null
  126.  
  127.     }
  128.  
  129.     ForEach($computer in $colPCs){
  130.  
  131.         $PCs.Remove($computer) | Out-Null
  132.         $offPCs.Add($computer) | Out-Null
  133.  
  134.     }
  135.  
  136.     Get-Job | Remove-Job
  137.     #$pcCol | Select Name, Result | Out-GridView -Title "Testing D$ Path" -OutputMode None
  138.     $offPCs | Select Name | Export-Csv -Path $Path -NoTypeInformation
  139.     $colPCs.Clear()
  140.     Search-Shortcuts
  141. }
  142.  
  143. Function Search-Shortcuts() {
  144. #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.
  145.  
  146.     $credential = Get-Credential
  147.     $i = 1
  148.  
  149.     ForEach($computer in $PCs) {
  150.  
  151.         $oldPath1 = "\\<oldserver1>\*"
  152.         $oldPath2 = "\\<oldserver2\*"
  153.         $newPath = "\\<newserver>\"
  154.         $shell = New-Object -ComObject WScript.Shell        
  155.         $shortcuts = $null
  156.  
  157.         Write-Progress -Activity "Obtaining list of shortcuts" -PercentComplete ($i/($PCs.count)*100)
  158.         $i++
  159.  
  160.         $DriveC = New-PSDrive -Name "$($computer.Name)C" -PSProvider FileSystem -Root \\$($computer.Name)\c$ -Credential $credential
  161.         If($DriveC -eq $null){
  162.             continue
  163.         }
  164.  
  165.         If($computer.Result2 -eq $true){
  166.  
  167.             $DriveD = New-PSDrive -Name "$($computer.Name)D" -PSProvider FileSystem -Root \\$($computer.Name)\d$ -Credential $credential
  168.             $desktop = "$($computer.Name)D:\Users\*\Desktop"
  169.             $XPdesktop = "$($computer.name)D:\Documents and Settings\*\Desktop"
  170.             Search-Files
  171.             Remove-PSDrive $DriveD
  172.  
  173.         }
  174.  
  175.         $desktop = "$($computer.Name)C:\Users\*\Desktop"
  176.         $XPdesktop = "$($computer.name)C:\Documents and Settings\*\Desktop"
  177.         Search-Files
  178.         Remove-PSDrive $DriveC
  179.  
  180.     }
  181.  
  182.     #$colPCs | Select "PC Host Name", "Directory Path", "Old Path", "New Path", "Check New Path Exists" | Export-Csv -Path "C:\Scripts\Output\Shortcut-Check-Results.csv" -NoTypeInformation
  183.     $colPCs | Select "PC Host Name", "Directory Path", "Old Path", "New Path", "Check New Path Exists" | Out-GridView -Title "Shortcuts Changed" -OutputMode None
  184. }
  185.  
  186. Function Search-Files(){
  187.  
  188.         Try{
  189.  
  190.             $shortcuts = Get-ChildItem -Path $desktop -Filter "*.lnk" -Recurse -ErrorAction Stop
  191.        
  192.         }
  193.  
  194.         Catch{
  195.  
  196.             $shortcuts = Get-ChildItem -Path $XPdesktop -Filter "*.lnk" -Recurse -ErrorAction SilentlyContinue
  197.             If ($shortcuts -eq $null){
  198.                 return
  199.             }
  200.  
  201.         }
  202.  
  203.         Finally{
  204.  
  205.             ForEach($file in $shortcuts) {
  206.  
  207.                 $link = $shell.CreateShortcut($file.FullName)
  208.                 $currentPath = $link.TargetPath
  209.  
  210.                 If(($currentPath -like $oldPath1) -or ($currentPath -like $oldPath2)) {
  211.  
  212.                     $splitPaths = (($link.TargetPath).Remove(0,2)).Split("\\",2)
  213.                     $modifiedPath = $newPath+$splitPaths[1]
  214.                     #$link.TargetPath = $modifiedPath
  215.                     #$link.Save()
  216.                     If($modifiedPath -like "*.local\*.*"){
  217.                         $Test = [System.IO.File]::Exists($modifiedPath)
  218.                     }
  219.                     Else{
  220.                         $Test = [System.IO.Directory]::Exists($modifiedPath)
  221.                     }
  222.                     $properties = @{"PC Host Name" = $computer.Name;
  223.                                     "Old Path" = $currentPath;
  224.                                     "New Path" = $modifiedPath;
  225.                                     "Directory Path" = $file.DirectoryName;
  226.                                     "Check New Path Exists" = $Test}
  227.                     $temp= New-Object -TypeName PSObject -Property $properties
  228.                     $colPCs.Add($temp) | Out-Null
  229.  
  230.                 }
  231.        
  232.             }
  233.        
  234.         $i++
  235.  
  236.         }
  237.  
  238. }
  239.  
  240. Get-CSV
Advertisement
Add Comment
Please, Sign In to add comment