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.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
ITEngineer
Posts: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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: 216
Last visit: Thu Mar 23, 2023 5:45 pm
Has voted: 4 times

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

Post by ITEngineer »

jvierra wrote: Thu Feb 07, 2019 11:24 pm "Second hop restriction" You cannot access a third computer from a remote session.
OK, so how to make it working?
/* IT Engineer */
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

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

Post by jvierra »

Use CredSSP which has to be configured on both ends.
This topic is 5 years and 1 month old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked