Start-Service cmdlet not working in a function

Welcome Forums General PowerShell Q&A Start-Service cmdlet not working in a function

This topic contains 4 replies, has 4 voices, and was last updated by

 
Participant
8 months, 4 weeks ago.

  • Author
    Posts
  • #94530

    Participant
    Points: 0
    Rank: Member

    I am passing the name of the service as a parameter. If the service is not running, it should start the service.

    I made sure that I have "Unrestricted" execution policy before I ran this function.

    I went into the directory where this .PS1 file exists (name of the file is StartWindowsservice.ps1) and ran this as following:

    > .\StartWindowsService.ps1 StartWindowsservice 'FAService'

    Nothing happens. It doesn't write anything to console either.

    Any help please?

              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"
                  }
                 }
         
  • #94533

    Participant
    Points: 49
    Rank: Member

    I just tested it out and ran fine....

    PS C:\WINDOWS\system32> StartWindowsService -ServiceName "SQLWriter"
    SQLWriter service is already started
    
    • #94536

      Participant
      Points: 0
      Rank: Member

      Oh gosh. I don't know why it is not working for me.

      StartWindowsService -ServiceName "SQLWriter"

      I am running on windows 8. Is there anything that I should do something different?

  • #94545

    Participant
    Points: 0
    Rank: Member

    > .\StartWindowsService.ps1 StartWindowsservice 'FAService'

    If you want to run it as a .ps1, then you have to strip the function part out and the contents of the .ps1 file would be:

    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"
    }

    And you would run that like:

    .\StartWindowsService.ps1 -ServiceName 'FAService'

    Otherwise, leaving it like you have it you have to dot source the file to get access to the function, and you'd have to call it either on two lines or separated by a semicolon like:

    . .\StartWindowsService.ps1
    StartWindowsservice -ServiceName 'FAService'
    #OR
    . .\StartWindowsService.ps1; StartWindowsservice -ServiceName 'FAService'
    
  • #94546

    Participant
    Points: -9
    Rank: Member

    Hi Jason,

    Are you running the powershell console as administrator?
    Can you try

    get-service 'servicename' | start-service
    

    Let us know if that works while the ps console is run as a administrator

    Regards
    Shihan

The topic ‘Start-Service cmdlet not working in a function’ is closed to new replies.