Posted On March 29, 2019

PowerShell: Elevating Credential

kimconnect 0 comments
blog.KimConnect.com >> Codes , Windows >> PowerShell: Elevating Credential
$jumpbox="127.0.0.1"

<#
# Static Credentials (unsecured)
$username = (Get-ADDomain).name+"\ADMINISTRATOR"
$password = "PASSWORD"
#>

# Dynamic Credential method 1
$who = whoami
	if ($who.substring($who.length-2, 2) -eq "-a"){$username=$who;}
    else {$username=$who+"-a";}
#$password = Read-Host -Prompt "Input the password for account $username" -AsSecureString
$password=convertto-securestring "PASSWORD" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username,$password
$elevate = New-PSSession -ComputerName $jumpbox -Credential $cred

<#
# Dynamic Credential method 2
$username = (Get-ADDomain).name+(Read-Host -Prompt 'Input the Admin Username: ')
$securedValue = Read-Host -AsSecureString -Prompt "Input the password for account $username"
$password = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($securedValue))
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $Username,$pass
$elevate = New-PSSession -ComputerName $jumpbox -Credential $cred
#>

# Run a script in the context of another user within the current host (no remote execution)
$script="C:\Users\kim\Desktop\test.ps1"
$Args = 'Start-Process -FilePath powershell.exe -ArgumentList \"-ExecutionPolicy Bypass -File "{0}"\" -Verb Runas' -f $script;
start-process powershell.exe -Wait -Credential $cred -NoNewWindow -ArgumentList $Args;

# Remote execution via WinRM
Invoke-Command -Session $elevate -ScriptBlock {
"Running as "+ (whoami)
$proxy="http://proxy:port"
$proxyUri=new-object System.Uri($proxy)
[System.Net.WebRequest]::DefaultWebProxy=new-object System.Net.WebProxy ($proxyUri, $true)
[System.Net.WebRequest]::DefaultWebProxy.Credentials=[System.Net.CredentialCache]::DefaultCredentials

    }

Leave a Reply

Your email address will not be published. Required fields are marked *

Related Post

PowerShell: Gather All Guess VM of All Hyper-V Clusters in the Domain

# getAllVms.ps1 $clusterName='*' function includeRSAT{ $ErrorActionPreference='stop' [Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12 #$rsatWindows7x32='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x86-RefreshPkg.msu' $rsatWindows7x64='https://download.microsoft.com/download/4/F/7/4F71806A-1C56-4EF2-9B4F-9870C4CFD2EE/Windows6.1-KB958830-x64-RefreshPkg.msu' $rsatWindows81='https://download.microsoft.com/download/1/8/E/18EA4843-C596-4542-9236-DE46F780806E/Windows8.1-KB2693643-x64.msu' $rsat1709 = "https://download.microsoft.com/download/1/D/8/1D8B5022-5477-4B9A-8104-6A71FF9D98AB/WindowsTH-RSAT_WS_1709-x64.msu"…

PowerShell: Measure File Copying Time

The scripty: $sourcesAndDestinations=@("C:\Users\kimconnect\Desktop\test\test1 C:\Users\kimconnect\Desktop\test\test2","C:\Users\kimconnect\Desktop\test\test2 C:\Users\kimconnect\Desktop\test\test3")$testPath="C:\Users\kimconnect\Desktop\test\test1"$switchesMirrorDirectories="/MIR /SEC /W:3 /FFT "$dateStamp = Get-Date -Format "yyyy-MM-dd-hh-mm"$currentPath=(Get-Item -Path ".\").FullName$log="/LOG+:$currentPath\robocopy-log-$dateStamp.txt"function…

Windows Active Directory Snapshots

Set scheduled task to run daily and call this maintainSnapshots.bat file:------------------------------@echo offREM Logs Location (Used…