Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 5264
  • Last Modified:

Running Remote Powershell script

I have a script on my local machine and want to run it on a remote server:
I run this command in PowerShell ISE:

Invoke-Command -ComputerName it-xyztest -filepath C:\Scripts\XYZScript.ps1

When I run this I get the error message:
[it-xyztest] Connecting to remote server it-xyztest failed with the following error
message : Access is denied. For more information, see the about_Remote_Troubleshooting
Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken


I know nothing about the remote machine.

When I try Enter-PSSession -ComputerName it-xyztest
To test connectivity I get this:

Enter-PSSession : Connecting to remote server it-mgmt01 failed with the following error
message : The client cannot connect to the destination specified in the request. Verify
that the service on the destination is running and is accepting requests. Consult the logs
and documentation for the WS-Management service running on the destination, most commonly
IIS or WinRM. If the destination is the WinRM service, run the following command on the
destination to analyze and configure the WinRM service: "winrm quickconfig". For more
information, see the about_Remote_Troubleshooting Help topic.
At line:1 char:1
+ Enter-PSSession -ComputerName it-xyztest


This is what is in my script:
$args = '/i "\\xyz.com\gpo$\TESTInventory\TEST Inventory.msi" /quiet /norestart'
[diagnostics.process]::start("msiexec.exe", $args).WaitForExit()


All I want to do is run this .ps1 from my desktop to a remote server.

What do I have to do?  Help I'm stuck.

Thx
0
techgenious
Asked:
techgenious
  • 3
  • 2
2 Solutions
 
Adam BrownSr Solutions ArchitectCommented:
You have to configure the remote server to accept remote powershell commands before you can use remote powershell on it. Running winrm quickconfig on the remote server will do this for you. If you don't have RDP access to the remote server, there are a couple different ways to get this set up remotely. One is to use psexec to run the command on the remote server. Another is to deploy a GPO to the server that enables it.
0
 
techgeniousAuthor Commented:
Now I was advised I do not have to run winrm quickconfig on the remote machine to run scripts to remote machines.

Then the only option your telling me is if I do not run:  winrm quickconfig on all my remote machines, I cannot run scripts remotely?

I just ran winrm quickconfig on the remote machine:

OUTPUT:
WinRM already is set up to receive requests on this machine.
WinRM already is set up to remote management on this machine.

I then ran my script:

Invoke-Command -ComputerName it-XYZtest -filepath C:\Scripts\XYZScript.ps1

This is what I get:
[it-XYZtest] Connecting to remote server it-XYZtest failed with the following error
message : Access is denied. For more information, see the about_Remote_Troubleshooting
Help topic.
    + CategoryInfo          : OpenError: (:) [], PSRemotingTransportException
    + FullyQualifiedErrorId : PSSessionStateBroken



Thx
0
 
Adam BrownSr Solutions ArchitectCommented:
In order to connect to the remote computer's Powershell session or use the invoke-command cmdlet against a remote machine, it has to be configured to accept remote management requests. winrm quickconfig does this for you. You don't have to run winrm quickconfig to do it, as there are other techniques, but it is necessary to configure the remote computer to accept requests for remoting. If you don't, remote requests are blocked by the firewall and no users are given permissions to perform remote actions against the server. You also need to configure the computer to accept remote scripts by using the set-executionpolicy cmdlet to allow remote scripts.

Also make sure that you are running the powershell session as an administrator. You may also need to enter credentials in the invoke-command cmdlet by using the -credentials switch. You can store credentials with $cred=get-credential and then passing $cred to invoke-command.
0
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
techgeniousAuthor Commented:
Thx excellent explanation.

Let me just recap since me big time beginner.   In order to run scripts remotely I need configured on remote machines.

winrm quickconfig
set-executionpolicy

Both of these can be done via GPO?
0
 
Sherif_TalaatCommented:
First, I'd like to mention that there is an equivalent command to "winrm quickconfig" in Windows PowerShell, this cmdlet is "Set-WSMANQuickConfig" or "Enable-PSRemoting" both cmdlet are doing the same function, and it will be nice if you use "-force" with this cmdlets.

Second, There is a group policy template for Windows PowerShell allows you to configure Execution Policies using gpo. You can download it from
http://www.microsoft.com/download/en/details.aspx?DisplayLang=en&id=25119

Third, WinRM group policy fall under
Computer Configuration\Policies\Administrative Templates\Windows Components\Windows Remote Management

Fourth, you can use logon scripts (VBscript or PowerShell) to execute whatever you want. So, if WinRM gpo options are not enough, you create a script that contains (winr quickconfig or Enable-PSRemoting) and assign it to your domain.
0
 
techgeniousAuthor Commented:
excellent response
0

Featured Post

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now