Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.98 KB | None | 0 0
  1. [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
  2.  
  3. $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  4.  
  5. if (Test-Path $scriptPath\servers.csv) {
  6. $csv = Import-Csv $scriptPath\servers.csv
  7. } elseif (Test-Path $scriptPath\..\servers.csv) {
  8. $csv = Import-Csv $scriptPath\..\servers.csv
  9. } else {
  10. [Microsoft.VisualBasic.Interaction]::MsgBox("Could not find .\servers.csv or ..\servers.csv.", 16, "Error")
  11. exit
  12. }
  13.  
  14. $domainUser = [Environment]::UserDomainName+"\"+[Environment]::UserName
  15. $credentials = Get-Credential -Message "Domain Admin Credentials" -UserName $domainUser
  16.  
  17. $uncPath = [Microsoft.VisualBasic.Interaction]::InputBox("Path (no trailing slash): \\server\", "Path", "")
  18.  
  19. $uncFileName = [Microsoft.VisualBasic.Interaction]::InputBox("Filename:", "Filename", "")
  20.  
  21. $credPassword = $credentials.GetNetworkCredential().Password
  22. $credUser = $credentials.UserName
  23.  
  24. if ($uncPath -and $uncFileName -and $credPassword -and $credUser) {
  25. Remove-Item $scriptPath\results.txt | Out-Null
  26.  
  27. foreach ($line in $csv) {
  28. $uncServer = $line.Name
  29. $server = "\\" + $uncServer + "\C$"
  30.  
  31. $networkLocation = New-PSDrive -Name "PowerShell" –PSProvider FileSystem –Root $server -Credential $credentials
  32.  
  33. try {
  34. $filename = "\\" + $uncServer + "\" + $uncPath + "\" + $uncFileName
  35. if (Test-Path $filename) {
  36. Write-host $uncServer":" $filename
  37. Add-Content $scriptPath\results.txt $filename
  38. } else {
  39. Write-host $uncServer":"
  40. }
  41. } catch [System.Exception] {
  42. Write-host "Error: $_.Exception.Message"
  43. }
  44.  
  45. Remove-PSDrive "PowerShell"
  46. }
  47.  
  48. notepad.exe $scriptPath\results.txt
  49.  
  50. } else {
  51. switch ("") {
  52. $uncPath {"Path cannot be empty."}
  53. $uncFileName {"Filename cannot be empty."}
  54. $credPassword {"Password not supplied."}
  55. $credUser {"Username not supplied."}
  56. }
  57. }
  58. Read-Host
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement