1

My scenario : Login to the remote machine via remote desktop connection and open command prompt as admin and execute some admin commands in the command prompt.

I am trying to automate the above scenario through powershell from my local machine.

Below are the steps that I have done :

  1. To check the working of my powershell script, I manually logged into the remote machine (Admin Cred) and executed this command Start-Process cmd -ArgumentList '/c cmdcommand > output.txt -Verb runas. This is working as expected in the remote machine as I am getting the desired output.
  2. Now, I tried to execute this command from the local powershell script through Invoke-Command -ComputerName computername - ScriptBlock {Start-Process cmd -ArgumentList '/c cmdcommand > output.txt -Verb runas} -Credentials $cred. I do not get any output.
  3. But, when I do try to execute the above command without runas paramter, I get the output saying this particular command needs admin credentials which is expected.Invoke-Command -ComputerName computername - ScriptBlock {Start-Process cmd -ArgumentList '/c cmdcommand > output.txt} -Credentials $cred.

Am I missing something here?

2 Answers 2

1

Execute:

 Enable-PSRemoting -Force

In an elevated prompt on your target PC. Then Restart-Service WinRM

Enter-PSSession -ComputerName $rpc -Credential (Get-Credential)
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the comment. I first invoked the windows.powershell session for the remote communication and with that session I was able to execute the remote commands as admin from my client machine.
0

If you are able to remote to Default endpoint Microsoft.PoweShell, That means the executing user is an administrator in the destination machine.

But here, I suspect the cmdcommand you specified is actually doing some execution which targets a remote resource, hence it will become a dual hope issue.

if so, below article will be a solution for you issue.

Its all about Credential Delegation

2 Comments

Thank you @Prasoon for the comment. I modified my script a little bit $session = New-PSSession -ComputerName $remoteComputer -ConfigurationName Windows.PowerShell and using this session in the invoke command solved my problem. My understanding was that, if we do not explicitly use session, the default session would windows.powershell. I have no idea how this worked. Any explanation for this?
Default endpoint is Microsoft.PowerShell, Probably in your environment, you might have an endpoint named Windows.PowerShell . To see the available endpoints, Login to the target machine and see the result of Get-PSSessionConfiguration cmdlet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.