Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 8583

Running PowerShell script as different user

$
0
0

Hello

I need a way to run a PowerShell script as a different user (I do not mean RunAs Administrator)

I see some examples of user Start-Process with the RunAS option but this is to RunAs Administrator as far as I can see not a specific user

I know you can use the credential object like so

$Cred = get-credential
get-service -computer remote1 -credential $cred

I have seen a couple of ways to store the get the store the $cred in a way that is not easy (at least at first glance) to work out the password, so this helps to an extent

for example within the script where I need to connect to a remote machine (as above) I can store $cred and use as above, however there are lots of places in the script where I need to user (normal user) to have more rights, therefore this would mean lots of $cred etc..

Therefore I thought about warping the whole script in an invoke-command like so

contents of myscript.ps1 below

"Starting Script at {0}" -f (get-date)

$Cred = ....code to store credential without displaying password in clear readable format...

invoke-command {

lots of power shell code ....

} -credential $cred -computername $env:computername

the thing is when I wrap too much code in the invoke-command as above PowerShell does not like it

So I wanted a way to start powershell.exe but as a specific username and password

I have seen some options for encrypting password when using DOS batch file RunAs option e.g.

RunAs /User:MyDomain\MyUser

then looking at firing off PowerShell by calling this dos batch file as it were, but this is messy (or at least I have not found a good enough dos based utility)

Any suggestions please

Thanks

AAnotherUser__


Viewing all articles
Browse latest Browse all 8583

Latest Images

Trending Articles



Latest Images