Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
803 views
in Technique[技术] by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...