Page 1 of 1

Invoke-Command not working for executing another script using UNCPath?

Posted: Thu Feb 07, 2019 11:27 pm
by ITEngineer
Hi All People,

I need some help to execute the Batch script remotely using Invoke-Command but still failed numerous times.

I have executed the PowerShell ISE from my local workstation as Administrator and my user account is the member of Enterprise Administrator and Domain Admins group.

When I try to RDP into the system and then run the script manually \\mydomain.com\netlogon\Script\LogonScript-v4.BAT it works with no issue.

This is the script:

Code: Select all

$ADDomain = (Get-WmiObject Win32_ComputerSystem).Domain
$Computer = "SITE1WKS" + "." + $ADDomain
$session = New-PSSession -ComputerName $Computer
Invoke-Command -Session $session -ScriptBlock {
    Try {
        \\$ADDomain\netlogon\Script\LogonScript-v4.BAT
    }
    Catch {
        Write-Error $_
    }    
}
Remove-PSSession $session
This is the error code:

Code: Select all

Access is denied
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException
When I run with just one simple line below:

Code: Select all

Invoke-Command -ComputerName 'SITE1WKS' -ScriptBlock {\\mydomain.com\netlogon\Script\LogonScript-v4.BAT}
It is still failed with the error:

Code: Select all

An error occurred while creating the pipeline.
    + CategoryInfo          : NotSpecified: (:) [], RuntimeException
    + FullyQualifiedErrorId : RuntimeException
    + PSComputerName        : SITE1WKS
I wonder if this is even possible to achieve or not?

Thank you in advance.

Re: Invoke-Command not working for executing another script using UNCPath?

Posted: Fri Feb 08, 2019 12:24 am
by jvierra
"Second hop restriction" You cannot access a third computer from a remote session.

Re: Invoke-Command not working for executing another script using UNCPath?

Posted: Sun Feb 10, 2019 4:56 pm
by ITEngineer
jvierra wrote:
Fri Feb 08, 2019 12:24 am
"Second hop restriction" You cannot access a third computer from a remote session.
OK, so how to make it working?

Re: Invoke-Command not working for executing another script using UNCPath?

Posted: Sun Feb 10, 2019 6:11 pm
by jvierra
Use CredSSP which has to be configured on both ends.