Advertisement
Guest User

Untitled

a guest
Oct 31st, 2016
139
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.90 KB | None | 0 0
  1. Start-Transcript -Path "logs\last_run.log"
  2.  
  3. Function Write-Log($string) {
  4. $timestamp = Get-Date -Format "s"
  5. Write-Host "[$timestamp]"$string
  6. }
  7.  
  8. Write-Log "Loading Visual Basic"
  9. [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') | Out-Null
  10.  
  11. $scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
  12.  
  13. Write-Log "Finding servers.csv"
  14. if (Test-Path $scriptPath\servers.csv) {
  15. Write-Log "Found servers.csv in same folder"
  16. $csv = Import-Csv $scriptPath\servers.csv
  17. } elseif (Test-Path $scriptPath\..\servers.csv) {
  18. Write-Log "Found servers.csv in ..\"
  19. $csv = Import-Csv $scriptPath\..\servers.csv
  20. } else {
  21. Write-Log "Could not find servers.csv"
  22. [Microsoft.VisualBasic.Interaction]::MsgBox("Could not find .\servers.csv or ..\servers.csv.", 16, "Error")
  23. exit
  24. }
  25.  
  26. Write-Log "Requesting domain credentials"
  27. $domainUser = [Environment]::UserDomainName+"\"+[Environment]::UserName
  28. $credentials = Get-Credential -Message "Domain Admin Credentials" -UserName $domainUser
  29.  
  30. Write-Log "Requesting file name to copy"
  31. $fileName = [Microsoft.VisualBasic.Interaction]::InputBox("File to copy:", "Original Filename", "")
  32. Write-Log "Requesting destination"
  33. $uncPath = [Microsoft.VisualBasic.Interaction]::InputBox("Destination (no trailing slash): \\server\", "Path", "")
  34.  
  35. $credPassword = $credentials.GetNetworkCredential().Password
  36. $credUser = $credentials.UserName
  37.  
  38. if ($uncPath -and $fileName -and $credPassword -and $credUser) {
  39. Remove-Item $scriptPath\results.txt | Out-Null
  40.  
  41. Write-Log "Looping over servers"
  42. foreach ($line in $csv) {
  43. $uncServer = $line.Name
  44. Write-Log $uncServer
  45. $server = "\\" + $uncServer + "\C$"
  46.  
  47. Write-Log " Creating network drive"
  48. $networkLocation = New-PSDrive –Root $server -Name "PowerShell" –PSProvider FileSystem -Credential $credentials
  49.  
  50. try {
  51. #$filename = "\\" + $uncServer + "\" + $uncPath + "\" + $fileName
  52. $newfilename = "\\" + $uncServer + "\" + $uncPath
  53.  
  54. if (Test-Path $filename) {
  55. Write-Log " Copying $fileName"
  56. # Rename-Item $filename $newfilename
  57. Copy-Item $fileName $newfilename -Force
  58. Add-Content $scriptPath\results.txt $fileName"`r`n`t"$newfilename
  59. } else {
  60. Write-Log $uncServer":"
  61. }
  62. } catch [System.Exception] {
  63. Write-Log "Error: $_.Exception.Message"
  64. }
  65.  
  66. Write-Log " Removing network drive"
  67. Remove-PSDrive "PowerShell"
  68. }
  69.  
  70. Write-Log "Opening results"
  71. notepad.exe $scriptPath\results.txt
  72.  
  73. } else {
  74. switch ("") {
  75. $uncPath {"Path cannot be empty."}
  76. $fileName {"Filename cannot be empty."}
  77. $uncNewFileName {"New Filename cannot be empty."}
  78. $credPassword {"Password not supplied."}
  79. $credUser {"Username not supplied."}
  80. }
  81. }
  82.  
  83. Stop-Transcript
  84.  
  85. Read-Host
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement