We help IT Professionals succeed at work.

Executing Powershell scripts remotely i.e. from another server

rakkad
rakkad asked
on
58 Views
Last Modified: 2017-12-05
Hi

I need to run a powershell script that calls a script remotely..

I have written the following batch script:-

del \\bwbia801\c$\cogtest\cognos_burstemail_TaskOutput.txt

powershell invoke-command -ComputerName bwbia801 -FilePath c:\cogtest\cognos-burstreports.ps1 >> \\bwbia801\c$\cogtest\cognos_burstemail_TaskOutput.txt 2>&1

but the output log details the following error:-

invoke-command : File C:\cogtest\cognos-burstreports.ps1 cannot be loaded because running scripts is disabled on this system. For more information,
see about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
At line:1 char:1
+ invoke-command -ComputerName bwbia801 -FilePath c:\cogtest\cognos-burstreports.p ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : SecurityError: (:) [Invoke-Command], PSSecurityException
    + FullyQualifiedErrorId : UnauthorizedAccess,Microsoft.PowerShell.Commands.InvokeCommandCommand

Anyone help ?

Thanks
Comment
Watch Question

PowerShell Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer
CERTIFIED EXPERT

Commented:
Not sure if the issue is the local or remote PS Session, going to assume it's the remote session, in which case try this:


Actually based off the error I think the issue is on the local machine, so this would get around that:
@(
	ECHO OFF
	SET "_RemoteSystem=bwbia801"
	SET "_PSCMD_FullPath=c:\cogtest\cognos-burstreports.ps1"
	SET "_Logfile_SharePath=c$\cogtest\cognos_burstemail_TaskOutput.txt"
	SET "_eLvl=0"
)

CALL :Main

(
	Exit /b %_eLvl%
)

:Main
	IF EXIST "\\%_RemoteSystem%\%_Logfile_SharePath%" (
		DEL "\\%_RemoteSystem%\%_Logfile_SharePath%"
	)

	powershell.exe -executionpolicy unrestricted -command "&{ invoke-command -ComputerName %_RemoteSystem% -ScriptBlock -FilePath %_PSCMD_FullPath%}" >> "\\%_RemoteSystem%\%_Logfile_SharePath%" 2>&1

GOTO :EOF

Open in new window


Seems PS run against remote systems don't suffer from UAC issues according to a TechNet article I was just reading.

Author

Commented:
Thanks for getting back to me much appreciated.

I re-ran your script but get the following error:-

Invoke-Command : Missing an argument for parameter 'ScriptBlock'. Specify a
parameter of type 'System.Management.Automation.ScriptBlock' and try again.
At line:1 char:42
+ &{ invoke-command -ComputerName bwbia801 -ScriptBlock -FilePath
c:\cogtest\cogno ...
+                                          ~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Invoke-Command], Parameter
   BindingException
    + FullyQualifiedErrorId : MissingArgument,Microsoft.PowerShell.Commands.In
   vokeCommandCommand
Ben Personick (Previously QCubed)Lead SaaS Infrastructure Engineer
CERTIFIED EXPERT

Commented:
looks like the iossue is the errant "Script-block" typo.

See if this resolves the issue for you.

REM Script: CallRemote.cmd
REM Version: 1.0.1
@(
	ECHO OFF
	SET "_RemoteSystem=bwbia801"
	SET "_PSCMD_FullPath=c:\cogtest\cognos-burstreports.ps1"
	SET "_Logfile_SharePath=c$\cogtest\cognos_burstemail_TaskOutput.txt"
	SET "_eLvl=0"
)

CALL :Main

(
	Exit /b %_eLvl%
)

:Main
	IF EXIST "\\%_RemoteSystem%\%_Logfile_SharePath%" (
		DEL "\\%_RemoteSystem%\%_Logfile_SharePath%"
	)

	powershell.exe -executionpolicy unrestricted -command "&{ invoke-command -ComputerName %_RemoteSystem% -FilePath %_PSCMD_FullPath%}" >> "\\%_RemoteSystem%\%_Logfile_SharePath%" 2>&1

GOTO :EOF

Open in new window

Jason CrawfordTransport Ninja
CERTIFIED EXPERT

Commented:
No comment has been added to this question in more than 21 days, so it is now classified as abandoned.

I have recommended this question be closed as follows:

Accept: Chris Dent (https:#a42110314)

If you feel this question should be closed differently, post an objection and the moderators will review all objections and close it as they feel fit. If no one objects, this question will be closed automatically the way described above.

exchangepro
Experts-Exchange Cleanup Volunteer

Gain unlimited access to on-demand training courses with an Experts Exchange subscription.

Get Access
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Empower Your Career
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE

Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.