Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
793 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 Reply

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.8k users

...