none
Powershell - SNMP Service Started RRS feed

  • Question

  • Hello -

    I was hoping somebody could help come up with a powershell script. I am a novice when it comes to powershell and am working on teaching myself, as it is an extremely powerful tool.

    I am looking for a script that will basically do the following:

    1.  Get-content from a text file, that will contain a list of servers (one server per line) called servers.txt
    2.  Go through the list of servers, and determine if the SNMP service is running on it.
    3.  If it's running, output a log file (either txt or cvs, preferably in cvs format).

    I would suspect that there would need to be some type of error correcting, in case a server is not online (indicate it is offline), and proceed to the next one.  If it helps the service name is:  "SNMP" and the display name of the service is "SNMP Service".  This way we can avoid using asterisks to pull all services that have SNMP in them.

    If it helps, I'm basically trying to go through all of the servers in my domain that have SNMP installed, and if they don't have it installed, I will most likely work on a script to install it remotely.  For now, I would really like something to tell me if the service is even installed.  I could potentially use this same script for other services down the road, if needed.

    This is probably a really simple one or two line code, but I can't seem to get it to work properly.  I would appreciate any assistance.  I thank you for your help and time.

    Monday, March 17, 2014 7:10 PM

Answers

  • You're right, I did copy it wrong.  I just ran it again, and it completed.  The problem is that the output is still  providing me with the status of all of the services, rather then only the SNMP one.

    Here is the code I used, with the output.

    get-content servers.txt | foreach-object {
      get-wmiobject Win32_Service -filter "Name='SNMP'" -computername $_ |
      select-object __SERVER,Name,Status
    }
    Get-Process | Out-File .\Services.txt

    Why is that last line there?

    EDIT: Here's the way I usually check something like this:

    Get-Content .\pcList.txt | ForEach {
    
        $computerName = $_
    
        Get-Service -ComputerName $computerName -Name 'SNMP' |
            Select @{N='ComputerName';E={ $computerName }},Name,Status,DisplayName
    
    } | Export-Csv .\serviceOutput.csv -NoTypeInformation



    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Edited by Mike Laughlin Monday, March 17, 2014 8:34 PM Add export-csv
    • Marked as answer by Tek_Guy Monday, March 17, 2014 8:52 PM
    Monday, March 17, 2014 8:30 PM
  • Here is a version you can use:


    $serviceName = "SNMP"
    
    get-content "servers.txt" | foreach-object {
      $params = @{
        "Class" = "Win32_Service"
        "ComputerName" = $_
        "ErrorAction" = "SilentlyContinue"
        "Filter" = "Name='$serviceName'"
      }
      $result = get-wmiobject @params |
        select-object __SERVER,Name,State
      if ( $? ) {
        if ( $result ) {
          $result
        }
        else {
          new-object PSObject -property @{
            "__SERVER" = $_
            "Name" = $serviceName
            "State" = "SERVICE DOES NOT EXIST"
          }
        }
      }
      else {
        new-object PSObject -property @{
          "__SERVER" = $_
          "Name" = $serviceName
          "State" = "SERVER NOT ACCESSIBLE"
        }
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Monday, March 17, 2014 8:55 PM
    • Marked as answer by Tek_Guy Monday, March 17, 2014 9:00 PM
    Monday, March 17, 2014 8:51 PM
    Moderator

All replies

  • I can't seem to get it to work properly.

    Can you be more specific? What doesn't work properly?


    -- Bill Stewart [Bill_Stewart]

    Monday, March 17, 2014 7:30 PM
    Moderator
  • One of the 28 variations of scripts I have attempted to build, and get to work.  I didn't want to post it, because I didn't want to cause any confusion (or make myself look foolish) that's why I was looking for somebody to throw one together, so I can build off of that.

    Here is just one variation of the scripts I have attempted, just for laughs:

    $Computers = Get-Content .\servers.txt
    Get-Service "SNMP Service" -computername $Computers | Select name,status,machinename |sort machinename |format-table -autosize
    Get-Process | Out-File .\Services.txt

    The script completes, and even provides me with an output.  The problem as I'm sure you already know is three fold:

    1.  It provides the output, but lists all running services, rather then only the SNMP service I need.
    2.  It outputs the data correctly for only the first server, and not the others in the text file.
    3.  It outputs properly on the command prompt window, but not in the text file.

    I hope this helps and I hope it doesn't cause any confusion.

    Monday, March 17, 2014 7:52 PM
  • Try it this way:


    get-content servers.txt | foreach-object {
      get-wmiobject Win32_Service -filter "Name='SNMP'" -computername $_ |
      select-object __SERVER,Name,Status
    }
    


    -- Bill Stewart [Bill_Stewart]


    Monday, March 17, 2014 8:09 PM
    Moderator
  • Here is what I receive when I try to run it like that:
    =====================================================

    The string starting:
    At C:\Users\edited\Desktop\Powershell_Scripts_In_Progress\SNMP\Query 2\Query
    _SNMP_29.ps1:2 char:39
    +   get-wmiobject Win32_Service -filter  <<<< "Name='SNMP' -computername $_ |
    is missing the terminator: ".
    At C:\Users\Edited\Desktop\Powershell_Scripts_In_Progress\SNMP\Query 2\Query
    _SNMP_29.ps1:4 char:2
    + } <<<<
        + CategoryInfo          : ParserError: (Name=`'SNMP`' -...,Name,Status
    }:String) [], ParseException
        + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString


    • Edited by Tek_Guy Monday, March 17, 2014 8:19 PM
    Monday, March 17, 2014 8:18 PM
  • Also, will this handle error correcting?  Another words if a server in this list is offline, will it continue to go through the list, and output that the machine is offline in the out file?

    Thank you for your help.

    Monday, March 17, 2014 8:21 PM
  • You are missing a terminator.  You copied the code wrong.  Look closely at the error message.

    ¯\_(ツ)_/¯

    Monday, March 17, 2014 8:22 PM
  • You're right, I did copy it wrong.  I just ran it again, and it completed.  The problem is that the output is still  providing me with the status of all of the services, rather then only the SNMP one.

    Here is the code I used, with the output.

    get-content servers.txt | foreach-object {
      get-wmiobject Win32_Service -filter "Name='SNMP'" -computername $_ |
      select-object __SERVER,Name,Status
    }
    Get-Process | Out-File .\Services.txt

    Monday, March 17, 2014 8:27 PM
  • You're right, I did copy it wrong.  I just ran it again, and it completed.  The problem is that the output is still  providing me with the status of all of the services, rather then only the SNMP one.

    Here is the code I used, with the output.

    get-content servers.txt | foreach-object {
      get-wmiobject Win32_Service -filter "Name='SNMP'" -computername $_ |
      select-object __SERVER,Name,Status
    }
    Get-Process | Out-File .\Services.txt

    Why is that last line there?

    EDIT: Here's the way I usually check something like this:

    Get-Content .\pcList.txt | ForEach {
    
        $computerName = $_
    
        Get-Service -ComputerName $computerName -Name 'SNMP' |
            Select @{N='ComputerName';E={ $computerName }},Name,Status,DisplayName
    
    } | Export-Csv .\serviceOutput.csv -NoTypeInformation



    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    • Edited by Mike Laughlin Monday, March 17, 2014 8:34 PM Add export-csv
    • Marked as answer by Tek_Guy Monday, March 17, 2014 8:52 PM
    Monday, March 17, 2014 8:30 PM
  • How else would I output the results, to a text/csv file?
    I apologize for my ignorance, on this matter.

    Monday, March 17, 2014 8:32 PM
  • How else would I output the results, to a text/csv file?
    I apologize for my ignorance, on this matter.

    Get-Process | Out-File blah will run Get-Process and then write that to an output file. It won't do anything with the output from gwmi.

    EDIT: My apologies if that question came off sounding too harsh. Not my intention.


    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Monday, March 17, 2014 8:35 PM
  • This seemed to do the trick, thank you!  Are you able to confirm that it will go through the entire list of servers, and continue even if it's offline, or would I need to add a line like this in there:

    $erroractionpreference = "SilentlyContinue"

    Monday, March 17, 2014 8:42 PM
  • Here is a version you can use:


    $serviceName = "SNMP"
    
    get-content "servers.txt" | foreach-object {
      $params = @{
        "Class" = "Win32_Service"
        "ComputerName" = $_
        "ErrorAction" = "SilentlyContinue"
        "Filter" = "Name='$serviceName'"
      }
      $result = get-wmiobject @params |
        select-object __SERVER,Name,State
      if ( $? ) {
        if ( $result ) {
          $result
        }
        else {
          new-object PSObject -property @{
            "__SERVER" = $_
            "Name" = $serviceName
            "State" = "SERVICE DOES NOT EXIST"
          }
        }
      }
      else {
        new-object PSObject -property @{
          "__SERVER" = $_
          "Name" = $serviceName
          "State" = "SERVER NOT ACCESSIBLE"
        }
      }
    }
    


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by Mike Laughlin Monday, March 17, 2014 8:55 PM
    • Marked as answer by Tek_Guy Monday, March 17, 2014 9:00 PM
    Monday, March 17, 2014 8:51 PM
    Moderator
  • I just ran it against a server I knew was offline, and it kept erroring out on those servers. 

    I threw that line I posted above in the code, and it's bypassing it now.  I kind of wish it would also add the server name and the fact that it was offline as it went through the list, but you're answer resolved my issue, and I thank you all very much for your patience with me.

    Here is the full code that I used in case it is helpful to anybody else.  Thanks again!

    $erroractionpreference = "SilentlyContinue"
    Get-Content .\servers.txt | ForEach {

        $computerName = $_

        Get-Service -ComputerName $computerName -Name 'SNMP' |
            Select @{N='ComputerName';E={ $computerName }},Name,Status,DisplayName

    } | Export-Csv .\services.csv -NoTypeInformation

    Monday, March 17, 2014 8:52 PM
  • I kind of wish it would also add the server name and the fact that it was offline as it went through the list...

    The script in my immediately previous response does exactly that.


    -- Bill Stewart [Bill_Stewart]

    Monday, March 17, 2014 8:54 PM
    Moderator
  •  I kind of wish it would also add the server name and the fact that it was offline as it went through the list

    <snip>

    See Bill's post above, that'll do what you're after.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Monday, March 17, 2014 8:55 PM
  • It sure does, thank you so very much!  I must have written my response before I saw this post.

    You guys have really helped me out, I look forward to learning more as I go.  I'm sure I will have more questions as I go one (lucky you guys hey...)

    Thank you!

    Monday, March 17, 2014 9:00 PM
  • Cheers, you're very welcome. Glad we could help out.

    Don't retire TechNet! - (Don't give up yet - 12,700+ strong and growing)

    Monday, March 17, 2014 10:22 PM