function Publish-PuppetModule {
        Build and Publish Puppet Module
        Generate package for the module and publish to the forge.
      .PARAMETER PuppetModuleFolderPath
        The path, relative or absolute, to the Puppet module's root folder.
      .PARAMETER ForgeUploadUrl
        The URL for the Forge Upload API. Defaults to the public forge.
      .PARAMETER ForgeToken
        The Forge API Token for the target account.
      .PARAMETER Build
        Flag whether to build the package.
      .PARAMETER Publish
        Flag whether to publish the package.
        Publish-PuppetModule -PuppetModuleFolderPath C:\output\testmodule -ForgeUploadUrl -ForgeToken testmoduletoken -Build true -Publish true
        This command will create or use existing pkg and Publishes the <tarball> to the Forge , for the `testmodule` depends on the options passed for pdk release command.

    param (

    begin {
      $PuppetModuleFolderPath = Resolve-Path -Path $PuppetModuleFolderPath -ErrorAction Stop
      If ($Publish) {
        If ([string]::IsNullOrEmpty($ForgeUploadUrl)) {
          $CommandPublish = "pdk release publish --forge-token $ForgeToken"
        } Else {
          $CommandPublish = "pdk release publish --forge-token $ForgeToken --forge-upload-url $ForgeUploadUrl"
      If ($Build) {
        $CommandBuild = 'pdk build'
    process {
      Try {
        $ErrorActionPreference = 'Stop'
        If ($Build)  {
          Invoke-PdkCommand -Path $PuppetModuleFolderPath -Command $CommandBuild -SuccessFilterScript {
            $_ -match "completed successfully"
        If ($Publish) {
          Invoke-PdkCommand -Path $PuppetModuleFolderPath -Command $CommandPublish -SuccessFilterScript {
            $_ -match "Publish to Forge was successful"
      } Catch {
    end {}