Reputation: 57
I have a problem with creating a script in windows powershell. So far I was able to find and list services that contain a given string of characters, but I have an assignment problem with RequiredService to each of the services. This is my code
$characters = Read-Host "Enter a character string to appear in the service"
$name = get-service -name *$characters*
For($i=0; $i -lt $name.Count; $i++)
{
Write-Host $name[$i]
$Required = get-service -RequiredServices $name[$i]
For($j=0; $j -lt $Required.Count; $j++)
{
Write-Host " "$Required[$j]
}
}
exit 0
and this is a problem
get-service : Cannot find any service with service name 'System.ServiceProcess.ServiceController'.
I have no idea why each service, despite being displayed well, is further otherwise as 'System.ServiceProcess.ServiceController'
Edit: I changed line 6
$Required = get-service -RequiredServices *($name[$i])
,but still the script doesn't work.
Get-Service : A positional parameter cannot be found that accepts argument 'XboxNetApiSvc'.
Upvotes: 3
Views: 187
Reputation: 8878
Powershell will automatically format objects for us in certain scenarios such as write host. Write-Host will show you the service name because that's the default tostring() for service controller objects. You simply need to specify the name yourself when trying to use it in a command
$characters = Read-Host "Enter a character string to appear in the service"
$name = get-service -name *$characters*
For($i=0; $i -lt $name.Count; $i++)
{
Write-Host $name[$i]
$Required = get-service -RequiredServices $name[$i].name
For($j=0; $j -lt $Required.Count; $j++)
{
Write-Host " "$Required[$j].name
}
}
exit 0
Upvotes: 2