Pregunta

I have a powershell script (test.ps1) residing on ServerA. All the script is supposed to do is check whether a folder exists on the server. I am executing the script from a client (within same network)

test.ps1

test-path E:\automation
# returns whether this folder exists on ServerA

I am calling that script from ComputerB's powershell.

PS > pushd \\serverA\scripts
PS > .\test.ps1

Output:

False

Expected Output:

True

Problem:

The script is trying to locate the folder on the "local system" (ComputerB) rather than ServerA
¿Fue útil?

Solución

Have you tried powershell remoting:

Enter-PSSession -ComputerName ServerA

This will allow powershell commands to be run on ServerA

Otros consejos

This should work:

invoke-command \\serverA\scripts\test.ps1 -computername serverB

You can do this with PSSession. My working code:

$ip = "<server ip / dns name>"
$user = "<your user name>"
$s = New-PSSession -ComputerName $ip -Credential $user

$serverTestFolder = "c:\"

if (Invoke-Command -Session $s -ScriptBlock {Test-Path -path $args[0]} -ArgumentList $serverTestFolder) {
    # here you are when folder exist on remote server
}

Important here is that you need to pass variable $serverTestFolder as argument via ArgumentList and then consume it via $args ([0] is position of argument within ArgumentList).

Test-Path "filesystem::\\\Srv\share"
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top