PowerShell - StopService with variable

Hi people mi need help

I am try create a script of stop services with variable values,  as down my script:

Clear-Host

$serviceList=gc servicos.txt

foreach ($service in $serviceList)

{

$service=$service-split(',')

$first=$service[0]

$second=$service[1]

$third=$service[2]

echo $first

Get-Service -InputObject $first | Start-Service

echo $second

Get-Service -InputObject $second | Start-Service

echo $third

Get-Service -InputObject $third | Start-Service

I get an error over the service ends successfully

 et-Service : Cannot bind parameter 'InputObject'. Cannot convert value "" to type "System.ServiceProcess.ServiceController". 

Error: "Invalid value  for parameter name."

At line:10 char:26

+ Get-Service -InputObject $first | Start-Service

+                          ~~~~~~

    + CategoryInfo          : InvalidArgument: (:) [Get-Service], ParameterBindingException

    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.GetServiceCommand

 

Get-Service : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument that is 

not null or empty, and then try the command again.

At line:12 char:26

+ Get-Service -InputObject $second | Start-Service

+                          ~~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-Service], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetServiceCommand

 

Get-Service : Cannot validate argument on parameter 'InputObject'. The argument is null or empty. Provide an argument that is 

not null or empty, and then try the command again.

At line:14 char:26

+ Get-Service -InputObject $third | Start-Service

+                          ~~~~~~

    + CategoryInfo          : InvalidData: (:) [Get-Service], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetServiceCommand 

March 24th, 2015 7:42am

How was your input text file formatted?


Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 7:55am

Hi.

The file was create as: servicoa,servicob,servicoc

and is a text file.

March 24th, 2015 8:32am

Hi,

Try it this way:

(Get-Content .\serviceList.txt ) -split ',' | ForEach {

    Start-Service -Name $_

}

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 9:15am

same error
March 24th, 2015 11:08am

same error

Not possible. You're using the actual service name, not the display name, correct?

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 11:21am

I am, the service started more with error.

example: my text file conatin: WinRM,Spooler,Themes

My script:

 Clear-Host

$serviceList=gc servicos.txt

foreach ($service in $serviceList)

{

$service=$service-split(',')

$first=$service[0]

$second=$service[1]

$third=$service[2]

echo $first

Get-Service -InputObject $first | Start-Service

echo $second

Get-Service -InputObject $second | Start-Service

echo $third

Get-Service -InputObject $third | Start-Service

Get-Service

your script:

 (Get-Content .\servicos.txt ) -split ',' | ForEach {

    Start-Service -Name $_

the error:

 Start-Service : Cannot bind argument to parameter 'Name' because it is an empty string.

At line:4 char:25

+     Start-Service -Name $_

+                         ~~

    + CategoryInfo          : InvalidData: (:) [Start-Service], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.StartServiceCo 

   mmand

 

Start-Service : Cannot bind argument to parameter 'Name' because it is an empty string.

At line:4 char:25

+     Start-Service -Name $_

+                         ~~

    + CategoryInfo          : InvalidData: (:) [Start-Service], ParameterBindingValidationException

    + FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Microsoft.PowerShell.Commands.StartServiceCo 

   mmand

  

March 24th, 2015 11:32am

What's the output of this:

(Get-Content .\servicos.txt  ) -split ',' | ForEach {

    Write-Host "Processing $_"

}

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 11:35am

 PS C:\Users\rogerioaguiarpacheco> (Get-Content .\servicos.txt  ) -split ',' | ForEach {

    Write-Host "Processing $_"

}

Processing WinRM

Processing Spooler

Processing Themes

Processing 

Processing  

March 24th, 2015 11:48am

See those blanks? That's going to be a problem.

Do you have extra commas in the file?

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 11:49am

No. I have one comma on file end.

ex: WinRM,Spooler,Themes,

March 24th, 2015 11:55am

No. I have one comma on file end.

ex: WinRM,Spooler,Themes,

Remove the trailing comma or do something like this:

(Get-Content .\servicos.txt) -split ',' | ForEach {

    If ($_ -ne '') {

        Write-Host "Processing $_"

    }

}

If that gives you the correct output, replace the Write-Host line with the Start-Service -Name $_ line from before.

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 12:02pm

No. I have one comma on file end.

ex: WinRM,Spooler,Themes,

Indeed that's causing the issue

I can reproduce the issue if my text file is like below

servicoa,servicob, servicoc
BITS,BITS,

Remove the comma at last - it will solve the

March 24th, 2015 12:04pm

Sucess! within the txt had blanks. Removed excluding this spaces.

Thanks for help. :)

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 12:12pm

Sucess! within the txt had blanks. Removed excluding this spaces.

Thanks for help. :)

Cheers, you're very welcome. Glad it worked out.

March 24th, 2015 12:14pm

Hi there,

i have a similar problem, 

in a script i get info about some services in my network

as an input i have:

$SDF

CMP      : DC1
RasMan   : Stopped
pla      : Stopped
AppIDSvc : Running
Schedule : Running

CMP      : SFILE
RasMan   : Stopped
pla      : Stopped
AppIDSvc : Stopped
Schedule : Running

##################

I tried to restart/stop/start services on target servers, for example:

$s="RasMan"
$comp = "dc1","sfile"

cls
#restart with error
Invoke-Command {restart-service $s passthru -Force} comp $compCannot bind argument to parameter 'Name' because it is null.
    + CategoryInfo          : InvalidData: (:) [Restart-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RestartServiceCommand
    + PSComputerName        : dc1
 
Cannot bind argument to parameter 'Name' because it is null.
    + CategoryInfo          : InvalidData: (:) [Restart-Service], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationErrorNullNotAllowed,Microsoft.PowerShell.Commands.RestartServiceCommand
    + PSComputerName        : sfile

** on target the $s =$null

if i use it like:

Invoke-Command {$s="RasMan";restart-service $s passthru -Force} comp $comp

work perfectly, but i need to do it for a diferent $S

have somone anny ideea?

best regards


Free Windows Admin Tool Kit Click here and download it now
June 18th, 2015 4:45am

This topic is closed and has been for quite a while.  Please start a new question.  It is against forum guidelines to tag you question onto another users topic.
June 18th, 2015 4:58am

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics