SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Run New PowerShell Process As A Different User


Run New PowerShell Process As A Different User

Author
Message
Coozie
Coozie
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 365

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


Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)

Group: General Forum Members
Points: 907017 Visits: 48690

$UserName = 'DOMAIN\DifferentUser'
$PlainPassword = 'StrongPassword'


The snippet of code above is from the code you posted. I'm no ninja at PowerShell but that code seems to imply that the user name and password will exist in clear text, which is a serious violation of all that is holy when it comes to security.

If what I've stated is correct, my recommendation would be to learn how to correctly use Active Directory for such things.


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Coozie
Coozie
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 365
Thanks for the reply, Jeff. This is just a simulation where I'm using a dummy account on a test server and was tired of typing the pwd. I can't imagine anyone actually thinking that this would be the right thing to do.

Having said that, do you have any suggestions or help on the actual topic?

Thanks,
Mark
Jeff Moden
Jeff Moden
SSC Guru
SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)SSC Guru (907K reputation)

Group: General Forum Members
Points: 907017 Visits: 48690
Coozie - Tuesday, January 15, 2019 11:19 AM
Thanks for the reply, Jeff. This is just a simulation where I'm using a dummy account on a test server and was tired of typing the pwd. I can't imagine anyone actually thinking that this would be the right thing to do.

Having said that, do you have any suggestions or help on the actual topic?

Thanks,
Mark

You said you wanted to spawn the process as a different user and then hardcoded it with no explanation. With that, I thought you were actually going to do such a thing. And, no... I can't imagine anyone actually doing such a thing and, yet, they do.

As for the actual topic, yes... I have an idea... why are you trying to get information about the computer using T-SQL. Why not just make a WMIC call? Also, why write something like this? Why not just buy an existing product the would not only identify such things but also provide you with the ability to monitor the servers for other things? Of course, you'll also take some exception to those questions because your post hasn't provided anyone with enough information to actually know what the end game here is and so I'm just guessing based on what I see in the code.

.


--Jeff Moden

RBAR is pronounced ree-bar and is a Modenism for Row-By-Agonizing-Row.
First step towards the paradigm shift of writing Set Based code:
Stop thinking about what you want to do to a row... think, instead, of what you want to do to a column.
If you think its expensive to hire a professional to do the job, wait until you hire an amateur. -- Red Adair

When you put the right degree of spin on it, the number 318 is also a glyph that describes the nature of a DBAs job. Wink

Helpful Links:
How to post code problems
How to post performance problems
Forum FAQs
Lowell
Lowell
SSC Guru
SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)SSC Guru (300K reputation)

Group: General Forum Members
Points: 300574 Visits: 42273
not sure if this helps.
Fore security reasons, i never log in as my elevated account, but as a regular account,and spawn applications with the privileged account as needed. via a simple cmd/bat file:
the advantage is i have saved my credentials the first time it runs, and just have to update when it's time to change password

for example, to start the PowerShell IDE, i have a *.cmd file with one of the various commands below in it, as examples:
Explorer++.cmd:
runas /savecred /user:mydomain\super.admin.lowell "C:\_Apps\Explorer++\Explorer++.exe"
PowerShell.cmd
runas /savecred /user:mydomain\super.admin.lowell "%windir%\system32\WindowsPowerShell\v1.0\PowerShell_ISE.exe"

Bids2008.cmd
runas /savecred /user:mydomain\super.admin.lowell "C:\Program Files (x86)\Microsoft Visual Studio 9.0\Common7\IDE\devenv.exe"

Lowell
--help us help you! If you post a question, make sure you include a CREATE TABLE... statement and INSERT INTO... statement into that table to give the volunteers here representative data. with your description of the problem, we can provide a tested, verifiable solution to your question! asking the question the right way gets you a tested answer the fastest way possible!
Sue_H
Sue_H
SSC Guru
SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)SSC Guru (79K reputation)

Group: General Forum Members
Points: 79483 Visits: 15942
Coozie - Tuesday, January 15, 2019 11:19 AM
Thanks for the reply, Jeff. This is just a simulation where I'm using a dummy account on a test server and was tired of typing the pwd. I can't imagine anyone actually thinking that this would be the right thing to do.

Having said that, do you have any suggestions or help on the actual topic?

Thanks,
Mark


I haven't had time to test this yet but I think you need to do this a little differently that using start process. Gather the credentials etc and then do New-PSSession and then invoke-sqlcmd.

Sue



Coozie
Coozie
SSC Eights!
SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)SSC Eights! (913 reputation)

Group: General Forum Members
Points: 913 Visits: 365

Summary:
I'm trying to automate a process.
In the process I create sql credentials and a windows account.
At a point in the process I have to execute code as that windows account. For example: create asymmetric key.
Then after that windows user creates the asymmetric key I have to complete some additional tasks to complete the automation.

So in a nutshell:
Execute code as me.
Execute code as a different Windows user.
Execute code as me.
Done.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum









































































































































































SQLServerCentral


Search