Skip to content

Commit d17b7c5

Browse files
committed
New version: 2.0
1 parent 48913a4 commit d17b7c5

File tree

6 files changed

+308
-356
lines changed

6 files changed

+308
-356
lines changed

PowerRunAs/PowerRunAs.psd1

Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
1+
#
2+
# Module manifest for module 'PowerRunAs'
3+
#
4+
# Generated by: Jean-Pierre LESUEUR
5+
#
6+
# Generated on: 18/02/2025
7+
#
8+
9+
@{
10+
11+
# Script module or binary module file associated with this manifest.
12+
RootModule = 'PowerRunAs'
13+
14+
# Version number of this module.
15+
ModuleVersion = '2.0.0'
16+
17+
# Supported PSEditions
18+
# CompatiblePSEditions = @()
19+
20+
# ID used to uniquely identify this module
21+
GUID = '5a822778-d1be-4b7b-8a29-ab45e3efb10c'
22+
23+
# Author of this module
24+
Author = 'Jean-Pierre LESUEUR'
25+
26+
# Company or vendor of this module
27+
CompanyName = 'Phrozen'
28+
29+
# Copyright statement for this module
30+
Copyright = '(c) 2025 Phrozen. All rights reserved.'
31+
32+
# Description of the functionality provided by this module
33+
Description = 'This module allows you to run a process (defaulting to PowerShell) as a different user by providing a known username and password. By default, the standard output (stdout), standard error (stderr), and standard input (stdin) are attached to the current console of the caller.'
34+
35+
# Minimum version of the Windows PowerShell engine required by this module
36+
# PowerShellVersion = ''
37+
38+
# Name of the Windows PowerShell host required by this module
39+
# PowerShellHostName = ''
40+
41+
# Minimum version of the Windows PowerShell host required by this module
42+
# PowerShellHostVersion = ''
43+
44+
# Minimum version of Microsoft .NET Framework required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
45+
# DotNetFrameworkVersion = ''
46+
47+
# Minimum version of the common language runtime (CLR) required by this module. This prerequisite is valid for the PowerShell Desktop edition only.
48+
# CLRVersion = ''
49+
50+
# Processor architecture (None, X86, Amd64) required by this module
51+
# ProcessorArchitecture = ''
52+
53+
# Modules that must be imported into the global environment prior to importing this module
54+
# RequiredModules = @()
55+
56+
# Assemblies that must be loaded prior to importing this module
57+
# RequiredAssemblies = @()
58+
59+
# Script files (.ps1) that are run in the caller's environment prior to importing this module.
60+
# ScriptsToProcess = @()
61+
62+
# Type files (.ps1xml) to be loaded when importing this module
63+
# TypesToProcess = @()
64+
65+
# Format files (.ps1xml) to be loaded when importing this module
66+
# FormatsToProcess = @()
67+
68+
# Modules to import as nested modules of the module specified in RootModule/ModuleToProcess
69+
# NestedModules = @()
70+
71+
# Functions to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no functions to export.
72+
FunctionsToExport = @("Invoke-RunAs")
73+
74+
# Cmdlets to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no cmdlets to export.
75+
CmdletsToExport = @()
76+
77+
# Variables to export from this module
78+
VariablesToExport = '*'
79+
80+
# Aliases to export from this module, for best performance, do not use wildcards and do not delete the entry, use an empty array if there are no aliases to export.
81+
AliasesToExport = @()
82+
83+
# DSC resources to export from this module
84+
# DscResourcesToExport = @()
85+
86+
# List of all modules packaged with this module
87+
# ModuleList = @()
88+
89+
# List of all files packaged with this module
90+
# FileList = @()
91+
92+
# Private data to pass to the module specified in RootModule/ModuleToProcess. This may also contain a PSData hashtable with additional module metadata used by PowerShell.
93+
PrivateData = @{
94+
95+
PSData = @{
96+
97+
# Tags applied to this module. These help with module discovery in online galleries.
98+
# Tags = @()
99+
100+
# A URL to the license for this module.
101+
# LicenseUri = ''
102+
103+
# A URL to the main website for this project.
104+
# ProjectUri = ''
105+
106+
# A URL to an icon representing this module.
107+
# IconUri = ''
108+
109+
# ReleaseNotes of this module
110+
# ReleaseNotes = ''
111+
112+
} # End of PSData hashtable
113+
114+
} # End of PrivateData hashtable
115+
116+
# HelpInfo URI of this module
117+
# HelpInfoURI = ''
118+
119+
# Default prefix for commands exported from this module. Override the default prefix using Import-Module -Prefix.
120+
# DefaultCommandPrefix = ''
121+
122+
}

