Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Two Parts to this script #
- # Part 1: Generate your encrypted password #
- # Do not include this in the end script - run it as its own separate instance #
- # Prompt you to enter the username and password #
- $credObject = Get-Credential
- # The credObject now holds the password in a ‘securestring’ format #
- $passwordSecureString = $credObject.password
- # Define a location to store the AESKey #
- $AESKeyFilePath = "\\path\to\encrypt\aeskey.txt"
- # Define a location to store the file that hosts the encrypted password #
- $credentialFilePath = "\\path\to\encrypt\credpassword.txt"
- # Generate a random AES Encryption Key. #
- $AESKey = New-Object Byte[] 32
- [Security.Cryptography.RNGCryptoServiceProvider]::Create().GetBytes($AESKey)
- # Store the AESKey into a file. This file should be protected! (e.g. ACL on the file to allow only select people to read) #
- Set-Content $AESKeyFilePath $AESKey # Any existing AES Key file will be overwritten #
- $password = $passwordSecureString | ConvertFrom-SecureString -Key $AESKey
- Set-Content $credentialFilePath $password
- # ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ #
- # Part 2: Elevate Powershell and run Robocopy within it to create a new folder, preserving non-inheritable permissions. #
- # Global Path #
- $path = "\\server\folder"
- # Set up path and user variables #
- $AESKeyFilePath = "$path\aes_encrypt\aeskey.txt" # location of the AESKey #
- $SecurePwdFilePath = "$path\aes_encrypt\credpassword.txt" # Location of the file that hosts the encrypted password #
- $userUPN = "domain\user" # User account login #
- # Use key and password to create local secure password #
- $AESKey = Get-Content -Path $AESKeyFilePath
- $pwdTxt = Get-Content -Path $SecurePwdFilePath
- $securePass = $pwdTxt | ConvertTo-SecureString -Key $AESKey
- # Create a new psCredential object with required username and password #
- $adminCreds = New-Object System.Management.Automation.PSCredential($userUPN, $securePass)
- #Variables for Robocopy #
- $server = "server"
- $projectnumber = Read-Host "Enter Project Number"
- $source = "$path\2019\PROJECT TEMPLATE"
- $destination = "$path\2019\$projectnumber"
- # Use the $adminCreds to run elevated Robocopy #
- Invoke-Command -ComputerName "$server" -Credential $adminCreds -ScriptBlock {
- $cmdArgs = @("$using:source", "$using:destination")
- Robocopy $cmdArgs /MIR /SEC /R:1 /W:1
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement