Payton Flint's Tech Blog
Menu
  • Home
  • Blog
  • Categories
  • Resources
  • About
  • Contact
Menu

PowerShell – Execute Scriptblock as Current User

Posted on August 7, 2024November 8, 2024 by paytonflint

Here’s a script I’ve put together that will retrieve the current user sessions from a remote host using psexec.exe. Then, it executes a scriptblock using the current user session context. Simple, but very powerful. I would suggest not running this in an enterprise environment if you would like to stay in your security team’s good graces. Here is a link to the script on my GitHub:

https://github.com/p8nflnt/psexec/blob/main/Invoke-PsExec.ps1

And here is the script:

<#
.SYNOPSIS
    Execute scriptblock within the current user context for a given host.

.NOTES
    Name: Invoke-PsExec
    Author: Payton Flint
    Version: 1.0
    DateCreated: 2024-Aug

.LINK
    https://github.com/p8nflnt/psexec/blob/main/Invoke-PsExec.ps1
    
PowerShell – Execute Scriptblock Using PsExec
#> # Clear variables for repeatability Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0 function Get-UserSessions { param ( [string]$computerName ) # invoke psexec to query user sessions on param-specified host # using .NET rather than Start-Process to return output w/ variable $procInfo = New-Object System.Diagnostics.ProcessStartInfo $procInfo.FileName = "psexec" $procInfo.RedirectStandardOutput = $true $procInfo.UseShellExecute = $false $procInfo.Arguments = "\\$computerName -nobanner -s -h query user" $proc = New-Object System.Diagnostics.Process $proc.StartInfo = $procInfo $proc.Start() | Out-Null $proc.WaitForExit() $stdOut = $proc.StandardOutput.ReadToEnd() # split output by newline $lines = $stdOut -split "`r?`n" # trim first and last lines if result is returned if ($lines.Length -ge 3) { $lines = $lines[1..($lines.Length - 2)] # initialize sessions array $sessions = @() # for each line from returned result... $lines | ForEach-Object { # use regex to parse each line $_ -match '^(?:\s)(\S+)(?:\s+)(.+)(?:\s+)(\d+)(?:\s\s)(\S+)(?:\s+)(.+)(?:\s\s)(.+)$' | Out-Null # build custom objects with appropriate property/value pairs $obj = [PSCustomObject]@{ Username = $matches[1] SessionName = $matches[2] Id = $matches[3] State = $matches[4] IdleTime = $matches[5] LogonTime = $matches[6] } # add custom object to sessions array $sessions += $obj } } else { Write-Host -ForegroundColor Red "No user sessions found for $computerName" } # return session info return $sessions } function Invoke-PsExec { param ( [string]$computerName, [int]$sessionId, [scriptBlock]$scriptBlock, [bool]$runAsSystem ) # build arguments using system parameter if ($runAsSystem -eq $true) { $psexecArgs = "\\$computerName -s -i $sessionId -nobanner powershell -nop -ep bypass -c `"$scriptBlock`"" } else { $psexecArgs = "\\$computerName -i $sessionId -nobanner powershell -nop -ep bypass -c `"$scriptBlock`"" } # invoke psexec to execute scriptblock on param-specified session # using .NET rather than Start-Process to return output w/ variable $procInfo = New-Object System.Diagnostics.ProcessStartInfo $procInfo.FileName = "psexec" $procInfo.RedirectStandardOutput = $true $procInfo.UseShellExecute = $false $procInfo.Arguments = $psexecArgs $proc = New-Object System.Diagnostics.Process $proc.StartInfo = $procInfo $proc.Start() | Out-Null $proc.WaitForExit() $stdOut = $proc.StandardOutput.ReadToEnd() return $stdOut } $computerName = "<COMPUTER NAME>" $sessions = Get-UserSessions -computerName $computerName $sessions $scriptBlock = { $textCommand = { whoami Write-Host "`r`nHello, World!`r`n" } powershell -noexit -c "$textCommand" } Invoke-PsExec -computerName $computerName -sessionId $($sessions[0].Id) -scriptBlock $scriptBlock -runAsSystem $true

Leave a Reply Cancel reply

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

About The Author

Author's Portrait

In my journey as a technologist and 11 years of experience as an IT professional, I have found my niche as Director of Infrastructure Services; developing my skillsets in management, scripting, cloud infrastructure, identity management, and networking.

I have experience as a Systems Administrator and Engineer for large enterprises including the DoD, government agencies, and a nuclear-generation site.

I've been blessed to collaborate with engineers at esteemed Fortune 50 corporations, and one of Africa's largest, to ensure successful implementation of my work.

GitHub Button

Credentials

M365 Endpoint Administrator Associate
M365 Fundamentals
Microsoft AZ-900
CompTIA CSIS
CompTIA CIOS
CompTIA Security+
CompTIA Network+
CompTIA A+
  • April 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
© 2022 Payton Flint | The views and opinions expressed on this website belong solely to the author/owner and do not represent the perspectives of any individuals, institutions, or organizations, whether affiliated personally or professionally, unless explicitly stated otherwise. The content and products on this website are provided as-is with no warranties or guaranties, are for informational/demonstrative purposes only, do not constitute professional advice, and are not to be used maliciously. The author/owner is not responsible for any consequences arising from actions taken based on information provided on this website, nor from the use/misuse of products from this site. All trademarks are the property of their respective owners.