locked
PowerShell: Query service and Set to disabled. RRS feed

  • Question

  • Hello, 

    Forgive my ignorance as a newbie on the subject. I'll try to keep it brief.  
    How do I write a PS script to query the spooler service and if the startuptype is set to automatic, set the service to disabled. 

    Get startuptype: 
    $WMI=Get-WmiObject -Class  Win32_Service | Where-Object Name -like Spooler |Select-Object -Property  Name, StartMode
    Ok, I get that.

    Set to disable: 
    $Set=Set-Service -Name Spooler -StartupType Disabled
    Ok, I get that too...

    So how do I tie it together? 
    How if $WMI is set to automatic then run $Set to disable as a one liner? 





    • Edited by RalphMBride Tuesday, October 29, 2019 10:34 PM
    Tuesday, October 29, 2019 10:33 PM

All replies

  •  Get-Service spooler | Set-Service  -StartupType Disabled

    The spooler is set to auto by default and setting it to disabled if it is di\sabled will have no inpact.

    Also you can just use the set,

    You can check this lke this:

     Get-Service spooler | Select StartType


    \_(ツ)_/


    • Edited by jrv Tuesday, October 29, 2019 10:39 PM
    Tuesday, October 29, 2019 10:35 PM
  • I guess what I'm looking for is a correct use of the "If then" usage of PS. 

    IF spooler is automatic THEN set to disabled.

    I just don't know how to tie it together in this case. 
    Tuesday, October 29, 2019 10:45 PM
  • Why does that matter? If you need it disable then just disable it.  If it I already disabled disabling it will not change anything.  It will still ne disabled.


    \_(ツ)_/

    Tuesday, October 29, 2019 10:49 PM
  • I am setting up a desired state configuration in SCCM.  There has to be a query first in the first script "and if then" a certain criteria is met run the second script.  

    The second script needs a If\Then statement properly written. 
    Tuesday, October 29, 2019 11:04 PM
  • I am setting up a desired state configuration in SCCM.  There has to be a query first in the first script "and if then" a certain criteria is met run the second script.  

    The second script needs a If\Then statement properly written. 

    So this is not a scripting question but it is a DSC issue.

    What you are trying to do in the DSC is still a mystery, 

    To test a service state just do the following

    Get-Service spooler | Where-Object{$_.StartupType -eq 'Automatic'}

    The results will be either true or false.

    Before trying to use DSC you must learn basic PowerShell or you will pretty much get nowhere and waste a lot of your valuable time.

    You could start by learning enough PS to learn how to use help.

    help about_if

    When in doubt about how to use PowerShell basic language features just use the help command.

    help about_language_keywords

    These are some of the many things you will learn in the first chapter of any good course or book.


    \_(ツ)_/

    Tuesday, October 29, 2019 11:37 PM
  •  SCCM has the selection field to specify the power shell command to be used. In this case one with the use of the IF\THEN command parameters. You are correct “you must learn basic PowerShell or you will pretty much get nowhere and waste a lot of your valuable time.”—-Thus my statement of “Forgive my ignorance as a newbie on the subject.” as I am looking for direction of using the IF\THEN option.  
    Wednesday, October 30, 2019 12:24 AM
  • There is no if/then in PowerShell. I suspect you are referring to some aspect of SCCM. This is not an SCCM forum so I suggest you start there and get more information on SCCM and how to use it.

    In most languages and systems if/else type constructs are used to detect the logical value of an expression and then perform a task based on that value.  How this relates to SCM is a mystery that the SCCM people can help you sort out.

    Open a post in the SCCM forum to get help with your issue.

    You should read the following link before posting in another forum as it will help you to ask aa better question:


    \_(ツ)_/

    Wednesday, October 30, 2019 12:33 AM