Intune – Run Powershell Scripts Directly from Github

As I explore more and more of how to use Intune in environments one of the things I do is run PowerShell scripts. I love this feature because it allows me to do even more. I was working with another Engineer on running scripts and we setup a script to install our remote control software we use on our MSP customers. We were talking about larger scale about how we do this currently and how we could use PowerShell scripts to install the software. The one item I think about is maintenance, how are we going to do this at scale for multiple customers? Enter GitHub, I will state I am not an expert at PowerShell and or GitHub and I will give 100% credit to this site for pointing me in the right direction. Some may look at this and say you can do this better or what ever, which I could not agree, but for me this at scale could be awesome. Enough of that, lets get into this.

In this post I do not intend to show you the process to upload the script into Intune but just show you in general what I came up with for a process.

First you need your GitHub repository. I did test this with both Private and Public repository and they both worked. I do prefer to do private and this demo will be using a private repository.

Now that you created your private repository, and lets create a simple PowerShell script.

Copy-Item -Path "C:\temp\apps.txt" -Destination "c:\temp\copy\"

Again just a simple copy file command here as an example

Next you need switch to the RAW view to get the full URL. I have changed the URL so it does not work but will look something like below. If you were not using a private it would not have the token at the end.

https://raw.githubusercontent.com/edtechjeff/scriptsintune/main/copyfile.ps1?token=xdCZFUPRqUHbvYKrpCuKWkLFGETdBwCUDkUePDhk

The last piece you need is the PowerShell script that you will import into Intune. The script you see below just has a couple of PowerShell commands.

$ScriptFromGitHub = Invoke-WebRequest
Invoke-Expression $($ScriptFromGitHub.Content)

Here is the command fully populated. You will notice that the URL that you put in is the one you took note of from the RAW view.

$ScriptFromGitHub = Invoke-WebRequest https://raw.githubusercontent.com/edtechjeff/scriptsintune/main/copyfile.ps1?token=xdCZFUPRqUHbvYKrpCuKWkLFGETdBwCUDkUePDhk
Invoke-Expression $($ScriptFromGitHub.Content)

Now you can import the PowerShell script into Intune and you are done. My long term goal is to use this method when onboarding our new customers by utilizing Intune if they have it setup. Other thoughts could be to have a larger script that runs and sets up the environment to what the customers wants. I hope this might help you in the future and as always if you have any suggestions or comments feel free to reach out. Have a great day.

Leave a comment