?
Solved

Running Remote Powershell script

Posted on 2012-03-21
6
Medium Priority
?
4,790 Views
Last Modified: 2012-04-05
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
Comment
Question by:techgenious
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 42

Accepted Solution

by:
Adam Brown earned 400 total points
ID: 37748562
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
 

Author Comment

by:techgenious
ID: 37749503
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
 
LVL 42

Expert Comment

by:Adam Brown
ID: 37749524
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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 

Author Comment

by:techgenious
ID: 37750587
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
 
LVL 3

Assisted Solution

by:Sherif_Talaat
Sherif_Talaat earned 400 total points
ID: 37750690
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
 

Author Closing Comment

by:techgenious
ID: 37813121
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

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Background Information Recently I have fixed file server permission issues for one of my client. The client has 1800 users and one Windows Server 2008 R2 domain joined file server with 12 TB of data, 250+ shared folders and the folder structure i…
This article provides a convenient collection of links to Microsoft provided Security Patches for operating systems that have reached their End of Life support cycle. Included operating systems covered by this article are Windows XP,  Windows Server…
This tutorial will walk an individual through the steps necessary to configure their installation of BackupExec 2012 to use network shared disk space. Verify that the path to the shared storage is valid and that data can be written to that location:…
This tutorial will walk an individual through locating and launching the BEUtility application to properly change the service account username and\or password in situation where it may be necessary or where the password has been inadvertently change…
Suggested Courses

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question