Running a powershell script from batch file as DIFFERENT USER (non interactively)

Hi Friends,
 I need your valuable suggestion on how I can achieve the following:

OS: Windows

In short: I want to understand how can I RUN a Powershell script from a Batch file by providing domain credentials (NON-INTERACTIVELY).

Here is my scenario in detail:

I have a java program, started by a Service, which creates a batch file on the fly which will have to invoke a powershell script during runtime and the powershell script has some cmdlets which need to RUN as DOMAIN USER, otherwise those cmdlets wont return values. Currently I am using a work around like.. I am running the service as the domain user and able to get the data from cmdlets, but when I run the service as Local System account I am not getting data from cmdlet.

Can you please suggest a better way to accomplish what i wanted.. getting data from cmdlet without having to run the service as domain user. Can I run the powershell script as DOMAIN USER from within the batch file. The program which creates the batch file has domain user credentials (username & password).

Please note that WinRM is disabled in the environment, otherwise I could use Invoke-Command with details and make things work without having to run the service as domain user.

Thanks a lot for your valuable suggestions.

Krishna VAsked:
Who is Participating?
Non-interactively is a problem.
In my opinion, running the service as a domain user is the way this should be done.

One other possibility I see is to create a scheduled task that runs with the credentials you want.  Then just have the batch file run the scheduled task via the schtasks.exe command on demand.
Kyle AbrahamsSenior .Net DeveloperCommented:

You can use psExec as an option or you could use PowerShell remoting.

$user = "username"
$pass = "password" | convertto-securestring -asplaintext -force
$cred = new-object -typename System.Management.Automation.PSCredential -argumentlist $user, $pass

<#this is a list of computers, but you can use just your local machine #>
$list = get-content C:\list.txt 

foreach($item in $list){
$session = new-pssession $item -Credential $cred
invoke-command -Session $session {desired thing here}

Open in new window

Krishna VAuthor Commented:
Thanks Kyle and footech for your suggestions. But my question is still un-answered as I cannot use powershell remoting.
Kyle AbrahamsSenior .Net DeveloperCommented:
what about ps exec?
Neither of my suggestions mentioned PS Remoting.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.