Aquí está mi script que se ejecuta como LocalSystem en una máquina, pero necesita credenciales de un usuario domaim acceder a una ubicación de archivos de red. Le permite almacenar la contraseña del usuario en un archivo cifrado "seguro"; eso solo puede ser leído por el usuario que lo escribió.
El ajuste y cambio de la contraseña se realiza copiando en la máquina un archivo con la contraseña de texto plano. Cuando se ejecuta la secuencia de comandos, lee la contraseña, la encripta y luego elimina la contraseña de texto plano.
$plaintext_password_file = 'C:\plaintext.txt' # Stores the password in plain text - only used once, then deleted
$encryted_password_file = 'C:\copy_pass.txt' # Stores the password in "safe" encrypted form - used for subsequent runs of the script
# - can only be decrypted by the windows user that wrote it
$file_copy_user = 'OURDOMAIN\A_User'
# Check to see if there is a new plaintext password
if (Test-Path $plaintext_password_file)
{
# Read in plaintext password, convert to a secure-string, convert to an encrypted-string, and write out, for use later
get-content $plaintext_password_file | convertto-securestring -asplaintext -force | convertfrom-securestring | out-file $encryted_password_file
# Now we have encrypted password, remove plain text for safety
Remove-Item $plaintext_password_file
}
# Read in the encrypted password, convert to a secure-string
$pass = get-content $encryted_password_file | convertto-securestring
# create a credential object for the other user, using username and password stored in secure-string
$credentials = new-object -typename System.Management.Automation.PSCredential -argumentlist $file_copy_user,$pass
# Connect to network file location as the other user and map to drive J:
New-PSDrive -Name J -PSProvider FileSystem -Root "\\network\file_directory" -Credential $credentials
# Copy the file to J:
Copy-Item -Force -Verbose -Path "C:\a_file.txt" -Destination "J:\"
Como un refinamiento adicional: El nombre de usuario también podría estar cifrada, así, en lugar de codificado.
Aquí hay una pregunta similar. http://stackoverflow.com/questions/10313/can-i-copy-files-to-a-network-place-from-a-script-or-the-command-line – notandy