# 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](https://www.recastsoftware.com/?utm_source=osdeploy\&utm_medium=ad\&utm_campaign=web) gets mad props for supporting Community content like this. Make sure you check our their Systems Management Tools

{% embed url="<https://www.recastsoftware.com/?utm_source=osdeploy&utm_medium=ad&utm_campaign=web>" %}
Sponsored by Recast Software
{% endembed %}

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

{% embed url="<https://gist.github.com>" %}

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

![](/files/5HMQ4kw1JZXqH7D7GDFv)

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

{% embed url="<https://marketplace.visualstudio.com/items?itemName=vsls-contrib.gistfs>" %}

You will need to sign into your GitHub account and authorize Visual Studio Code to access GitHub

![](/files/6BhmiiZA0efrcUnPcPho)

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

![](/files/HmKhXG9EOUkyCY77kLiw)

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

```
Gist Editor and Share URL Format
https://gist.github.com/<GitHubUser>/<Gist>

Gist Editor and Share URL
https://gist.github.com/OSDeploy/30dc8839e8972493be1a343d509f423f
```

![](/files/PN25XXeVK977jUThSqUF)

### Gist Raw Commit URL

When you press the Raw button on the Gist Editor Share Link, you are redirected to the Raw Commit Link

![](/files/5Xu6BsgmZaJg6NTJetQe)

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

```
Gist Raw URL Format
<Site>/<GitHubUser>/<Gist>/raw/<Commit>/<ScriptName>

Gist Raw URL
https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/8fdf485273d0d69d6b54c624e758c9b90f6b5bce/live.osdcloud.com.ps1
```

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

```
Gist Raw URL Format
<Site>/<GitHubUser>/<Gist>/raw/<ScriptName>

Gist Raw URL
https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1
```

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

```powershell
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'

(Invoke-WebRequest -UseBasicParsing -Uri $Uri).content
(iwr $Uri -UseB).content
```

![](/files/Z99jVbuMZCDsQatKJ9gf)

### Invoke-RestMethod

This is a better alternative for scripts as Invoke-RestMethod returns the content as structured data

```powershell
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'

Invoke-RestMethod -Uri $Uri
irm $Uri
```

![](/files/DYiIm6PuUdei7sB1UtUL)

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

{% embed url="<https://www.shorturl.at>" %}

![](/files/3Jk0DqklTUnLICQgtg0H)

{% embed url="<https://bitly.com>" %}

![](/files/L7SXPzSBVhs043yZYXon)

## 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](https://domains.google.com/registrar/) for OSDCloud.com and they make it very easy to do this

![](/files/Kc1hb0WTpMsDH4MkbeMy)

This quickly allows me to get to my PSCloudScript by simply going to [live.osdcloud.com](http://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

![](/files/S7GAmubgktaXuETDco3v)

Which looks really nice when I want to perform an `Invoke-RestMethod`

![](/files/W8gQzs5FPuZohF4xWkzG)

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

```powershell
$Uri = 'https://gist.githubusercontent.com/OSDeploy/30dc8839e8972493be1a343d509f423f/raw/live.osdcloud.com.ps1'

Invoke-Expression -Command (Invoke-RestMethod -Uri $Uri)
iex(irm $Uri)pow
Invoke-RestMethod -Uri $Uri | Invoke-Expression
irm $Uri|iex
```

![](/files/Rui5HjNg15Td360NLeEL)

{% hint style="info" %}
You can test my script in full Windows as it will only execution the functions in WinPE
{% endhint %}

![](/files/FYztn3WiCHvVLADRJqCw)

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

![](/files/O8pKbrGP0t2ChFepRbKe)

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

```batch
REM Too long
powershell Invoke-Expression -Command (Invoke-RestMethod -Uri live.osdcloud.com)

REM Still too long
powershell Invoke-RestMethod -Uri live.osdcloud.com | Invoke-Expression

REM Not everyone will understand the pipe
powershell irm live.osdcloud.com | iex

REM Shortest command line, but will non-ps users understand a pipe?
powershell irm live.osdcloud.com|iex

REM Ideal command line
powershell iex(irm live.osdcloud.com)
```

Optionally these can be run if you are already in PowerShell

```powershell
#Too long
Invoke-Expression -Command (Invoke-RestMethod -Uri live.osdcloud.com)

#Still too long
Invoke-RestMethod -Uri live.osdcloud.com | Invoke-Expression

#Not everyone will understand the pipe
irm live.osdcloud.com | iex

#Shortest command line, but will non-ps users understand a pipe?
irm live.osdcloud.com|iex

#Ideal command line
iex(irm live.osdcloud.com)
```

Now I can boot to WinPE and run OSDCloud as long as it has PowerShell from the ADK installed :)

![](/files/Q5TF1Ds5Lq4CggUyEmFw)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.osdeploy.com/archive/blog/2022/pscloudscript-basics.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
