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

Ask your PowerShell-related questions, including questions on cmdlet development!
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
ITEngineer
Posts: 210
Joined: Wed Oct 12, 2011 10:52 am

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

Post 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.
/* IT Engineer */

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

"Second hop restriction" You cannot access a third computer from a remote session.

User avatar
ITEngineer
Posts: 210
Joined: Wed Oct 12, 2011 10:52 am

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

Post 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?
/* IT Engineer */

jvierra
Posts: 14546
Joined: Tue May 22, 2007 9:57 am
Contact:

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

Post by jvierra »

Use CredSSP which has to be configured on both ends.

Locked