Content-Type | Azure Static Web App
So now is about the time that I address what Content-Type is and how it relates to hosting PowerShell files on the internet. To demonstrate this, I'll create an Azure Static Web App
Azure Static Web App is a service that can publish a GitHub Repo to an Azure Web using GitHub Actions. You can learn more at the following links
The configuration is very simple, I used defaults for most settings
The first time I ran this it failed because I didn't have an Index.html in the root of my GitHub Repo, so make sure you check the Job for any errors
So I simply created a copy of a PowerShell script and saved it as Index.html in the root of my Repo and had the Build and Deploy Job run again
Once the Azure Static Web App build finally completed, my PSCloudScript GitHub Repo was published at the following link
Opening this in Chrome shows all of the PowerShell formatting is lost
Additionally, when I navigate to the URL of one of my PowerShell .ps1 files, I'm prompted to download the file instead of viewing the content in Chrome. This is not the same results that I received when viewing the content on GitHub's Raw links
The reason why the PowerShell script saved as HTML, and the .ps1 file don't render the same as they do in GitHub Raw is because of the Content-Type. A quick check of the different URL's with
Invoke-WebRequestconfirms this. These files are absolutely being rendered based on their Content-Type
PS C:\> (Invoke-WebRequest -UseBasicParsing agreeable-river-0b8a1af10.1.azurestaticapps.net).Headers.'Content-Type'
PS C:\> (Invoke-WebRequest -UseBasicParsing agreeable-river-0b8a1af10.1.azurestaticapps.net/Test-PSCloudScriptAzStaticWebApp.ps1).Headers.'Content-Type'
#GitHub Raw Test-PSCloudScriptAzStaticWebApp.ps1
PS C:\> (Invoke-WebRequest -UseBasicParsing https://raw.githubusercontent.com/OSDeploy/PSCloudScript/main/Test-PSCloudScriptAzStaticWebApp.ps1).Headers.'Content-Type'
To keep the PowerShell formatting in the .html and the .ps1 file, it is necessary to change the Content-Type to
text/plain(the same as GitHub Raw). To change this on an Azure Static Web App, I can simply create a file called
staticwebapp.config.jsonin the root of my GitHub Repo with the following settings
This configuration change is detailed in Microsoft Docs at the following link
After creating the config file and pushing it to GitHub, the GitHub Action will publish the content back to my Azure Static Web. Another test in Chrome shows the
text/plainformatting in index.html, and the PowerShell file will now display in the browser rather than prompting for a download
If you are publishing PowerShell scripts to a Web Site, make sure you set the Content-Type for the files to
text/plainif you want them displayed and rendered properly in a browser. Additionally, if you are publishing PowerShell scripts to an Azure Blob Storage, you can set the Content-Type to
text/plainas needed using Azure Storage Explorer