1

I'm trying to pass parameters to this function bellow at the powershell CLI:

 function FuncCheckService{

 param($ServiceName)
 $arrService = Get-Service -Name $ServiceName
 if ($arrService.Status -ne "Running"){
 Start-Service $ServiceName
 Write-Host "Starting " $ServiceName " service" 
 " ---------------------- " 
 " Service is now started"
 }

 if ($arrService.Status -eq "running"){ 
 Write-Host "$ServiceName service is already started"
 }
 }

Service Function script

Did the following way testing with Software Protection service (sppsvc) and the command bellow on the Powershell CLI:

PS User>.\CHECKSERVICE.PS1 FuncCheckService -ServiceName 'sppsvc'

Still not working and get no outputs. Did I do any mistake above?

Tks in advance

  • 1
    Your script file does nothing but define function. It does not call it. Thus, no output. – user364455 Sep 20 '18 at 18:09
  • @PetSerAl In the prompt command line is the way I'm callling the function with parameters. – Marlon Sep 21 '18 at 20:59
  • 1
    If your script file contain single function definition, then you can just put function code directly into script and use it. If your script file contains multiple function definitions, then you can dot source it and call defined functions. – user364455 Sep 23 '18 at 16:15
  • @PetSerAl I edited post and put link to the original script.. Which way I could call this function ? If I understood I could do this way (at last line of powershell script): FuncCheckService -ServiceName "Name of service to monitor" – Marlon Sep 23 '18 at 18:04
  • . .\CHECKSERVICE.PS1; FuncCheckService … – user364455 Sep 23 '18 at 18:06
1

Calling function with one service:

function FuncCheckService{

param($ServiceName)
$arrService = Get-Service -Name $ServiceName
if ($arrService.Status -ne "Running"){
Start-Service $ServiceName
Write-Host "Starting " $ServiceName " service"
" ---------------------- "
" Service is now started"
}



if ($arrService.Status -eq "running"){
Write-Host "$ServiceName service is already started"
}

}
FuncCheckService(sppsvc)

Output:

.\service.ps1
Start-Service : Não é possível validar o argumento no parâmetro 'InputObject'. O argumento é nulo ou vazio. Forneça um
argumento que não seja nulo ou vazio e tente o comando novamente.
No C:\Users\Marlon\OneDrive\uteis\service.ps1:8 caractere:15
+ Start-Service $ServiceName
+               ~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Start-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.StartServiceCommand

Starting    service
 ----------------------
 Service is now started
 service is already started
  • 1
    Better late than never.... Consider showing how you are executing in addition to the script and the output. – Pimp Juice IT Oct 1 at 2:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.