Powershell – Set credentials/password for remote connection in powershell on Windows Server 2012

powershellwindows-server-2012-r2

I have 2 servers (both Windows Server 2012 R2). They both have an Administrator account with password xxx and the 2 servers are in the same network (domain). I didn't install/configure those servers.

I'm able to execute powershell commands from server 1:

Invoke-Command -ComputerName server01 -ScriptBlock {Get-Culture}

I can also use this command

Invoke-Command -ComputerName server01 -Credential Administrator -ScriptBlock {Get-Culture}

There pops up a windows and I have to fill in my password.
I want that only the option with Credential/password is allowed and only when this connection comes from server02.

How do I have to achieve this in powershell?

Best Answer

You need to create a credential object with the Get-Credential cmdlet, store it in a variable, and use it with your Invoke-Command.

$Cred = Get-Credential
Invoke-Command -Credential $Cred
Related Topic