How to automatically start (or create and start) windows service after codedeploy to an EC2 instance












0














I am attempting to create a pipeline that can automatically deploy via AWS-CodeDeploy and start a windows service on an EC2 instance, but I am having trouble getting the powershell scripting correct.



I have the following:



Appspec.yml



version: 0.0
os: windows
files:
- source:
destination: C:tempMyApp
hooks:
ApplicationStop:
- location: DeploymentScriptsapplicationStop.bat
timeout: 180
ApplicationStart:
- location: DeploymentScriptsapplicationStart.bat
timeout: 180


applicationStart.bat



c:WindowsSystem32WindowsPowerShellv1.0powershell.exe C:tempMyAppDeploymentScriptsservice_create_start.ps1 -ServiceName MyService -BinaryPath 'C:tempMyAppMyService.exe' -DisplayName 'Test Application' -Description 'This is a test'


And the following powershell service_create_start.ps1:



Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)

If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {

If ((Get-Service $ServiceName).Status -eq 'Running') {

Stop-Service $ServiceName
Write-Host "Stopping $ServiceName"

} Else {

Write-Host "$ServiceName found, but it is not running."

}

} Else {

Write-Host "$ServiceName not found. Creating new Windows service."

}

$ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description

New-Service $ArgumentList

Start-Service $ServiceName


I created the bat file because it seemed that I could not launch a powershell script from code deploy with parameters. I am not otherwise tied to it.



My first problem is that it gets hung a prompt for "BinaryPathName" I have a feeling this is an issue with my quotes in one of these files.



My second problem (if I go enter this manually in testing) is that I get an Access is denied exception on the New-Service line.



To address this problem I have tried to use Start-Process an the RunAs verb to run an elevated powershell, however I am having trouble figuring out the syntax to add my parameters in (beyond the ps1 file name) with this syntax.



Am I even on the correct path here, or is there a better way to do this?










