Created
October 8, 2020 20:03
-
-
Save raulunzue/382bd5532c9dfeb1cb1322b5ce428dc9 to your computer and use it in GitHub Desktop.
Elevar credenciales en script Powershell para ejecutar otro diferente
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# PASAMOS CREDENCIALES EN BASE64 - USUARIO | |
$Usuario = 'ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUA'; | |
$otroUsuario = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Usuario)) | |
# PASAMOS CREDENCIALES EN BASE64 - CONTRASEÑA | |
$Pw = 'ZQBsAGIAbABvAGcAZABlAG4AZQBnAHUA' | |
$otroUsuarioClave = [System.Text.Encoding]::Unicode.GetString([System.Convert]::FromBase64String($Pw)) | |
# NOMBRE DE SERVER | |
$hostname = c:\windows\system32\hostname.exe | |
# CONEXION CON CREDENCIALES ELEVADAS | |
$otroUsuarioClaveSS = $otroUsuarioClave | ConvertTo-SecureString -Force -AsPlainText | |
$myCredencial = New-Object System.Management.Automation.PsCredential("$env:userdomain\$otroUsuario", $otroUsuarioClaveSS) | |
$otroUsuarioSesion = New-PSSession -ComputerName $hostname -Credential $myCredencial | |
# PUNTO DE CONTROL | |
Write-Host "Ejecutando bajo el contexto de: $env:userdomain\$env:username" | |
# EJECUCION SCRIPT | |
Invoke-Command -Session $otroUsuarioSesion -FilePath "C:\script-prueba.ps1" | |
# PUNTO DE CONTROL | |
Write-Host "Ejecutando bajo el contexto de: $env:userdomain\$env:username" | |
# CIERRE SESION | |
Get-PSSession | Remove-PSSession |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment