开发者

invoke command on remote machine is not working using powershell

开发者 https://www.devze.com 2023-04-06 18:53 出处:网络
I ran the below commands on my machine to download data from one server to another server using the invoke command

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: 0开发者_运维知识库x80070005 (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


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
}


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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号