share|improve this question



























    0














    I am attempting to create a pipeline that can automatically deploy via AWS-CodeDeploy and start a windows service on an EC2 instance, but I am having trouble getting the powershell scripting correct.



    I have the following:



    Appspec.yml



    version: 0.0
    os: windows
    files:
    - source:
    destination: C:tempMyApp
    hooks:
    ApplicationStop:
    - location: DeploymentScriptsapplicationStop.bat
    timeout: 180
    ApplicationStart:
    - location: DeploymentScriptsapplicationStart.bat
    timeout: 180


    applicationStart.bat



    c:WindowsSystem32WindowsPowerShellv1.0powershell.exe C:tempMyAppDeploymentScriptsservice_create_start.ps1 -ServiceName MyService -BinaryPath 'C:tempMyAppMyService.exe' -DisplayName 'Test Application' -Description 'This is a test'


    And the following powershell service_create_start.ps1:



    Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)

    If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {

    If ((Get-Service $ServiceName).Status -eq 'Running') {

    Stop-Service $ServiceName
    Write-Host "Stopping $ServiceName"

    } Else {

    Write-Host "$ServiceName found, but it is not running."

    }

    } Else {

    Write-Host "$ServiceName not found. Creating new Windows service."

    }

    $ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description

    New-Service $ArgumentList

    Start-Service $ServiceName


    I created the bat file because it seemed that I could not launch a powershell script from code deploy with parameters. I am not otherwise tied to it.



    My first problem is that it gets hung a prompt for "BinaryPathName" I have a feeling this is an issue with my quotes in one of these files.



    My second problem (if I go enter this manually in testing) is that I get an Access is denied exception on the New-Service line.



    To address this problem I have tried to use Start-Process an the RunAs verb to run an elevated powershell, however I am having trouble figuring out the syntax to add my parameters in (beyond the ps1 file name) with this syntax.



    Am I even on the correct path here, or is there a better way to do this?










    share|improve this question

























      0












      0








      0







      I am attempting to create a pipeline that can automatically deploy via AWS-CodeDeploy and start a windows service on an EC2 instance, but I am having trouble getting the powershell scripting correct.



      I have the following:



      Appspec.yml



      version: 0.0
      os: windows
      files:
      - source:
      destination: C:tempMyApp
      hooks:
      ApplicationStop:
      - location: DeploymentScriptsapplicationStop.bat
      timeout: 180
      ApplicationStart:
      - location: DeploymentScriptsapplicationStart.bat
      timeout: 180


      applicationStart.bat



      c:WindowsSystem32WindowsPowerShellv1.0powershell.exe C:tempMyAppDeploymentScriptsservice_create_start.ps1 -ServiceName MyService -BinaryPath 'C:tempMyAppMyService.exe' -DisplayName 'Test Application' -Description 'This is a test'


      And the following powershell service_create_start.ps1:



      Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)

      If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {

      If ((Get-Service $ServiceName).Status -eq 'Running') {

      Stop-Service $ServiceName
      Write-Host "Stopping $ServiceName"

      } Else {

      Write-Host "$ServiceName found, but it is not running."

      }

      } Else {

      Write-Host "$ServiceName not found. Creating new Windows service."

      }

      $ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description

      New-Service $ArgumentList

      Start-Service $ServiceName


      I created the bat file because it seemed that I could not launch a powershell script from code deploy with parameters. I am not otherwise tied to it.



      My first problem is that it gets hung a prompt for "BinaryPathName" I have a feeling this is an issue with my quotes in one of these files.



      My second problem (if I go enter this manually in testing) is that I get an Access is denied exception on the New-Service line.



      To address this problem I have tried to use Start-Process an the RunAs verb to run an elevated powershell, however I am having trouble figuring out the syntax to add my parameters in (beyond the ps1 file name) with this syntax.



      Am I even on the correct path here, or is there a better way to do this?










      share|improve this question













      I am attempting to create a pipeline that can automatically deploy via AWS-CodeDeploy and start a windows service on an EC2 instance, but I am having trouble getting the powershell scripting correct.



      I have the following:



      Appspec.yml



      version: 0.0
      os: windows
      files:
      - source:
      destination: C:tempMyApp
      hooks:
      ApplicationStop:
      - location: DeploymentScriptsapplicationStop.bat
      timeout: 180
      ApplicationStart:
      - location: DeploymentScriptsapplicationStart.bat
      timeout: 180


      applicationStart.bat



      c:WindowsSystem32WindowsPowerShellv1.0powershell.exe C:tempMyAppDeploymentScriptsservice_create_start.ps1 -ServiceName MyService -BinaryPath 'C:tempMyAppMyService.exe' -DisplayName 'Test Application' -Description 'This is a test'


      And the following powershell service_create_start.ps1:



      Param([Parameter(Mandatory=$true)][string]$ServiceName,[Parameter(Mandatory=$true)][ValidateScript({Test-Path $_ -PathType 'leaf'})][string]$BinaryPath, [string]$Displayname, [string]$Description)

      If (Get-Service $ServiceName -ErrorAction SilentlyContinue) {

      If ((Get-Service $ServiceName).Status -eq 'Running') {

      Stop-Service $ServiceName
      Write-Host "Stopping $ServiceName"

      } Else {

      Write-Host "$ServiceName found, but it is not running."

      }

      } Else {

      Write-Host "$ServiceName not found. Creating new Windows service."

      }

      $ArgumentList = '-Name "{0}" -BinaryPathName "{1} -k netsvcs" -DisplayName "{2}" -StartupType Automatic -Description "{3}"' -f $ServiceName, $BinaryPath, $DisplayName, $Description

      New-Service $ArgumentList

      Start-Service $ServiceName


      I created the bat file because it seemed that I could not launch a powershell script from code deploy with parameters. I am not otherwise tied to it.



      My first problem is that it gets hung a prompt for "BinaryPathName" I have a feeling this is an issue with my quotes in one of these files.



      My second problem (if I go enter this manually in testing) is that I get an Access is denied exception on the New-Service line.



      To address this problem I have tried to use Start-Process an the RunAs verb to run an elevated powershell, however I am having trouble figuring out the syntax to add my parameters in (beyond the ps1 file name) with this syntax.



      Am I even on the correct path here, or is there a better way to do this?







      powershell amazon-ec2 service aws-code-deploy






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 12 at 21:46









      CSeverance

      102




      102
























          1 Answer
          1






          active

          oldest

          votes


















          0














          I dont think you can pass in parameters like this (as a single string), you better off using splatting:



          $myInput = @{
          Name = $ServiceName
          BinaryPathName = '{0} -k netsvc' -f $BinaryPath
          StartupType = 'automatic'
          DisplayName = $DisplayName
          Description = $Description
          }
          New-Service @myInput


          reading: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6






          share|improve this answer

















          • 1




            This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
            – CSeverance
            Nov 13 at 15:36











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270574%2fhow-to-automatically-start-or-create-and-start-windows-service-after-codedeplo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          I dont think you can pass in parameters like this (as a single string), you better off using splatting:



          $myInput = @{
          Name = $ServiceName
          BinaryPathName = '{0} -k netsvc' -f $BinaryPath
          StartupType = 'automatic'
          DisplayName = $DisplayName
          Description = $Description
          }
          New-Service @myInput


          reading: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6






          share|improve this answer

















          • 1




            This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
            – CSeverance
            Nov 13 at 15:36
















          0














          I dont think you can pass in parameters like this (as a single string), you better off using splatting:



          $myInput = @{
          Name = $ServiceName
          BinaryPathName = '{0} -k netsvc' -f $BinaryPath
          StartupType = 'automatic'
          DisplayName = $DisplayName
          Description = $Description
          }
          New-Service @myInput


          reading: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6






          share|improve this answer

















          • 1




            This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
            – CSeverance
            Nov 13 at 15:36














          0












          0








          0






          I dont think you can pass in parameters like this (as a single string), you better off using splatting:



          $myInput = @{
          Name = $ServiceName
          BinaryPathName = '{0} -k netsvc' -f $BinaryPath
          StartupType = 'automatic'
          DisplayName = $DisplayName
          Description = $Description
          }
          New-Service @myInput


          reading: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6






          share|improve this answer












          I dont think you can pass in parameters like this (as a single string), you better off using splatting:



          $myInput = @{
          Name = $ServiceName
          BinaryPathName = '{0} -k netsvc' -f $BinaryPath
          StartupType = 'automatic'
          DisplayName = $DisplayName
          Description = $Description
          }
          New-Service @myInput


          reading: https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_splatting?view=powershell-6







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 13 at 6:13









          4c74356b41

          23.9k32050




          23.9k32050








          • 1




            This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
            – CSeverance
            Nov 13 at 15:36














          • 1




            This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
            – CSeverance
            Nov 13 at 15:36








          1




          1




          This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
          – CSeverance
          Nov 13 at 15:36




          This solved the problem completely. It seems that when the code deploy agent ran it, it was not getting the access denied message, so using splatting to get the parameters passed in correctly solved all of my problems. Thank you!
          – CSeverance
          Nov 13 at 15:36


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.





          Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


          Please pay close attention to the following guidance:


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53270574%2fhow-to-automatically-start-or-create-and-start-windows-service-after-codedeplo%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Guess what letter conforming each word

          Port of Spain

          Run scheduled task as local user group (not BUILTIN)