user2049142
user2049142

Reputation: 149

Having trouble passing Credentials to Start-Service in Powershell without getting an invalid argument error

I have script that I'm using within a batch file to run a script to start service that requires the user starting the service to pass in a username and password the service requires. An input box comes up and I am able to enter in the credentials.

$srvName = Get-Service | Where-Object { $_.ServiceName -like ‘myService’ }
Start-Service net -arg $srvName -Credential (Get-Credential)Out-File C:\a.txt

However, the services doesn't start, and in my outfile, I get the following error.

CategoryInfo          : InvalidArgument: (:) [Start-Service], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.PowerShell.Commands.StartServiceCommand

When I look, I find plenty of examples of setting credentials for a service using Powershell, but only one for doing what I want to do and it obviously doesn't work.

Upvotes: 0

Views: 1630

Answers (1)

Jacob
Jacob

Reputation: 1192

You need to pipe it to out-file

$srvName = Get-Service | Where-Object { $_.ServiceName -like ‘myService’ }
Start-Service $srvName -Credential (Get-Credential) | Out-File C:\a.txt

The error posted states InvalidArgument: (:) because it's interpreting Out-File C:\a.txt as a parameter for Start-Service hence the InvalidArgument.

Additionally, start service doesn't have an -arg parameter, and I'm not sure what the net is for.

Upvotes: 1

Related Questions