Hi DHoss-6479,
You can tie a credential to a PowerShell session configuration and reuse the configuration for all future connections.
For this example, we will work with a server named SRV1 and create a new session configuration on this machine using the Register-PSSessionConfiguration cmdlet. The command below creates a session called Demo and uses the RunAsCredential parameter to run the session.
Invoke-Command -ComputerName SRV1 -ScriptBlock { Register-PSSessionConfiguration -Name Demo -RunAsCredential 'domain\mydomainaccount' -Force }
This command creates a new session configuration on the remote computer and, when connected, forces it to always run with the credential provided.
Next, specify the configuration with the ConfigurationName parameter when running Invoke-Command. Use the same command as above, but with the Demo configuration. Use this session configuration the next time you run a command on a remote computer that connects to a third computer.
Invoke-Command -ComputerName 'SRV1' -ScriptBlock { Get-ChildItem -Path \SRV2\c$ } -ConfigurationName Demo
Directory: \\SRV1\c$
Mode LastWriteTime Length Name PSComputerName
---- ------------- ------ ---- --------------
d----- 11/30/2016 11:35 AM Program Files SRV1
d----- 5/25/2017 11:32 AM Windows SRV1
<snip>
Instead of an access denied message, the command runs as expected. Just use the ConfigurationName parameter each time you use Invoke-Command or Enter-PSSession.
Now that we've solved this PowerShell remoting issue, you can make it work in a more streamlined fashion. With the $PSDefaultParameterValues automatic variable, you can avoid using the ConfigurationName parameter each time. You can program PowerShell to use a certain parameter when using a specific command.
Use the ConfigurationName parameter and specify the value of the session Demo every time you call Invoke-Command. To do that, create the $PSDefaultParameterValues hash table and assign it a key of Invoke-Command:ConfigurationName and a value of Demo as shown below.
$PSDefaultParameterValues = @{'Invoke-Command:ConfigurationName'='Demo' }
All these techniques should help you work more efficiently when using PowerShell to work with remote machines.
---------------------------------------------------------------------------------------------------------------------------------------------------------
--If the reply is helpful, please Upvote and Accept as answer--