Get-Ryan

[PowerShell] Disable Broken Shortcut Cleanup - Win7

Nov 9th, 2015
547
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2. <#
  3. .Note that this script requires the use of the NTFSSecurity module
  4. which can be obtained at https://ntfssecurity.codeplex.com/
  5. #>
  6.  
  7. $computers = Get-ADComputer <#your filter here#>
  8. $contentArray = New-Object System.Collections.ArrayList
  9.  
  10. ForEach($computer in $computers){
  11.  
  12.     $path = "\\$($computer.Name)\c$\Windows\diagnostics\scheduled\Maintenance\TS_BrokenShortcuts.ps1"
  13.  
  14.     Set-NTFSOwner -Path $path -Account <#domain\adminaccount#>
  15.     Add-NTFSAccess -Path $path -Account <#domain\adminaccount#> -AccessRights FullControl
  16.  
  17.     $content = Get-Content -Path $path
  18.  
  19.     $content | ForEach-Object{$contentArray.Add($_) | Out-Null}
  20.  
  21.     ForEach($line in $contentArray){
  22.  
  23.         If ($line -match '\[string\]\$list = ""' -and ($contentArray[$contentArray.IndexOf($line) + 1]) -notmatch '<#'){
  24.             $contentArray.Insert(($contentArray.IndexOf($line) + 1), "    <#")
  25.             Break
  26.         }
  27.    
  28.     }
  29.  
  30.     ForEach($line in $contentArray){
  31.  
  32.         If ($line -match 'return \$list' -and ($contentArray[$contentArray.IndexOf($line) - 1] -notmatch '#>')){
  33.             $contentArray.Insert($contentArray.IndexOf($line), "    #>")
  34.             Break
  35.         }
  36.  
  37.     }
  38.  
  39.     $contentArray | Set-Content -Path $path
  40.  
  41.     Set-NTFSOwner -Path $path -Account 'NT SERVICE\TrustedInstaller'
  42.     Get-NTFSAccess -Path $path | Where-Object {$_.Account -like "*<adminaccount>"} | Remove-NTFSAccess
  43.  
  44. }
Advertisement
Add Comment
Please, Sign In to add comment