简体   繁体   中英

problem with service interpretation in windows powershell script

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'.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM