Skip to content

Instantly share code, notes, and snippets.

@thereverand
Last active May 5, 2023 09:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save thereverand/e97427dc67fa6c035e299c72b859d91f to your computer and use it in GitHub Desktop.
Save thereverand/e97427dc67fa6c035e299c72b859d91f to your computer and use it in GitHub Desktop.
Install PowerShell Universal as a service on Linux
# ----
# This script will install PowerShell Universal on Linux as a service
# This has been tested on Ubuntu 20.04 (ARM64) on a Raspberry Pi 4
# ----
# Dependencies:
# wget
# unzip
#
# Make sure they are installed
# ----
# These are used to derive the download URL
PSU_VERSION="2.4.1" # Change this to the current version
PSU_ARCH="arm64" # Change this to your desired architecture
PSU_FILE="Universal.linux-${PSU_ARCH}.${PSU_VERSION}.zip"
PSU_URL="https://imsreleases.blob.core.windows.net/universal/production/${PSU_VERSION}/${PSU_FILE}"
# These are used for installing PowerShell Universal
# If you'd like to use a different path, change this
PSU_PATH="/opt/psuniversal"
PSU_EXEC="${PSU_PATH}/Universal.Server"
# These are for installing it as a service
PSU_SERVICE="psuniversal"
PSU_USER="psuniversal"
# ----
# BEGIN
# ----
echo "Creating $PSU_PATH and granting access to $USER"
sudo mkdir $PSU_PATH
sudo setfacl -m "u:${USER}:rwx" $PSU_PATH
echo "Creating user $PSU_USER and making it the owner of $PSU_PATH"
sudo useradd $PSU_USER -m
sudo chown $PSU_USER -R $PSU_PATH
echo "Downloading PowerShell Universal $PSU_VERSION ($PSU_ARCH)"
wget -q $PSU_URL -O $PSU_FILE
echo "Extracting $PSU_FILE to $PSU_PATH"
unzip -o -qq $PSU_FILE -d $PSU_PATH
echo "Make $PSU_EXEC executable"
sudo chmod +x $PSU_EXEC
echo "Creating service configuration"
cat <<EOF > ~/$PSU_SERVICE.service
[Unit]
Description=PowerShell Universal
[Service]
ExecStart=$PSU_EXEC
SyslogIdentifier=psuniversal
User=$PSU_USER
Restart=always
RestartSec=5
[Install]
WantedBy=multi-user.target
EOF
echo "Creating and starting service"
sudo cp -f ~/$PSU_SERVICE.service /etc/systemd/system
sudo systemctl daemon-reload
sudo systemctl enable $PSU_SERVICE
sudo systemctl start $PSU_SERVICE
sudo systemctl status $PSU_SERVICE --no-pager
# If you don't use UFW, you can comment this out
echo "Allow port 5000/tcp"
sudo ufw allow 5000/tcp
# ----
# END
# ----
PSU_PATH="/opt/psuniversal"
PSU_SERVICE="psuniversal"
PSU_USER="psuniversal"
PSU_USER_HOME=$(sh -c "echo ~$PSU_USER")
echo "Stopping/Disabling/Deleting service $PSU_SERVICE"
sudo systemctl stop $PSU_SERVICE
sudo systemctl disable $PSU_SERVICE
sudo rm "/etc/systemd/system/${PSU_SERVICE}.service"
echo "Removing applicaton files"
sudo rm -r $PSU_PATH
echo "Removing user $PSU_USER and it's home directory"
sudo userdel $PSU_USER
sudo rm -r $(echo $PSU_USER_HOME)
@Dragod
Copy link

Dragod commented Oct 20, 2022

Thanks for this I couldn't find how to do it anywhere else.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment