Januschan

RunPowerShellScript.sh

Dec 6th, 2019 (edited)
271
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 3.03 KB | None | 0 0
  1. #!/usr/bin/env bash
  2.  
  3. # call this script to execute a single powershell script
  4. # it will download powershell core if necessary
  5.  
  6. #  Usage
  7. #         sudo bash ./RunPowerShellScript.sh ./MyScriptToRun.ps1
  8.  
  9. # NOTE: like any shell script, this requires 'execute' permission which can be set as follows
  10. #         sudo chmod +x RunPowerShellScript.sh
  11.  
  12. function RunPowerShellScript()
  13. {
  14.     local platform="$(uname -s)"
  15.     case "${platform}" in
  16.         Linux*)     local archiveExtension=.tar.gz  #Linux
  17.                     local unarchive='sudo tar zxf ${tempDownload} -C ${targetdir}'
  18.                     local package=linux-x64${archiveExtension} ;;
  19.         Darwin*)    local archiveExtension=.tar.gz #Mac
  20.                     local unarchive='sudo tar zxf ${tempDownload} -C ${targetdir}'
  21.                     local package=osx-x64${archiveExtension} ;;
  22.         CYGWIN*)    local archiveExtension=.zip #Cygwin
  23.                     local unarchive='sudo powershell.exe -nologo -noprofile -command "& { Add-Type -A \''System.IO.Compression.FileSystem\''; [IO.Compression.ZipFile]::ExtractToDirectory(\''${tempDownload}\'', \''${targetdir}\''); }"'
  24.                     local package=win-x64${archiveExtension} ;;
  25.         MINGW*)     local archiveExtension=.zip #Mingw
  26.                     local unarchive='sudo powershell.exe -nologo -noprofile -command "& { Add-Type -A \''System.IO.Compression.FileSystem\''; [IO.Compression.ZipFile]::ExtractToDirectory(\''${tempDownload}\'', \''${targetdir}\''); }"'
  27.                     local package=win-x64${archiveExtension} ;;
  28.         *)          machine="UNKNOWN:${platform}" ;;
  29.     esac
  30.  
  31.     echo
  32.     echo Running on $platform
  33.  
  34.     local version=7.1.2
  35.     local url=https://github.com/PowerShell/PowerShell/releases/download/v${version}/powershell-${version}-${package}
  36.     local tempDownload=/tmp/powershell.${archiveExtension}
  37.     local targetdir=/usr/local/tmp/microsoft/powershell/${version}
  38.  
  39.     echo
  40.  
  41.     if test -f $targetdir/pwsh; then
  42.         echo "$targetdir/pwsh already exists."
  43.     else
  44.         echo "Downloading $tempDownload..."
  45.         echo
  46.         # Download the powershell '.tar.gz' archive
  47.         curl -L -o $tempDownload $url
  48.  
  49.         # Create the target folder where powershell will be placed
  50.         sudo mkdir -p $targetdir
  51.  
  52.         # Expand powershell to the target folder
  53.         eval $unarchive
  54.  
  55.         # Set execute permissions
  56.         sudo chmod +x $targetdir/pwsh
  57.     fi
  58.  
  59.     # Create the symbolic link that points to pwsh
  60.     #sudo ln -s $targetdir/pwsh /usr/local/bin/pwsh
  61.    
  62.     # Remove the symbolic link
  63.     #unlink /usr/local/bin/pwsh
  64.    
  65.     # OS-X pre-req
  66.     # xcode-select --install  # this could be part of the Darwin case in switch above
  67.  
  68.     echo $targetdir/pwsh $1
  69.     echo
  70.     $targetdir/pwsh $1
  71.     echo
  72.  
  73.     # Cleanup (defeats the existence check)
  74.     #sudo rm -f -r $targetdir
  75. }
  76.  
  77. if [ $# -eq 0 ]
  78.   then
  79.     echo
  80.     echo "Missing required parameter: path of PowerShell script to run"
  81.     echo
  82.     echo "Usage: sudo bash ./RunPowerShellScript.sh ./MyScriptToRun.ps1"
  83.     echo
  84.   else
  85.     RunPowerShellScript $1
  86. fi
  87.  
Add Comment
Please, Sign In to add comment