There are a few ways to run a program or script as another user from within a script:
The built-in command line application RUNAS
The Windows Command Line RUNAS
command would look like a good solution to your problem if you were able to specify the credentials.
RUNAS /user:user@domain.microsoft.com "powershell pshell.ps1"
As you have said, however, you need to run this task from CONTROL-M, so this is not possible. It is also then not possible to run the task from Task Scheduler as I suggested in a comment and another answer has suggested.
Therefore, my next suggestion is to use a PowerShell script to do this:
The PowerShell scriptlet Invoke-Command
Firstly, you need to enable Win-RM to allow this to work. To do so, type the following in an elevated (i.e. run as administrator) command prompt:
winrm /quickconfig
Next, write a script with the stored credentials you want. Note that this will be stored as plain text, so if you are not willing to do that you will need to look at using a secure credential file.
$username = "DOMAIN\User"
$password = "Password"
$secstr = New-Object -TypeName System.Security.SecureString
$password.ToCharArray() | ForEach-Object {$secstr.AppendChar($_)}
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $username, $secstr
Invoke-Command -FilePath "C:\Script\To\Execute.ps1" -Credential $cred -Computer localhost
Naturally, you need to replace C:\Script\To\Execute.ps1
with the file path to your PowerShell script you want to run, and replace DOMAIN\User
and Password
with the user you want to run as and their password, respectively.
This script will now run as the user specified above.
However, you may be unwilling or unable to use a PowerShell script, and so your last solution rests in a third party application, such as:
SysInternals PsExec
PsExec is a completely free tool offered for download on TechNet specifically designed for running commands, applications, etc. on remote computers. It works perfectly well on the local machine and believe it or not, allows you to specify the specific user (and password!) you want the application to run with.
- Download and extract the application
- Put the application somewhere in your
PATH
attribute (SET PATH=C:\PsExec;%PATH%
works, if you installed it to C:\PsExec)
- Run the command
psexec -u DOMAIN\user -p password script.ps1
with the appropriate changes.