none
Powershell - SNMP Service Install (Server 2008) RRS feed

  • Question

  • Hello, so this is a follow up to the assistance that was provided to me yesterday about checking to see if the SNMP service was installed across multiple servers.  I was able to gather a list of devices that require the SNMP service to be installed, and (thankfully) most of them are Server 2008 machines.  The pre-2008 machines I will just hit manually, since there are less than 20 of them, and to minimize confusion, and a lack of powershell knowledge from my side.

    I have approximately 120 servers that will require the service to be installed onto them, and was looking to do this via powershell.  Luckily, I was able to find this great script online, and I have tested it with my own variables inserted, and it works great (for one server, ran manually on it).  I was wondering if somebody could help me figure out how to get the script to do the following two things:

    1.  Install the SNMP service to multiple servers from a text file (I attempted this with the get-content).
    2.  Output the results to either a text file or a csv file (also tried this as can be seen below).

    Here is the script, with what I tried (and failed).  The only two lines of code that are mine, are the very top one and the very bottom one, I will bold them to indicate that.  Thanks for any help that may be offered.

        #Powershell Script To Install SNMP Services (SNMP Service, SNMP WMI Provider)
         
        Get-Content .\servers.txt
        #Variables :)
        $pmanagers = "MY VARIABLE 1"
        $commstring = "MY VARIABLE 2"
             
        #Import ServerManger Module
        Import-Module ServerManager
         
        #Check If SNMP Services Are Already Installed
        $check = Get-WindowsFeature | Where-Object {$_.Name -eq "SNMP-Services"}
        If ($check.Installed -ne "True") {
                #Install/Enable SNMP Services
                Add-WindowsFeature SNMP-Services | Out-Null
        }
         
        ##Verify Windows Servcies Are Enabled
        If ($check.Installed -eq "True"){
                #Set SNMP Permitted Manager(s) ** WARNING : This will over write current settings **
                reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v 1 /f /t REG_SZ /d localhost | Out-Null
                #Used as counter for incremting permitted managers
                $i = 2
                Foreach ($manager in $pmanagers){
                        reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /f /t REG_SZ /d $manager | Out-Null
                        $i++
                        }
                #Set SNMP Community String(s)- *Read Only*
                Foreach ( $string in $commstring){
                        reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $string /f /t REG_DWORD /d 4 | Out-Null
                        }
        }
        Else {Write-Host "Error: SNMP Services Not Installed"} | Export-Csv .\SNMP_Install.csv -NoTypeInformation

    Wednesday, March 19, 2014 7:53 PM

Answers

  • Alright, so after a couple of weeks I wanted to follow up with everybody on this and let you know what I did.  It was apparent I was too "green" to be able to accomplish this task using Powershell.  Perhaps as my skills develop, I will get better.

    For now, I wanted to let everybody know how I accomplished this task, in case it helps them in the future.  Basically I had somebody in a different thread help me with a powershell script that ran against my list of servers and reported back to me on what machines didn't have the SNMP service running.  I took that list, and then I used the Solarwinds Enabler tool (Free Download) and ran it against the list of servers I had to enable SNMP on (from the script I mentioned above).  I ran the script again, to ensure all servers had SNMP enabled/installed.  I did have a few issues with some 2000/2003 machines and had to install it manually on those, but there were only about 15 or so.  Lastly, I went ahead and enabled the GPO and set the policy how I needed it configured, and pushed it out to all servers.  That was it, SNMP enabled/installed and configured on all required servers.

    It was quite painless, really.  I hope this helps somebody down the road.  I really appreciate everybody's efforts with trying to assist me with this.  I'm sure I will have plenty of other scripts I will need help with in the future.

    Thank again!

    • Marked as answer by Tek_Guy Tuesday, April 1, 2014 2:37 PM
    Tuesday, April 1, 2014 2:36 PM

