0

Assume I am running my machine as mydomain\myuser and I need to run some tools that require auth against someone elses domain. I do the following

runas /user:theirdomain\theiruser /netonly powershell.exe

Then in any powershell commands that I run in that powershell window I need to detect the theirdomain\theiruser that I ran with.

This was discussed here but for .net and there wasn't any solutions.

The implication was that you could run something remoted somewhere and be able to then ask that remote server what user they are using. I don't have any powershell remoting knowledge but lets assume that I have a Powershell running server somewhere that I could run a remote command against - could I use that to capture the NetOnly username?

in the meantime I think I will try to pass the username separately to the environment somehow but there must be a more elegant solution for this?

Thanks for any thoughts!

Brett
  • 719
  • 1
  • 7
  • 19

1 Answers1

0

I recommend using the following:

Invoke-Command -ScriptBlock {Get-Process} -ComputerName $computerName -Credential (Get-Credential)

or you can use the following:

Enter-PSSession -Credential (Get-Credential) -ComputerName $computerName

If you prefer to run individual commands by hand, with those credentials:

Also, keep in mind Get-credential is not the only way to create a PSCredential, refer to Microsoft documentation for more examples.

Roque Sosa
  • 573
  • 4
  • 21
  • Thanks, although when I try "Invoke-Command -ScriptBlock {[Environment]::UserName} -ComputerName $computername" I get an error "The user name or password is incorrect". I don't want to provide Credentials as I already did that with RunAs /netonly and defeats the point – Brett Apr 05 '20 at 22:17
  • oh OK, so if you are already running that session of PS with the desired credentials, you don't need to add the -Credential parameter. – Roque Sosa Apr 05 '20 at 22:22
  • Yes, I'm not. I don't understand why am I getting username or password incorrect when I can login to that machine with the same login and password? – Brett Apr 06 '20 at 08:05
  • could you edit you question with the command that you are running and show the error? – Roque Sosa Apr 06 '20 at 13:38