0

I have a script block that I'm trying to make it run as a different domain user.

$Username = 'domain\test'
$Password = '1234'
$pass = ConvertTo-SecureString -AsPlainText $Password -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $UserName,$pass

Invoke-Command -ScriptBlock{
write-host "hello"

} -Credential $cred -ComputerName $env:COMPUTERNAME

When I run it I got the following error:

[test-pc] Connecting to remote server test-pc failed with the following error message : The client cannot connect to the destination specified in the request. Verify that the service on the destination is running and is accepting requests.
 Consult the logs and documentation for the WS-Management service running on the destination, most commonly IIS or WinRM. If the destination is the WinRM service, run the following command on the destination to analyze and configure the WinRM se
rvice: "winrm quickconfig". For more information, see the about_Remote_Troubleshooting Help topic.

Why the script is trying to authenticate locally and not against the DC ? Thanks

13
  • Is the WS-Management service running on the remote computer? If you're trying to use PowerShell remoting it needs to be enabled and the correct ports need to be open. I don't think you're even being authenticated yet. Commented Oct 12, 2018 at 14:18
  • Have you setup WinRM on the remote server to accept WinRM? Have you been through the steps in about_Remote_Troubleshooting ? learn.microsoft.com/en-us/powershell/module/… Commented Oct 12, 2018 at 14:19
  • I have a lab with a Windows 10 client and a DC (2012 R2). When I execute my script on the client , I want to use another domain account. Commented Oct 12, 2018 at 14:24
  • The error message tells you what you need to do. Go on your 2012 R2 box, open PowerShell as admin and type in winrm quickconfig Commented Oct 12, 2018 at 14:30
  • 1
    I want to execute manage-bde with a specific domain user. That is my objective. All the commands are going to be executed locally , th eonly thing that happens remotely is the authentication with the AD. Commented Oct 12, 2018 at 15:49

1 Answer 1

2

If you don't actually want to run the script remotely, you can use Start-Process to run Powershell as another user, which will then execute your command/script as that user.

(See powershell command line help for full syntax options and examples)

# Using Get-Credential to illustrate, substitute with your own credential code
$cred = Get-Credential

# Run Command:
Start-Process -FilePath Powershell -Credential $cred -ArgumentList '-Command', 'Write-Host "Hello"'

# Run Script:    
Start-Process -FilePath Powershell -Credential $cred -ArgumentList '-File', 'C:\folder\script.ps1'
Sign up to request clarification or add additional context in comments.

3 Comments

is it possible to use it with a scriptblock ?
Yes, -Command accepts a scriptblock, see the link I included as it covers this.
@Nae How nice of MS to not redirect the original url, I've now updated it.

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.