Solved

Running Remote Powershell script

Posted on 2012-03-21
6
4,106 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
  • 3
  • 2
6 Comments
 
LVL 38

Accepted Solution

by:
Adam Brown earned 100 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 38

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
Too many email signature updates to deal with?

Do you feel like you are taking up all of your time constantly visiting users’ desks to make changes to email signatures? Wish you could manage all signatures from one central location, easily design them and deploy them quickly to users? Well, there is an easy way!

 

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 100 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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Suggested Solutions

You might have come across a situation when you have Exchange 2013 server in two different sites (Production and DR). After adding the Database copy in ECP console it displays Database copy status unknown for the DR exchange server. Issue is strange…
I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
This tutorial will walk an individual through the steps necessary to join and promote the first Windows Server 2012 domain controller into an Active Directory environment running on Windows Server 2008. Determine the location of the FSMO roles by lo…
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…

707 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now