Jump to content

Running powershell script on remote PC

UrbanFreestyle

Good morning all. So i have been teaching myself about powershell and am amazed at how powerful it is. One thing i am missing is knowing how to run the PS1 script on a remote PC on our network. Basically i would like to be able to enter a server name and have the script run on that machine. A bit like using shutdown -i 

 

Can anyone offer some advise? The ps1 will be saved to their local machine / to one of our network drives.

 

The powershell script does not need to be invoked as admin.

Link to comment
Share on other sites

Link to post
Share on other sites

Setup IIS with ASP. Then...

If you're interested in a product please download and read the manual first.

Don't forget to tag or quote in your reply if you want me to know you've answered or have another question.

Link to comment
Share on other sites

Link to post
Share on other sites

6 hours ago, UrbanFreestyle said:

i dont need to call it from a webpage though?

You need a server to run server side code on. PowerShell is a server side language which is also .NET, IIS allows you to use .NET code although idk about powershell scripts but what he sent seems legit although not exactly recommended.

 

That being said you should do it the proper way with the correct networking settings etc. A random guide online - https://www.howtogeek.com/117192/how-to-run-powershell-commands-on-remote-computers/ (I know the guide says how to run commands but you can run a script as a command so it's the same thing basically).

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

i wouldnt say it is a 'server side' language though. i use it all the time on client end too.

i'm thinking i might just try the invoke command and see how i get on.

Link to comment
Share on other sites

Link to post
Share on other sites

Easy.

Invoke-Command -ComputerName FQDNOfTarget -FilePath \\YourNetworkDrive\Script.ps1

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 4/19/2019 at 12:11 AM, Acedia said:

Easy.


Invoke-Command -ComputerName FQDNOfTarget -FilePath \\YourNetworkDrive\Script.ps1

 

Thanks. so for example this would be:

Invoke-Command -ComputerName work1.domain.local -FilePath \\server1\script.ps1

 

 

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, UrbanFreestyle said:

Thanks. so for example this would be:

Invoke-Command -ComputerName work1.domain.local -FilePath \\server1\script.ps1

 

 

That would be the most basic example. There are many different ways of improving it tho. If you need different credentials you can feed them via -Credentials $Credentials if you store them in $credentials=Get-Credentials or from the secure store from your machine, you could set multiple computer names in an object and use that, importing them from an array or a text file or by getting them from AD itself, you could skip the FilePath alltogether and feed the Script directly by using -ScriptBlock {$Script} or -ScriptBlock {Restart-Computer} and so on.

Link to comment
Share on other sites

Link to post
Share on other sites

10 hours ago, Acedia said:

That would be the most basic example. There are many different ways of improving it tho. If you need different credentials you can feed them via -Credentials $Credentials if you store them in $credentials=Get-Credentials or from the secure store from your machine, you could set multiple computer names in an object and use that, importing them from an array or a text file or by getting them from AD itself, you could skip the FilePath alltogether and feed the Script directly by using -ScriptBlock {$Script} or -ScriptBlock {Restart-Computer} and so on.

Thanks, thankfully no creds needed. I hope that the invoke command isnt blocked.

Link to comment
Share on other sites

Link to post
Share on other sites

4 hours ago, UrbanFreestyle said:

sadly invoke is blocked ? also cant run it through a PS Session ?

 

Run this on the target

Enable-PSRemoting -Force

Enable-PSRemoting -Force

 

Link to comment
Share on other sites

Link to post
Share on other sites

there are some nice tutorials around as running scripts remote seems to be a part of powershell. depending on what you want to do you may also have a look at ansible. supports windows too and can run powershell scripts too ? 

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×