Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- .Description
- This script obtains a list of computers from a csv file if present or queries AD for all enabled PC objects.
- It then checks the connection to those PCs and separates those that responded vs those that didn't.
- With those that responded, it obtains the .lnk files from all profile desktops and modifies them from an old file server
- location to a new one.
- .Notes
- AUTHOR: Ryan Simeone
- LASTEDIT: 9/8/2015 10:00 PM UTC
- #>
- Function Get-CSV() {
- #Check for existing csv containing remaining PC list and query AD for PCs if not found
- $csvPath = "C:\Scripts\Input\"
- #$csvNameFull = Get-Item -Path "$($csvPath)MyTest*.csv" | Select Name
- $csvName = "MyTest"
- If($csvNameFull -eq $null) {
- $csvNewRuns = 1
- $computers = Get-ADComputer -Filter {Enabled -eq $true} -SearchBase "OU=<ouName>,DC=<domainName>,DC=<domainSuffix>" -SearchScope Subtree
- Get-PingTest
- }
- Else {
- #The substring position will depend on your choice of csv file name length
- $csvRuns = $csvNameFull.Name.Substring(12,1)
- [int]$csvNewRuns = 1 + $csvRuns
- $computers = Import-Csv -Path $csvPath$($csvNameFull.Name)
- Remove-Item -Path $csvPath*.csv
- Get-PingTest
- }
- }
- Function Get-PingTest() {
- #Obtain list of PCs from csv or AD query and test connections. Place inactive PCs back in csv for later runs.
- $Collection = @()
- $pingTest = $null
- $offPCs = New-Object System.Collections.ArrayList
- $PCs = New-Object System.Collections.ArrayList
- Write-Host "Pinging computers..."
- ForEach($computer in $computers) {
- $pingTest = Test-Connection -ComputerName $computer.Name -Count 1 -BufferSize 16 -AsJob
- $temp = New-Object PSObject -Property @{Name=$computer.Name;
- Job=$pingTest;
- Return=$null}
- $Collection += $temp
- }
- Get-Job | Wait-Job | Out-Null
- Write-Host "Pinging complete!"
- ForEach($col in $Collection){
- If($col.Return -eq $null){
- $col.Return = Receive-Job $col.Job
- If($col.Return.StatusCode -eq 0){
- $PCs.Add($col) | Out-Null
- }
- Else{
- $offPCs.Add($col) | Out-Null
- }
- }
- }
- $Path = "{0}{1} Runs-{2} PCsLeft-{3}.csv" -f $csvPath,$csvName,$csvNewRuns,$offPCs.Count
- #$offPCs | Select Name | Export-Csv -Path $Path -NoTypeInformation
- Get-Job | Remove-Job
- #Search-Shortcuts
- Check-DriveC
- }
- Function Check-DriveC(){
- $jobs = New-Object System.Collections.ArrayList
- $i = 1
- Write-Host "Checking C drive accessibility"
- ForEach($computer in $PCs){
- Write-Progress -Activity "Checking for C drive" -PercentComplete (($i/$PCs.Count)*100)
- $TestDrive = [System.IO.Directory]::Exists("\\$($computer.Name)\c$")
- Add-Member -NotePropertyName "Result" -NotePropertyValue $TestDrive -InputObject $PCs[$PCs.IndexOf($computer)] -Force
- $i++
- }
- #$PCs | Select Name, Result | Out-GridView -Title Results -OutputMode None
- Check-DriveD
- }
- Function Check-DriveD(){
- $colPCs = New-Object System.Collections.ArrayList
- $i = 1
- Write-Host "Checking D drive accessibility..."
- ForEach($computer in $PCs){
- Write-Progress -Activity "Checking for D drive" -PercentComplete (($i/$PCs.Count)*100)
- $TestDrive = [System.IO.Directory]::Exists("\\$($computer.Name)\d$")
- Add-Member -NotePropertyName "Result" -NotePropertyValue $TestDrive -InputObject $PCs[$PCs.IndexOf($computer)] -Force
- $i++
- }
- Write-Host "D drive check complete!"
- If($computer.Result -eq $false){
- $colPCs.Add($computer) | Out-Null
- }
- ForEach($computer in $colPCs){
- $PCs.Remove($computer) | Out-Null
- $offPCs.Add($computer) | Out-Null
- }
- Get-Job | Remove-Job
- #$pcCol | Select Name, Result | Out-GridView -Title "Testing D$ Path" -OutputMode None
- $offPCs | Select Name | Export-Csv -Path $Path -NoTypeInformation
- $colPCs.Clear()
- Search-Shortcuts
- }
- Function Search-Shortcuts() {
- #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.
- $credential = Get-Credential
- $i = 1
- ForEach($computer in $PCs) {
- $oldPath1 = "\\<oldserver1>\*"
- $oldPath2 = "\\<oldserver2\*"
- $newPath = "\\<newserver>\"
- $shell = New-Object -ComObject WScript.Shell
- $shortcuts = $null
- Write-Progress -Activity "Obtaining list of shortcuts" -PercentComplete ($i/($PCs.count)*100)
- $i++
- $DriveC = New-PSDrive -Name "$($computer.Name)C" -PSProvider FileSystem -Root \\$($computer.Name)\c$ -Credential $credential
- If($DriveC -eq $null){
- continue
- }
- If($computer.Result2 -eq $true){
- $DriveD = New-PSDrive -Name "$($computer.Name)D" -PSProvider FileSystem -Root \\$($computer.Name)\d$ -Credential $credential
- $desktop = "$($computer.Name)D:\Users\*\Desktop"
- $XPdesktop = "$($computer.name)D:\Documents and Settings\*\Desktop"
- Search-Files
- Remove-PSDrive $DriveD
- }
- $desktop = "$($computer.Name)C:\Users\*\Desktop"
- $XPdesktop = "$($computer.name)C:\Documents and Settings\*\Desktop"
- Search-Files
- Remove-PSDrive $DriveC
- }
- #$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
- $colPCs | Select "PC Host Name", "Directory Path", "Old Path", "New Path", "Check New Path Exists" | Out-GridView -Title "Shortcuts Changed" -OutputMode None
- }
- Function Search-Files(){
- Try{
- $shortcuts = Get-ChildItem -Path $desktop -Filter "*.lnk" -Recurse -ErrorAction Stop
- }
- Catch{
- $shortcuts = Get-ChildItem -Path $XPdesktop -Filter "*.lnk" -Recurse -ErrorAction SilentlyContinue
- If ($shortcuts -eq $null){
- return
- }
- }
- Finally{
- ForEach($file in $shortcuts) {
- $link = $shell.CreateShortcut($file.FullName)
- $currentPath = $link.TargetPath
- If(($currentPath -like $oldPath1) -or ($currentPath -like $oldPath2)) {
- $splitPaths = (($link.TargetPath).Remove(0,2)).Split("\\",2)
- $modifiedPath = $newPath+$splitPaths[1]
- #$link.TargetPath = $modifiedPath
- #$link.Save()
- If($modifiedPath -like "*.local\*.*"){
- $Test = [System.IO.File]::Exists($modifiedPath)
- }
- Else{
- $Test = [System.IO.Directory]::Exists($modifiedPath)
- }
- $properties = @{"PC Host Name" = $computer.Name;
- "Old Path" = $currentPath;
- "New Path" = $modifiedPath;
- "Directory Path" = $file.DirectoryName;
- "Check New Path Exists" = $Test}
- $temp= New-Object -TypeName PSObject -Property $properties
- $colPCs.Add($temp) | Out-Null
- }
- }
- $i++
- }
- }
- Get-CSV
Advertisement
Add Comment
Please, Sign In to add comment