locked
Powershell “Start-Service” cmdlet not working . Doesn't write anything to the host either RRS feed

  • Question

  • Hello folks,

    I saved this function into StartWindowsService.ps1 file. I made sure that the executionpolicy is 'Unrestricted'. I ran this file the following way: Before I ran the next line, I stopped the 'FAService' from services. so that my function will start the service.

    I ran the following line at Powershell command prompt. It doesn't write to host anything and doesn't start FASservice. I am really dumb founded. 

    It clearly shows that I am not calling the function in the right way from command prompt. How is it so difficult in powershell to call a function which accepts parameters?

    C:\LocationofthisFile\ .\StartWindowsService.ps1 StartWindowsService FAService

    function StartWindowsService{
              Param([string] $ServiceName)
              $aService = Get-Service -Name $ServiceName
              if ($aService.Status -ne "Running"){
                  Start-Service $ServiceName
                  Write-Host "Starting " $ServiceName " service" 
                  " ---------------------- " 
                  " Service is now started"
               }
    
              if ($aService.Status -eq "running"){ 
                     Write-Host "$ServiceName service is already started"
               }
         }

    Saturday, February 24, 2018 2:54 PM

All replies