is it posible to run a powershell script i.e. powershell.exe c:\script1.ps1 but when is launches it runs the script under the credentials of a different user (AD User)
thing is I do not want the user who runs the script to be promoted for a password for this user credentials the script will run under. I not too concerned if the password is in clear text on the command line or similar
Once you have the password ready for use in a credential object, you can do a number of things, such as call Start-Process to launch PowerShell.exe, specifying the credential in the -Credential parameter, or Invoke-Command to invoke a "remote" command locally, specifying the credential in the -Credential parameter, or you could call Start-Job to do the work as a background job, passing the credentials you want into the -Credential parameter.
Hopefully that should be enough to get you started. Let us know if you need more assistance.
I changed the username for the username I want to use and I change the password to the password of the user. I left the rest as is. saved as script.ps1 went into powershell console and ran ./script.ps1 it ran with no errors.
I am assuming a new enrity (for want of a beter word) called $cred has been created holding the username and password?
The thing is how to I now utilize this for example if I have a .cmd file called runthis.cmd which contains
Now the above script100.ps1 needs to run with higher credentials (as above), how do I achieve this with the -Credential command?
for example from within powershell console (as a test) i tried ./script100.ps1 -Credential $Cred
However I was prompted for the username and password
Sorry to be so dumb, I guess if you are scripters at heart is straightforward.
Can you please step me through the process, Thank you very much Ernie
hello I figured it out and got it working, unfortunately it appears I cannot use ConvertTo-SecureString logged in as someone else i.e. only the user that created the password file can covert if back again.
I will take a look at the other option mentioned, thank again, any more advise most welcome Ernie
If you follow these steps, you should get back the name of the user you specified in $username. That means you are executing the Get-UserName.ps1 script file in the context of the other user, which is what you need. To do this from a PowerShell.exe command line, you could have a second file that creates the credential and invokes the first file and then use that second file in your PowerShell command line.
" That means you are executing the Get-UserName.ps1 script file in the context of the other user, which is what you need. To do this from a PowerShell.exe command line, you could have a second file that creates the credential and invokes the first file and then use that second file in your PowerShell command line."