PowerRunAs/PowerRunAs.psm1

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
# ----------------------------------------------------------------------------------- #
2+
# #
3+
# .Developer #
4+
# Jean-Pierre LESUEUR (@DarkCoderSc) #
5+
# https://www.twitter.com/darkcodersc #
6+
# https://github.com/PhrozenIO #
7+
# https://github.com/DarkCoderSc #
8+
# www.phrozen.io #
9+
# jplesueur@phrozen.io #
10+
# PHROZEN #
11+
# .License #
12+
# Apache License #
13+
# Version 2.0, January 2004 #
14+
# http://www.apache.org/licenses/ #
15+
# .Disclaimer #
16+
# This script is provided "as is", without warranty of any kind, express or #
17+
# implied, including but not limited to the warranties of merchantability, #
18+
# fitness for a particular purpose and noninfringement. In no event shall the #
19+
# authors or copyright holders be liable for any claim, damages or other #
20+
# liability, whether in an action of contract, tort or otherwise, arising #
21+
# from, out of or in connection with the software or the use or other dealings #
22+
# in the software. #
23+
# #
24+
# ----------------------------------------------------------------------------------- #
25+
26+
$global:sharedData = @{
27+
"StdErrTick" = 0
28+
}
29+
30+
function Add-StdHandler
31+
{
32+
param (
33+
[Parameter(Mandatory=$true)]
34+
[System.IO.StreamReader] $Stream,
35+
36+
[bool] $StandardErrorFlag = $false
37+
)
38+
39+
$runspace = [RunspaceFactory]::CreateRunspace()
40+
$runspace.Open()
41+
42+
$runspace.SessionStateProxy.SetVariable("SharedData", $global:sharedData)
43+
$runspace.SessionStateProxy.SetVariable("hostUI", $host.UI)
44+
$runspace.SessionStateProxy.SetVariable("Stream", $Stream);
45+
$runspace.SessionStateProxy.SetVariable("StandardErrorFlag", $StandardErrorFlag);
46+
47+
$powershell = [PowerShell]::Create().AddScript({
48+
try
49+
{
50+
$sbData = New-Object System.Text.StringBuilder
51+
52+
while ($true)
53+
{
54+
$null = $sbData.Append([char]$Stream.BaseStream.ReadByte())
55+
56+
if ((-not $StandardErrorFlag) -and (([Environment]::TickCount - $SharedData.StdErrTick) -le 150))
57+
{
58+
Start-Sleep -Milliseconds 100
59+
60+
continue
61+
}
62+
63+
if ($StandardErrorFlag)
64+
{
65+
$SharedData.StdErrTick = [Environment]::TickCount
66+
}
67+
68+
$hostUI.Write($sbData.ToString())
69+
$sbData.Clear()
70+
}
71+
}
72+
catch {
73+
break
74+
}
75+
})
76+
77+
$powershell.Runspace = $runspace
78+
79+
$null = $powershell.BeginInvoke()
80+
81+
return $runspace
82+
}
83+
84+
function Invoke-RunAs
85+
{
86+
param (
87+
[Parameter(Mandatory=$true)]
88+
[string] $Username,
89+
90+
[Parameter(Mandatory=$true)]
91+
[string] $Password,
92+
93+
[string] $Application = "powershell.exe",
94+
[string] $Argument = "",
95+
[string] $Domain = "",
96+
[switch] $Detach
97+
)
98+
99+
$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
100+
101+
$startInfo = New-Object System.Diagnostics.ProcessStartInfo
102+
$startInfo.FileName = $Application
103+
$startInfo.WorkingDirectory = $env:SystemRoot
104+
$startInfo.UseShellExecute = $false
105+
$startInfo.Domain = $Domain
106+
$startInfo.Username = $Username
107+
$startInfo.Password = $securePassword
108+
$startInfo.Arguments = $Argument
109+
110+
if (-not $Detach)
111+
{
112+
$startInfo.RedirectStandardError = $true
113+
$startInfo.RedirectStandardOutput = $true
114+
$startInfo.RedirectStandardInput = $true
115+
$startInfo.CreateNoWindow = $true
116+
}
117+
118+
$proc = New-Object System.Diagnostics.Process
119+
120+
$proc.StartInfo = $startInfo
121+
122+
$null = $proc.Start()
123+
124+
if (-not $Detach)
125+
{
126+
$runspaces = @()
127+
try
128+
{
129+
$runspaces += Add-StdHandler -Stream $proc.StandardError -StandardErrorFlag $true
130+
$runspaces += Add-StdHandler -Stream $proc.StandardOutput
131+
132+
while (-not $proc.HasExited)
133+
{
134+
$userInput = Read-Host
135+
136+
$proc.StandardInput.WriteLine($userInput)
137+
138+
if ($userInput.ToLower() -eq "exit")
139+
{
140+
break
141+
}
142+
}
143+
}
144+
finally
145+
{
146+
foreach ($runspace in $runspaces)
147+
{
148+
$runspace.Close()
149+
$runspace.Dispose()
150+
}
151+
}
152+
}
153+
}
154+
155+
try {
156+
Export-ModuleMember -Function Invoke-RunAs
157+
} catch {}

0 commit comments

Comments
 (0)