All replies

  • What is the error? 

    You need to run this remotely?!?

    Is PowerSHell remoting set up an configured on all servers?

    You will need to use a script block that is tested and then run it with "Invoke-Command".  YOU probably should run it as a job since you have many servers.

    A good working knowledge of server software and PowerShell are required,


    ¯\_(ツ)_/¯

    Wednesday, March 19, 2014 10:02 PM
  • Here is the error I receive:

    An empty pipe element is not allowed.
    At C:\Users\EDITED\Desktop\Powershell_Scripts_In_Progress\SNMP_Install\SNMP_
    Install_7.ps1:33 char:61
    +     Else {Write-Host "Error: SNMP Services Not Installed"} | <<<<  Export-Csv
     .\serviceOutput.csv -NoTypeInformation
        + CategoryInfo          : ParserError: (:) [], ParseException
        + FullyQualifiedErrorId : EmptyPipeElement

    This is somehow related to the way I am attempting to output the file, that much I know, but I don't understand why.  The script doesn't complete, so the results are not executed. 

    I have run dozens of scripts remotely, and on the same servers I am attempting to run this particular script on, so I know permissions are not an issue.  I also know that it's not an issue with powershell not being configured to run remotely on these servers.  Unfortunately, I do not have a good working knowledge of powershell (that's why I'm here, trying to learn) however I do have a good working knowledge of server software :-).

    What could the problem be?  It appears that somebody put a great deal of hard work into building the script I listed above, I would be hard pressed to find out that this would be difficult to get to run remotely.  For the task that it has been written for, why would somebody want to login to each server manually and run this script, rather than import a list of servers and do it in minutes. 

    I would prefer to not run this as a job, and I don't know how to script in powershell well at all at this point.  I have been basically finding scripts online (trying to understand how they actually work), googling the heck out of questions that I have, piecing scripts together, or doing simple things with them, when I am not getting help from these forums.

    Any other suggestions, or things I could try?  Thanks for your feedback.

    Thursday, March 20, 2014 1:08 PM
  • What does this have to do with your original question?  Why are you exporting anything?

    You have to be clear and concise. We cannot guess at what you are trying to do.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 1:33 PM
  • I believe all of your output is going to out-null so there's nothing in the pipeline to export.
    Thursday, March 20, 2014 1:47 PM
  • I'm not sure how I can be more clear, this was one of the two main things I am attempting to accomplish with this script.  I am trying to take the script above and do two things with it. 

    1.  Install the SNMP service to multiple servers from a text file.
    2.  Output the results to either a text file or a csv file.

    To answer your question, I would like the results to be exported, so that I can track what servers this was successfully ran on. 

    Thursday, March 20, 2014 2:08 PM
  • Thanks Rhys,

    I am a scripting noob, I'm not quite sure what this means.  Is there another way that I should be attempting to export the results?

    Thursday, March 20, 2014 2:09 PM
  • I think this might work for you, but the error checking is sparse with respect to the configuration part, and the out-null will hide any success or failure messages.

    #Variables :)
    $pmanagers = "MY VARIABLE 1"
    $commstring = "MY VARIABLE 2"
             
    #Import ServerManger Module
    Import-Module ServerManager
    
    $servers = Get-Content .\servers.txt 
    Foreach ($server in $servers) {
         $sess = New-PSSession -computername $server
        #Check If SNMP Services Are Already Installed
        $check = Get-WindowsFeature -computername $server | Where-Object {$_.Name -eq "SNMP-Services"}
        If ($check.Installed -ne "True") {
                #Install/Enable SNMP Services
                invoke-command -session $sess -scriptblock {Add-WindowsFeature SNMP-Services | Out-Null}
        }
         
        ##Verify Windows Servcies Are Enabled
        $check = Get-WindowsFeature -computername $server | Where-Object {$_.Name -eq "SNMP-Services"}
        If ($check.Installed -eq "True"){
                #Set SNMP Permitted Manager(s) ** WARNING : This will over write current settings **
                Invoke-Command -session $sess -scriptblock {reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v 1 /f /t REG_SZ /d localhost | Out-Null }
                #Used as counter for incremting permitted managers
                $i = 2
                Foreach ($manager in $pmanagers){
                       Invoke-command -session $sess -scriptblock {param($i,$manager)reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /f /t REG_SZ /d $manager | Out-Null } -AttributeList $i,$manager
                        $i++
                        }
                #Set SNMP Community String(s)- *Read Only*
                Foreach ( $string in $commstring){
                        Invoke-command -session $sess -scriptblock {param($string)reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $string /f /t REG_DWORD /d 4 | Out-Null } -AttributeList $string
                        }
               Remove-PSSession $sess
               $Result = New-Object PSObject -Property @{Server=$server;Result="SNMP Installed and Configured"}
        }
        Else {
              $Result = New-Object PSObject -Property @{Server=$server;Result="Error: SNMP Services Not Installed"}
        }
        Export-Csv .\SNMP_Install.csv -NoTypeInformation -InputObject $Result
    }
    

    Thursday, March 20, 2014 2:11 PM
  • How can it install on multiple servers.  The CmdLets are not remotable.  You need to use a remoting solution.  Remoting solutions cannot output to a single file and can only output to files on the remote machines.

    You are adding code to a script that does not work.  Get the install script working remotely.  Output the messages to the console and collect them from the jobs.

    An example of remoting that you should try to see if you can remote:

    Invoke-Command -computer <some pc> -ScriptBlock {Get-ChildItem c:\}

    Run this for all computers. If it works then you can move to the next step.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:13 PM
  • you should also note that the settings for SNMP should be done with Group Policy. This can be set up before installing the service. We do not use the registry for this.

    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:15 PM
  • Using GP to do the settings here is all you need for a script.

    $sb={
        Import-Module ServerManager
        if( -not (Get-WindowsFeature SNMP-Services).Installed ){
    Add-WindowsFeature SNMP-Services } } Get-Content .\servers.txt | ForEach-Object{ Invoke-Command -ScriptBlock $sb -ComputerName $_ }

    You can add whatever output lines you need.


    ¯\_(ツ)_/¯


    • Edited by jrv Thursday, March 20, 2014 2:28 PM
    • Proposed as answer by Rhys W Edwards Thursday, March 20, 2014 2:35 PM
    Thursday, March 20, 2014 2:22 PM
  • Correct, the intent is to configure the group policies with the settings we would like.  The goal for this task is to get the SNMP service installed onto the servers (120 of them) where it is not installed onto first.  Once that part is taken care of and verified, the GPO's will be put into place.

    Unfortunately (from what I have researched), you are unable to install the SNMP service via the GPO, otherwise I would be more then happy to do it that way, rather than with Powershell.

    Thursday, March 20, 2014 2:22 PM
  • Thank you for the explanation, and for the example.  I will try this out, and see if I can get this to work.

    Out of curiosity, if this were your task, is this how you would approach handling this?  I'm just looking for what makes the most sense, and since I have been working with powershell lately, I figured this was my best way of accomplishing this task.  If you know of a better way, I am all ears....

    Thursday, March 20, 2014 2:25 PM
  • Thank you Rhys,

    Your script didn't actually work, but I think it's because of what jrv was eluding to below.  I think I have my code messed up.  I appreciate the work you put into this, and it actually taught me something else I was wondering about, so thank you.

    I'll keep digging at this, and see if I can get it to work, otherwise I may have to do this manually....

    Thursday, March 20, 2014 2:28 PM
  • I edited the above code to fix an issue.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:28 PM
  • you can also add the -WhatIf to the code and run it repeatedly until it works as you like.

    Add-WindowsFeature SMTP-Service -WhatIf


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:31 PM
  • Here is a good tutorial on how to use the roles and features installer with PowerShell.

    http://www.petri.co.il/manage-windows-features-with-powershell.htm


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:35 PM
  • I would do it as jrv suggested.  I'm still relatively new to this as well and too often get trapped trying to fix the code that someone's posted rather than offering a better way to do what they need.

    Then someone like jrv comes along and reminds me that writing really good powershell code usually means writing deceptively simple code....

    Thursday, March 20, 2014 2:38 PM
  • I would do it as jrv suggested.  I'm still relatively new to this as well and too often get trapped trying to fix the code that someone's posted rather than offering a better way to do what they need.

    Then someone like jrv comes along and reminds me that writing really good powershell code usually means writing deceptively simple code....

    The code posted cannot be fixed because it is wrong.  It is something cobbled together by someone with little knowledge of Windows or PowerShell.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:47 PM
  • To get a summary of the installer we would use this:

    Get-Content .\servers.txt |
        ForEach-Object{
            $computer=$_
            Invoke-Command -ScriptBlock $sb -ComputerName $_ | 
                ForEach-Object{
                    Add-Member -MemberType NoteProperty -Name Computer -Value $computer -PassThru
                }
        } |
        Export-Csv <file>

    If you are not trained in Windows server deployment or in PowerShell you will likely have a hard time understanding how this has to work.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 2:49 PM
  • here is what the GP settings looks like:

    SNMP Service

    Data collected on: 3/20/2014 10:58:08 AM

    General

    Details

    Domain

    TESTNET.local

    Owner

    TESTNET\Domain Admins

    Created

    3/20/2014 10:53:20 AM

    Modified

    3/20/2014 10:57:54 AM

    User Revisions

    0 (AD), 0 (sysvol)

    Computer Revisions

    3 (AD), 3 (sysvol)

    Unique ID

    {8B82351A-5BF0-47F7-8549-71C2D592FF72}

    GPO Status

    Enabled

    Links

    Location

    Enforced

    Link Status

    Path

    None


    This list only includes links in the domain of the GPO.

    Security Filtering

    The settings in this GPO can only apply to the following groups, users, and computers:

    Name

    NT   AUTHORITY\Authenticated Users

    Delegation

    These groups and users have the specified permission for this GPO

    Name

    Allowed Permissions

    Inherited

    KAHLNET\Domain Admins

    Edit settings, delete,   modify security

    No

    KAHLNET\Enterprise   Admins

    Edit settings, delete,   modify security

    No

    NT   AUTHORITY\Authenticated Users

    Read (from Security   Filtering)

    No

    NT AUTHORITY\ENTERPRISE   DOMAIN CONTROLLERS

    Read

    No

    NT AUTHORITY\SYSTEM

    Edit settings, delete,   modify security

    No

    Computer Configuration (Enabled)

    Policies

    Administrative Templates

    Policy definitions (ADMX files) retrieved from the local machine.

    Network/SNMP

    Policy

    Setting

    Comment

    Communities

    Enabled

          
                                      
         

    Communities

         
         

    public

         
       

    Policy

    Setting

    Comment

    Permitted   Managers

    Enabled

          
                                      
         

    Permitted Managers

         
         

    BUILTIN\Administrators

         
       

    Policy

    Setting

    Comment

    Traps   for public community

    Enabled

          
                                      
         

    Trap configuration

         
         

    test-trap-1

         
       

    User Configuration (Enabled)

    No settings defined.


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 3:04 PM
  • Thank you Rhys,

    Your script didn't actually work, but I think it's because of what jrv was eluding to below.  I think I have my code messed up.  I appreciate the work you put into this, and it actually taught me something else I was wondering about, so thank you.

    I'll keep digging at this, and see if I can get it to work, otherwise I may have to do this manually....

    The code you posted is just a bunch of things pasted together.  It will not get you where you want to go.

    #1 - set Group Policy and be sure it is applied to all servers.  Use new GPO named SNMP Service.  Set managers and communities
    #2 - test remoting code with WhatIF.  Fix all remoting issues
    #3 - Run code to install SNMP.

    Done!


    ¯\_(ツ)_/¯

    Thursday, March 20, 2014 3:10 PM
  • Alright, so after a couple of weeks I wanted to follow up with everybody on this and let you know what I did.  It was apparent I was too "green" to be able to accomplish this task using Powershell.  Perhaps as my skills develop, I will get better.

    For now, I wanted to let everybody know how I accomplished this task, in case it helps them in the future.  Basically I had somebody in a different thread help me with a powershell script that ran against my list of servers and reported back to me on what machines didn't have the SNMP service running.  I took that list, and then I used the Solarwinds Enabler tool (Free Download) and ran it against the list of servers I had to enable SNMP on (from the script I mentioned above).  I ran the script again, to ensure all servers had SNMP enabled/installed.  I did have a few issues with some 2000/2003 machines and had to install it manually on those, but there were only about 15 or so.  Lastly, I went ahead and enabled the GPO and set the policy how I needed it configured, and pushed it out to all servers.  That was it, SNMP enabled/installed and configured on all required servers.

    It was quite painless, really.  I hope this helps somebody down the road.  I really appreciate everybody's efforts with trying to assist me with this.  I'm sure I will have plenty of other scripts I will need help with in the future.

    Thank again!

    • Marked as answer by Tek_Guy Tuesday, April 1, 2014 2:37 PM
    Tuesday, April 1, 2014 2:36 PM
  • Your answer is more information but is different from the original question.

    On W2000 and W2003 the SNMP components are not generally available for unattended install.  You can copy the componentss to the system and trigger a remote install using native methods.

    Yes - much of the problem is alack of knowledge about how Windows works at a deployment and configuration level.  Post-WS2003 we have the tools I posted and we can use a workflow or DSSC to install required components.  Take the time to learn PowerShell. It will make all of this much easier in the future.


    ¯\_(ツ)_/¯

    Tuesday, April 1, 2014 3:19 PM
  • Alright, so after a couple of weeks I wanted to follow up with everybody on this and let you know what I did.  It was apparent I was too "green" to be able to accomplish this task using Powershell.  Perhaps as my skills develop, I will get better.

    For now, I wanted to let everybody know how I accomplished this task, in case it helps them in the future.  Basically I had somebody in a different thread help me with a powershell script that ran against my list of servers and reported back to me on what machines didn't have the SNMP service running.  I took that list, and then I used the Solarwinds Enabler tool (Free Download) and ran it against the list of servers I had to enable SNMP on (from the script I mentioned above).  I ran the script again, to ensure all servers had SNMP enabled/installed.  I did have a few issues with some 2000/2003 machines and had to install it manually on those, but there were only about 15 or so.  Lastly, I went ahead and enabled the GPO and set the policy how I needed it configured, and pushed it out to all servers.  That was it, SNMP enabled/installed and configured on all required servers.

    It was quite painless, really.  I hope this helps somebody down the road.  I really appreciate everybody's efforts with trying to assist me with this.  I'm sure I will have plenty of other scripts I will need help with in the future.

    Thank again!

    PS: Thanks for the post back.


    ¯\_(ツ)_/¯

    Tuesday, April 1, 2014 4:08 PM