Powershell script to modify service startup type from Automatic to Manual

Hi

Can some help me to modify a powershell script to change STARTUP Type for some of services from AUTOMATIC to MANUAL ?

This script is for starting service. I need the same script to modify Startup Type.

$serverList  = gc servers.txt
$serviceList = gc services.txt

ForEach ($server in $serverList)
{
    ForEach ($service in $serviceList)
    {
        Get-Service -Name $service -ComputerName $server | Start-service
    }
}
michalek19Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Jose Gabriel Ortega CastroCEOCommented:
it would be something like:

$serverList  = gc servers.txt
$serviceList = gc services.txt

ForEach ($server in $serverList){
    ForEach ($service in $serviceList){
     #this will set to manual and start the service, if you just want to change the automatic to manual, remove the | start-service
        Get-Service -Name $service -ComputerName $server | Set-Service -StartupType Manual |  Start-Service
    }
}

Open in new window


With elevated privilegies.

Note: this will set to manual and start the service, if you just want to change the automatic to manual, remove the | start-service in line 7
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
Above answers your question directly, but it's much better to make use of the string array feature of Get-Service.
$serverList  = gc servers.txt
$serviceList = gc services.txt
Get-Service -Name $serviceList -ComputerName $serverList | Set-Service -StartupType Manual # |  Start-Service

Open in new window

or
Get-Service -Name (gc services.txt) -ComputerName (gc servers.txt) | Set-Service -StartupType Manual # |  Start-Service

Open in new window

0
yo_beeDirector of Information TechnologyCommented:
Qlemo would you be able to explain why its better to do it your method vs Jose.  
Yours look more straight forward, but is there any benefit in performance?
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
The code as shown by Jose has to query each computer for each single service one by one. So first optimization is to contact each PC once to get all services of interest, by providing the string array for services as a parameter. This improves performance.

Providing a computer array adds the option of parallel processing for some cmdlets. IIRC Get-Service does not run parallel threads, but it is still much more simple to just provide a string array of PCs.
0
Qlemo"Batchelor", Developer and EE Topic AdvisorCommented:
I've checked our answers again, and see we omitted the necessary -PassThru switch for Set-Service to allow the service object to be passed thru the pipeline for Start-Service.
My suggestion then wouldd be:
Get-Service -Name (gc services.txt) -ComputerName (gc servers.txt) | Set-Service -StartupType Manual -PassThru |  Start-Service

Open in new window

It didn't matter for my code, as Start-Service has been commented out, but of course it is not correct.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.