πŸ†•
PSCloudScript Basics
I'm going to write a few posts about this topic, so it is probably best to start with the basics. So what is a PSCloudScript? It's what I call a PowerShell script that you run off the internet
​Recast Software gets mad props for supporting Community content like this. Make sure you check our their Systems Management Tools
Home
Recast Software
Sponsored by Recast Software

GitHub gist

The first thing to do is to get your PowerShell script in the cloud, and you can do this easily by creating a GitHub Gist
Discover gists
Gist
It really doesn't matter if you save it as a Public or a Private gist as it's still accessable by anyone with the link

Visual Studio Code Extensions

There are several Visual Studio Code extensions that can help you manage your GitHub gists, but one that really stands out is GistPad. It will be much easier to edit in VSCode with this and the PowerShell extension. You can add this from the Marketplace at this link
https://marketplace.visualstudio.com/items?itemName=vsls-contrib.gistfs
marketplace.visualstudio.com
You will need to sign into your GitHub account and authorize Visual Studio Code to access GitHub
This script that I'm working on will be used with OSDCloud. Once you are done with all your script edits, save it back to GitHub gists

Getting the right URL

Its important to get the right Url to link for a PSCloudScript

Gist Editor Share URL

The GitHub Gist Editor and the Share link are identical, but the problem is that this URL doesn't have an easy way to get the PowerShell Script out
1
Gist Editor and Share URL Format
2
https://gist.github.com/<GitHubUser>/<Gist>
3
​
4
Gist Editor and Share URL
5
https://gist.github.com/OSDeploy/30dc8839e8972493be1a343d509f423f
Copied!

Gist Raw Commit URL

When you press the Raw button on the Gist Editor Share Link, you are redirected to the Raw Commit Link
You will get a URL returned that adds a second set of numbers. This second set is known as the Commit, so if you share this URL, then you are sharing only this version of your script, so any future changes will not be reflected in this URL because this content will be static going forward
1
Gist Raw URL Format
2
<Site>/<GitHubUser>/<Gist>/raw/<Commit>/<ScriptName>
3
​
4
Gist Raw URL
5
https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/8fdf485273d0d69d6b54c624e758c9b90f6b5bce/live.osdcloud.com.ps1
Copied!

Gist Raw URL (Latest)

To ensure you always have the latest content for your script, you need to remove the Commit from the URL, so you will need to format your URL like this example. This is the URL you need to use for your PSCloudScript
1
Gist Raw URL Format
2
<Site>/<GitHubUser>/<Gist>/raw/<ScriptName>
3
​
4
Gist Raw URL
5
https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1
Copied!

WebRequest vs RestMethod

Invoke-WebRequest

Once the Gist is published, and you have your real Gist Raw link, you could use the following command to get the Gist Content. This command can be abbreviated as well
1
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'
2
​
3
(Invoke-WebRequest -UseBasicParsing -Uri $Uri).content
4
(iwr $Uri -UseB).content
Copied!

Invoke-RestMethod

This is a better alternative for scripts as Invoke-RestMethod returns the content as structured data
1
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'
2
​
3
Invoke-RestMethod -Uri $Uri
4
irm $Uri
Copied!

URL Shortner

The goal is to make the shorter, so why not try a URL shortener? It is easy enough to test if the URL shortener you are going to use will work, and most of them should
ShortURL - URL Shortener
Bitly | Custom URL Shortener, Link Management & Branded Links
Bitly

Subdomains

This won't work with all hosts, but you may be able to redirect a subdomain if your DNS Host supports this. I use Google Domains for OSDCloud.com and they make it very easy to do this
This quickly allows me to get to my PSCloudScript by simply going to live.osdcloud.com​
One benefit of using GitHub Raw links is that the MIME information is presented as Text. If you are linking to another source, you may be prompted to download the ps1 file rather than viewing the text
Which looks really nice when I want to perform an Invoke-RestMethod

Executing a PSCloudScript

This is the simple part. Once you have a method to get your PSCloudScript content, just use Invoke-Expression -Command to execute it. Here are some examples on how to do this
1
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'
2
​
3
Invoke-Expression -Command (Invoke-RestMethod -Uri $Uri)
4
iex(irm $Uri)pow
5
Invoke-RestMethod -Uri $Uri | Invoke-Expression
6
irm $Uri|iex
Copied!
You can test my script in full Windows as it will only execution the functions in WinPE

OSDCloud Live

So here I have a basic WinPE with PowerShell installed, but there are some issues running OSDCloud ...
  • PSGallery doesn't work
  • Curl.exe isn't present
  • OSD Module isn't installed
So instead of mounting and making changes to the WIM, I'll script my solution and run this as a PSCloudScript. From a Command Prompt, any of these commands will work
1
REM Too long
2
powershell Invoke-Expression -Command (Invoke-RestMethod -Uri live.osdcloud.com)
3
​
4
REM Still too long
5
powershell Invoke-RestMethod -Uri live.osdcloud.com | Invoke-Expression
6
​
7
REM Not everyone will understand the pipe
8
powershell irm live.osdcloud.com | iex
9
​
10
REM Shortest command line, but will non-ps users understand a pipe?
11
powershell irm live.osdcloud.com|iex
12
​
13
REM Ideal command line
14
powershell iex(irm live.osdcloud.com)
Copied!
Optionally these can be run if you are already in PowerShell
1
#Too long
2
Invoke-Expression -Command (Invoke-RestMethod -Uri live.osdcloud.com)
3
​
4
#Still too long
5
Invoke-RestMethod -Uri live.osdcloud.com | Invoke-Expression
6
​
7
#Not everyone will understand the pipe
8
irm live.osdcloud.com | iex
9
​
10
#Shortest command line, but will non-ps users understand a pipe?
11
irm live.osdcloud.com|iex
12
​
13
#Ideal command line
14
iex(irm live.osdcloud.com)
Copied!
Now I can boot to WinPE and run OSDCloud as long as it has PowerShell from the ADK installed :)