Explore remote C drive

DHoss 61 Reputation points
2022-06-09T19:30:10.87+00:00

Hi Guys
I am here again and asking a questions. I have a powershell script that I run with admin rights and in that script I connect to remote machine and get system information. Now if I want to access the C drive on the remote machine it ask me to authenticate again. Is there any way to bypass this 2nd authentication process by any means?
Any help would be much appreciated. I run the Powershell script from a batch file where I authenticate.. batch file like this...

@echo off  
PowerShell.exe -executionpolicy bypass -Command "start-process 'powershell.exe' '-Command "".\Scripts.ps1"' -WindowStyle Hidden -credential  "org\userID" -passthru"  
exit/B  
Windows for business | Windows Server | User experience | PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,931 Reputation points
    2022-06-13T07:20:40.87+00:00

    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--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.