Please suggest how can i convert below PowerShell script to bash script

Santosh Shet 1 Reputation point
2022-06-09T03:55:39.117+00:00

$releaseBody = @{
ChannelId = $channel.Id
ProjectId = $project.Id
Version = $releaseVersion
SelectedPackages = @()
}

$template = Invoke-RestMethod -Uri "$octopusURL/api/$($space.id)/deploymentprocesses/deploymentprocess-$($project.id)/template?channel=$($channel.Id)" -Headers $header

$template.Packages | ForEach-Object {
$uri = "$octopusURL/api/$($space.id)/feeds/$($.FeedId)/packages/versions?packageId=$($.PackageId)&take=1"
$version = Invoke-RestMethod -Uri $uri -Method GET -Headers $header
$version = $version.Items[0].Version

$releaseBody.SelectedPackages += @{  
    ActionName           = $_.ActionName  
    PackageReferenceName = $_.PackageReferenceName  
    Version              = $version  
}  

}

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-06-10T08:05:19.157+00:00

    Hi SantoshShet-8694,

    There is no Automated tools or options to convert any PowerShell script to bash script and if you do it manually there are chances of having limited functionality or compatibility but you can make your PowerShell scripts run directly from Bash without the need of running PowerShell first.

    Let's consider your script is saved as new.ps1

    $releaseBody = @{
    ChannelId = $channel. Id
    ProjectId = $project. Id
    Version = $releaseVersion
    SelectedPackages = @()
    }

    $template = Invoke-RestMethod -Uri "$octopusURL/api/$($space. id)/deploymentprocesses/deploymentprocess-$($project. id)/template?channel=$($channel. Id)" -Headers $header

    $template.Packages | ForEach-Object {
    $uri = "$octopusURL/api/$($space. id)/feeds/$($.FeedId)/packages/versions?packageId=$($.PackageId)&take=1"
    $version = Invoke-RestMethod -Uri $uri -Method GET -Headers $header
    $version = $version.Items[0].Version

    $releaseBody.SelectedPackages += @{
    ActionName = $.ActionName
    PackageReferenceName = $
    .PackageReferenceName
    Version = $version
    }
    }

    To call this script you will need to do the following in PowerShell.

    $ .\new.ps1 "My Script"

    This will create a markdown file in the current directory with the title passed in and the contents from the script.

    Now, To execute the PowerShell script directly from Bash on Linux. If you put a shebang at the start of the script and the path to the interpreter you want to use.

    Type the following command in Bash to determine the location of the PowerShell executable on your system.

    $ which pwsh
    /usr/bin/pwsh

    Now that we know the location, let’s edit the new.ps1 script to add shebang.

    ! /usr/bin/pwsh

    $releaseBody = @{
    ChannelId = $channel. Id
    ProjectId = $project. Id
    Version = $releaseVersion
    SelectedPackages = @()
    }

    $template = Invoke-RestMethod -Uri "$octopusURL/api/$($space. id)/deploymentprocesses/deploymentprocess-$($project. id)/template?channel=$($channel. Id)" -Headers $header

    $template.Packages | ForEach-Object {
    $uri = "$octopusURL/api/$($space. id)/feeds/$($.FeedId)/packages/versions?packageId=$($.PackageId)&take=1"
    $version = Invoke-RestMethod -Uri $uri -Method GET -Headers $header
    $version = $version.Items[0].Version

    $releaseBody.SelectedPackages += @{
    ActionName = $.ActionName
    PackageReferenceName = $
    .PackageReferenceName
    Version = $version
    }
    }

    With that line added the final step is to make it executable.

    $ chmod +x new.ps1

    Now you can run our PowerShell script from Bash and the shebang will make sure the script is ran using PowerShell.

    $ ./new.ps1 "My Script"


    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.