Run New PowerShell Process As A Different User

Hello,
I’m trying to create a PowerShell script that will run a new PowerShell Process as a different ‘Windows’ user. The following code seems to work for running the new process as the different user, but I cannot understand how to send a command to the new process. I would like to be able to spawn the new process as a different user and pass some sql commands to it.

cls
$HostName = ‘SQLInstance’
$UserName = ‘DOMAIN\DifferentUser’
$PlainPassword = ‘StrongPassword’
$SecurePassword = $PlainPassword | ConvertTo-SecureString -AsPlainText -Force
$Credentials = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName, $SecurePassword

$MyUserName = $Credentials.GetNetworkCredential().UserName
$MyDomain = $Credentials.GetNetworkCredential().Domain
#$PlainPassword = $Credentials.GetNetworkCredential().Password

$abc = $args
$startInfo = $NULL
$process = $NULL
$standardOut = $NULL

$MyCommand = “Invoke-Sqlcmd -query ““SELECT ServerProperty(‘ComputerNamePhysicalNetBIOS’)”” -ServerInstance ““SQLInstance”” -Database ““DatabaseName”””
#$MyCommand = $env:computername

$startInfo = New-Object System.Diagnostics.ProcessStartInfo
$startInfo.FileName = “powershell.exe”
$startInfo.Arguments = ($MyCommand)

$startInfo.RedirectStandardOutput = $true
$startInfo.UseShellExecute = $false
$startInfo.CreateNoWindow = $false
$startInfo.Domain = $MyDomain
$startInfo.Username = $MyUserName
$startInfo.Password = $SecurePassword

$process = New-Object System.Diagnostics.Process
$process.StartInfo = $startInfo
$process.Start() | Out-Null
$standardOut = $process.StandardOutput.ReadToEnd()
$process.WaitForExit()

$standardOut

###################################

The code above is what I used and modified from the following article:
https://stackoverflow.com/questions/18540505/processstartinfo-and-process-in-powershell-authentication-error

Why do you want to use the New-Object System.Diagnostics.ProcessStartInfo object?

Did you try Invoke-Command?

$Command = {Invoke-Sqlcmd -query "SELECT ServerProperty('ComputerNamePhysicalNetBIOS')" -ServerInstance "SQLInstance" -Database "DatabaseName"}
Invoke-Command -ComputerName LocalHost -ScriptBlock $MyCommand -Credential (Get-Credential)

If you are doing this in the consolehost, ISE or VSCode, why are you not just using the built-in Start-Process cmdlet?

Another Q&A on the topic from your same source where a few responses are given.
https://stackoverflow.com/questions/20642502/how-do-i-start-a-new-powershell-instance-and-run-commands-in-it

Thank you for the replies!
Invoke-command doesn’t work for me because remote is disabled.

and the link from Postanote would work if I could just get it to launch the code as a different user as opposed to elevated access.

I changed the verb to ‘runasuser’ then added

$newProcess.username =

$newProcess.domain =

$newProcess.password =

$newProcess.UseShellExecute = $false

 

doesn’t work. Not sure I understand how it works well enough to change to runasuser

runas is pretty simple