Advertisement
ReverendDS

Robocopy Create From Template Without Admin

Mar 26th, 2019
1,043
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Two Parts to this script #
  2.  
  3. # Part 1: Generate your encrypted password #
  4. # Do not include this in the end script - run it as its own separate instance #
  5. # Prompt you to enter the username and password #
  6.  $credObject = Get-Credential
  7.  
  8.  # The credObject now holds the password in a ‘securestring’ format #
  9.  $passwordSecureString = $credObject.password
  10.  
  11.  # Define a location to store the AESKey #
  12.  $AESKeyFilePath = "\\path\to\encrypt\aeskey.txt"
  13.  # Define a location to store the file that hosts the encrypted password #
  14.  $credentialFilePath = "\\path\to\encrypt\credpassword.txt"
  15.  
  16.  # Generate a random AES Encryption Key. #
  17.  $AESKey = New-Object Byte[] 32
  18.  [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($AESKey)
  19.  
  20.  # Store the AESKey into a file. This file should be protected! (e.g. ACL on the file to allow only select people to read) #
  21.  Set-Content $AESKeyFilePath $AESKey # Any existing AES Key file will be overwritten #
  22.  $password = $passwordSecureString | ConvertFrom-SecureString -Key $AESKey
  23.  Set-Content $credentialFilePath $password
  24.  
  25. # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
  26.  
  27. # Part 2: Elevate Powershell and run Robocopy within it to create a new folder, preserving non-inheritable permissions. #
  28. # Global Path #
  29. $path = "\\server\folder"
  30.  
  31. # Set up path and user variables #
  32. $AESKeyFilePath = "$path\aes_encrypt\aeskey.txt" # location of the AESKey #
  33. $SecurePwdFilePath = "$path\aes_encrypt\credpassword.txt" # Location of the file that hosts the encrypted password #              
  34. $userUPN = "domain\user" # User account login #
  35.  
  36. # Use key and password to create local secure password #
  37. $AESKey = Get-Content -Path $AESKeyFilePath
  38. $pwdTxt = Get-Content -Path $SecurePwdFilePath
  39. $securePass = $pwdTxt | ConvertTo-SecureString -Key $AESKey
  40.  
  41. # Create a new psCredential object with required username and password #
  42. $adminCreds = New-Object System.Management.Automation.PSCredential($userUPN, $securePass)
  43.  
  44. #Variables for Robocopy #
  45. $server = "server"
  46. $projectnumber = Read-Host "Enter Project Number"
  47. $source = "$path\2019\PROJECT TEMPLATE"
  48. $destination = "$path\2019\$projectnumber"
  49.  
  50. # Use the $adminCreds to run elevated Robocopy #
  51. Invoke-Command -ComputerName "$server" -Credential $adminCreds -ScriptBlock {
  52.     $cmdArgs = @("$using:source", "$using:destination")
  53.     Robocopy $cmdArgs /MIR /SEC /R:1 /W:1
  54. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement