• Stars
    star
    321
  • Rank 130,752 (Top 3 %)
  • Language
    PowerShell
  • License
    MIT License
  • Created over 4 years ago
  • Updated 7 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

a PowerShell module that allows you to impersonate the currently logged on user, while running PowerShell.exe as system.

RunAsUser Module

This module has been created to have the ability to run scripts under the current user session while the application executing this script only has SYSTEM access. This is especially useful when performing tasks from RMM(Remote Monitoring and Management) systems that do not have the abilty to execute monitoring components in user-space.

This script was originally based on Murrayju his work with CreateProcessAsUser, but has been pratically rewritten by jborean93 to support elevation.

Installation instructions

This module has been published to the PowerShell Gallery. Use the following command to install:

install-module RunAsUser

Usage

To execute a script under the current user you'll need to run the script as SYSTEM using your RMM or other methods. To execute the script run the following command

$scriptblock = { "Hello world" | out-file "C:\Temp\HelloWorld.txt" }
invoke-ascurrentuser -scriptblock $scriptblock

The script will run, store a file with the results in C:\Temp\Helloworld.txt that you can pick up with another PowerShell command such as get-content. The command will wait until the execution of the command is finished. If you do not wish to wait for the command to finish you can use the -NoWait parameter.

$scriptblock = { "Hello world" | out-file "C:\Temp\HelloWorld.txt" }
invoke-ascurrentuser -scriptblock $scriptblock -NoWait

If you want to use the results of the output immediately, you can use the -CaptureOutput switch. This switch captures all output as plain-text. This means you'll have to convert this to a PowerShell Object yourself if you wish to use one:

$scriptblock = { $PSVersiontable | Convertto-json }
$JSON = invoke-ascurrentuser -scriptblock $scriptblock -CaptureOutput
$JSON | ConvertFrom-Json

For longer scripts, that go over the limit of the command line cache, you can use the option -CacheToDisk. This will write the script to the $ENV:TEMP folder, and delete when execution has been done.

$scriptblock = { SUPERLONGSCRIPTHERE }
invoke-ascurrentuser -scriptblock $scriptblock -NoWait -CacheToDisk

At times the launching PowerShell version does not match the version you want the script to run under, some RMM systems initiate PowerShell scripts under their own executable. To prevent issues with this, use the "UseWindowsPowerShell" switch:

$scriptblock = { "Hello world" | out-file "C:\Temp\HelloWorld.txt" }
invoke-ascurrentuser -scriptblock $scriptblock -UseWindowsPowerShell

Sometimes you need to run an application that does not elevate itself, for this use the -NonElevatedSession switch:

$scriptblock = { "Hello world" | out-file "C:\Temp\HelloWorld.txt" }
invoke-ascurrentuser -NonElevatedSession -scriptblock $scriptblock

When you want to capture the output of your script invoked as the user then use the -CaptureOutput switch: $scriptblock = { "Hello world" } invoke-ascurrentuser -scriptblock $scriptblock -CaptureOutput

Examples:

To get the OneDrive files in the currently logged on user profile:

$scriptblock = {
$IniFiles = Get-ChildItem "$ENV:LOCALAPPDATA\Microsoft\OneDrive\settings\Business1" -Filter 'ClientPolicy*' -ErrorAction SilentlyContinue

if (!$IniFiles) {
    write-host 'No Onedrive configuration files found. Stopping script.'
    exit 1
}

$SyncedLibraries = foreach ($inifile in $IniFiles) {
    $IniContent = get-content $inifile.fullname -Encoding Unicode
    [PSCustomObject]@{
        'Item Count' = ($IniContent | Where-Object { $_ -like 'ItemCount*' }) -split '= ' | Select-Object -last 1
        'Site Name'  = ($IniContent | Where-Object { $_ -like 'SiteTitle*' }) -split '= ' | Select-Object -last 1
        'Site URL'   = ($IniContent | Where-Object { $_ -like 'DavUrlNamespace*' }) -split '= ' | Select-Object -last 1
    }
}
$SyncedLibraries | ConvertTo-Json | Out-File 'C:\programdata\Microsoft OneDrive\OneDriveLibraries.txt'
}
Invoke-ascurrentuser -scriptblock $scriptblock
$SyncedLibraries = (get-content "C:\programdata\Microsoft OneDrive\OneDriveLibraries.txt" | convertfrom-json)
if (($SyncedLibraries.'Item count' | Measure-Object -Sum).sum -gt '280000') {
write-host "Unhealthy - Currently syncing more than 280k files. Please investigate."
$SyncedLibraries
}
else {
write-host "Healthy - Syncing less than 280k files."
}

As this script demonstrates, all user variables are the one of the current logged on user, instead of the SYSTEM account. You can also use this to browse the HCKU registry tree, or any files or shares to which only the user has access

Would run the start-sleep command for 60 seconds, but allow you to directly continue other tasks.

Contributions

Feel free to send pull requests or fill out issues when you encounter them. I'm also completely open to adding direct maintainers/contributors and working together! :)

Future plans

Version 1.8 includes all things I required for myself, if you need a feature, shoot me a feature request :)

  • Allow running scripts impersonating the currently logged on user
  • Allow running scripts impersonating the currently logged on user, with elevated token if the user is also a local administrator.
  • Allow running scripts impersonating the currently logged on user, with option to select if elevation is used or not.

More Repositories

1

CIPP

CIPP is a M365 multitenant management solution
JavaScript
791
star
2

CIPP-API

PowerShell
201
star
3

PowerShellWarrantyReports

a repo dedicated to automatic warranty reporting and retrieval from different systems such as IT-Glue, Connectwise, Autotask, and N-central.
PowerShell
169
star
4

AutomaticDocumentation

A repo dedicated to the Automatic Documentation blogs on CyberDrain.com
PowerShell
66
star
5

AutotaskAPI

Autotask 2020.2 REST API PowerShell wrapper
PowerShell
66
star
6

PSdrawIO

PowerShell
47
star
7

RMMIntuneHelper

PowerShell
29
star
8

AzAutomapper

Automatically mapping SharePoint sites in the OneDrive for Business client
PowerShell
26
star
9

PowerShellMonitoring

A repo dedicated to all PowerShell Monitoring blogs on cyberdrain.com
PowerShell
26
star
10

ITGlueBackup

A backup tool for the documentation system IT-Glue.
PowerShell
23
star
11

SecureAppModel

Scripts to create secure application model for Microsoft Partners to access delegated administration automation with scripts, while MFA is enabled.
PowerShell
21
star
12

Webinars

Webinar content
PowerShell
19
star
13

GPODeployment

Script to allow GPO deployment for non-domain joined environments or cloud only environments.
PowerShell
17
star
14

AzGlue

A forwarder for the IT-Glue API hosted in Azure, to secure and move around rate limitations by the IT-Glue API.
PowerShell
16
star
15

SecureScore

SecureScore module to allow easy downloading of the MS secure score
PowerShell
12
star
16

IntuneAutomation

A repo dedicated to automating intune tasks for delegated partners.
PowerShell
11
star
17

AzPwPush

PowerShell
11
star
18

ExchangeOnlineManagement.SAM

PowerShell
11
star
19

AzValidate

PowerShell
9
star
20

AzPam

JavaScript
8
star
21

O365AuditlogSearch

A script that assists in searchable auditlogs and downloading these on a schedule for O365.
PowerShell
7
star
22

KelvinTegelaar

Readme :)
7
star
23

AzNableProxy

PowerShell
7
star
24

O365Squat

PowerShell
7
star
25

TeamsCodeJoiner

PowerShell
5
star
26

AzureDeploy

4
star
27

AzWarrantyUpdate

PowerShell
4
star
28

CyberDrain

Central repo for all CyberDrain.com blogs.
4
star
29

AzDynaDNS

A dyndns managed DNS replacement within Azure.
PowerShell
3
star
30

FasterPartnerPortal

A faster Microsoft Partner Portal generated via PowerShell
PowerShell
3
star
31

AzFunctionExample

PowerShell
1
star