invoke command on remote machine is not working using powershell

4

I ran the below commands on my machine to download data from one server to another server using the invoke command

Enable-PSRemoting -force 

Enter-PSSession Server1

invoke-command -computername Server1 -credential:'dom\jack' {c:\temp.ps1 -server serverX -id 4231e429-d238-4e32-a1bb-0ee812cd3124 -download $true}

ERROR is: Failed: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

but when i run the above command on my machine as

c:\temp.ps1 -server serverX -id 4231e429-d238-4e32-a1bb-0ee812cd3124 -download $true

it works as expected.

Is there something i am missing when i execute it remotely....please help me.

thanks

powershell
asked on Stack Overflow Sep 22, 2011 by Praveen Jakkaraju • edited Sep 22, 2011 by manojlds

3 Answers

1

Try this good References:
http://www.ravichaganti.com/blog/?p=1108
http://technet.microsoft.com/en-us/magazine/ff700227.aspx

It might be something to do with the TrustedHosts or Authentication setting of a client. You can set it like this:WinRM set winrm/config/client @{TrustedHosts="*"}

Read more about this here: http://blogs.dirteam.com/blogs/sanderberkouwer/archive/2008/02/23/remotely-managing-your-server-core-using-winrm-and-winrs.aspx

I use

powershell.exe -ExecutionPolicy Unrestricted -WindowStyle Hidden -NoLogo

I use this code:

try
{

    Invoke-Command -credential $testCred -computer $ServerName -scriptblock { 
        param([String]$scriptDeploy, [String]$destino)  &"$scriptDeploy" 'parametro1' $destino
        $ScriptBlockOutput = $Error
    } -ArgumentList $RutaRemotaParaScriptDeInstalacion, "$dirRemotoDestino" 

    "`r`n`r`nOK para script de despliegue"
    exit 0;

}
catch
{
    "`r`n`r`nError en script de despliegue"
    "`r`nError in " + $_.InvocationInfo.ScriptName + " at line: " + $_.InvocationInfo.ScriptLineNumber + ", offset: " + $_.InvocationInfo.OffsetInLine + ".";

    exit -1
}
answered on Stack Overflow Jun 20, 2012 by Kiquenet
0

You need to enable remoting on the remote machine. You also need to make sure the firewall/anti virus does not block the remoting ports. These are port 5985 for http, or port 5986 for https.

If both machines on the same domain it's fairly easy to get working. If the machines are on different domains however then it's more complex. There's a registry setting that needs to be changed on the remote server, and you need to pass credentials. Have a read here for more info. There is of course ssl which can also be enabled, but that's another story.

answered on Stack Overflow Sep 22, 2011 by TheCodeKing
0

There is a bug in your script.

You should not be executing Enter-PSSession before the Invoke-Command, because the Invoke-Command itself sets up the PSSession. Use only this:

Invoke-command -computername Server1 -credential:'dom\jack' {c:\temp.ps1 -server serverX -id 4231e429-d238-4e32-a1bb-0ee812cd3124 -download $true}

... Without the Enter-PSSession

answered on Stack Overflow Oct 10, 2019 by John Sivertsen

User contributions licensed under CC BY-SA 3.0