locked
How to configure SNMP community string and snmp server ip through a script(shell script/power shell/python) for win 2012 server OS RRS feed

  • Question

  • Hi all,

    I need to script to install snmp and configure snmp string and server ip in windows 2012 using powershell.

    I figured out a way to install SNMP  using install windows feature ,but i'm not able to get what needs to be done to add the community string and snmp server ip in windows 2012 using power shell script .

    There's a lot of help and guidance available for earlier windows version but not 2012 servers..

    Would appreciate if someone guides me regarding this as soon as possible.

    PS:Script i'm using currently is as follows:

    #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
    #for Win2008  Add-WindowsFeature SNMP-Services | Out-Null
             #For 2012
             Write-Host "SNMP Service Installing..."
           Get-WindowsFeature -name SNMP* | Install-WindowsFeature -IncludeManagementTools -Restart
           $check = Get-WindowsFeature -Name SNMP-Service

    ##Verify Windows Servcies Are Enabled
    If ($check.Installed -eq "True"){
        Write-Host "Configuring SNMP Services..."
    #Set SNMP Community String(s)- *Read Only*
    Foreach ( $string in $commstring){
     I need help here(Win 2012)  -how to add community string and snmp server ip
    }
    }
    Else {Write-Host "Error: SNMP Services Not Installed"}

    Ashwini

    Tuesday, December 9, 2014 9:37 AM

Answers

  • Here is one. All you need to do is change the variables accordingly. In your case:

    $pmanagers = '10.54.130.12'

    $CommString = 'XYZ'

    Note:  This does not work on Server 2012 R2 servers.  The services names have changed and I haven't taken the time to modify my script yet.
    
    ####################################
    # Script Location: T:\scripts\InstallSNMP.ps1
    # Description: Powershell script to install and configure SNMP Services (SNMP Service, SNMP WMI Provider)
    # Last update: 2014/02/14
    ####################################
    
    #Variables :)
    $pmanagers = @("snmpserver1.company.com","snmpserver2.company.com") # ADD YOUR MANAGER(s) in format @("manager1","manager2")
    $CommString = @("Ninja") # ADD YOUR COMM STRING(s) in format @("Community1","Community2")
    
    #Import ServerManger Module
    Import-Module ServerManager
    
    #Check if SNMP-Service is already installed
    $check = Get-WindowsFeature -Name SNMP-Service
    
    If ($check.Installed -ne "True") {
    #Install/Enable SNMP-Service
    Write-Host "SNMP Service Installing..."
    Get-WindowsFeature -name SNMP* | Add-WindowsFeature -IncludeManagementTools | Out-Null
    }
    
    $check = Get-WindowsFeature -Name SNMP-Service
    
    ##Verify Windows Services Are Enabled
    If ($check.Installed -eq "True"){
    Write-Host "Configuring SNMP Services..."
    #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 /t REG_SZ /d localhost /f | Out-Null
    
    #Set SNMP Traps and SNMP Community String(s) - *Read Only*
    Foreach ($String in $CommString){
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
    # Set the Default value to be null
    reg delete ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /ve /f | Out-Null
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $String /t REG_DWORD /d 4 /f | Out-Null
    $i = 2
    Foreach ($Manager in $PManagers){
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /t REG_SZ /d $manager /f | Out-Null
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /v $i /t REG_SZ /d $manager /f | Out-Null
    $i++
    }
    }
    }
    Else {
    Write-Host "Error: SNMP Services Not Installed"
    }

    Source: http://community.whatsupgold.com/library/powershellscripts/installandconfiguresnmpwithpowershell

    Update; Just ran the above on my 2012 Dev machine, with changed variables as described and it gives the desired output.


    • Edited by Jesper Arnecke Wednesday, December 10, 2014 7:20 AM
    • Marked as answer by k Ashwini Wednesday, December 10, 2014 10:28 AM
    Wednesday, December 10, 2014 6:59 AM

All replies

  • You can add configuration to the registry

    Take a look at SNMP registry reference for more info.

    Remember to restart snmp services after changing the registry


    Gleb.

    Tuesday, December 9, 2014 9:54 AM
  • HI Gleb,

    Could you let me know what exactly need to be done in the registry specially for 2012 server OS..?I'm really new to powershell.

    Also is there anyother way like commands to set string and ip than meddling the registry?

    Appreciate the help in advance..!!

    Thanks,

    Ashwini


    Ashwini

    Tuesday, December 9, 2014 10:00 AM
  • Hiya,

    You just need to create the registry keys using powershell.
    Two examples would be:

    Exact SNMP registry specific settings
    http://winadminnotes.wordpress.com/2011/07/28/how-to-install-snmp-remotely/

    Example of Powershell function to create registry entries using new-ItemProperty
    function Disable-LoopbackCheck {
     $path = 'HKLM:\SYSTEM\CurrentControlSet\Control\Lsa\'
     $name = 'DisableLoopbackCheck'
     $propertyType = 'DWord'
     $value = '00000001'
    
     try {
      New-ItemProperty -Path $path -Name $name -PropertyType $propertyType -Value $value
     }
     catch {
      Set-ItemProperty -Path $path -Name $name -Value $value -PassThru
     }
    }
    



    Tuesday, December 9, 2014 11:00 AM
  • HI Jesper ,

    I dont need to run the script remotely..

    Also sorry I didnt get the example function which you mentioned above.

    How can I add the community string and snmp server Ip with it..Where do i have to incorporate this function in my script?

    All I want is, to know what   code to add so that I can configure  the highlighted things in the image t after installation through scrip

    f


    Ashwini


    • Edited by k Ashwini Wednesday, December 10, 2014 6:36 AM
    Wednesday, December 10, 2014 6:19 AM
  • Here is one. All you need to do is change the variables accordingly. In your case:

    $pmanagers = '10.54.130.12'

    $CommString = 'XYZ'

    Note:  This does not work on Server 2012 R2 servers.  The services names have changed and I haven't taken the time to modify my script yet.
    
    ####################################
    # Script Location: T:\scripts\InstallSNMP.ps1
    # Description: Powershell script to install and configure SNMP Services (SNMP Service, SNMP WMI Provider)
    # Last update: 2014/02/14
    ####################################
    
    #Variables :)
    $pmanagers = @("snmpserver1.company.com","snmpserver2.company.com") # ADD YOUR MANAGER(s) in format @("manager1","manager2")
    $CommString = @("Ninja") # ADD YOUR COMM STRING(s) in format @("Community1","Community2")
    
    #Import ServerManger Module
    Import-Module ServerManager
    
    #Check if SNMP-Service is already installed
    $check = Get-WindowsFeature -Name SNMP-Service
    
    If ($check.Installed -ne "True") {
    #Install/Enable SNMP-Service
    Write-Host "SNMP Service Installing..."
    Get-WindowsFeature -name SNMP* | Add-WindowsFeature -IncludeManagementTools | Out-Null
    }
    
    $check = Get-WindowsFeature -Name SNMP-Service
    
    ##Verify Windows Services Are Enabled
    If ($check.Installed -eq "True"){
    Write-Host "Configuring SNMP Services..."
    #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 /t REG_SZ /d localhost /f | Out-Null
    
    #Set SNMP Traps and SNMP Community String(s) - *Read Only*
    Foreach ($String in $CommString){
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
    # Set the Default value to be null
    reg delete ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /ve /f | Out-Null
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $String /t REG_DWORD /d 4 /f | Out-Null
    $i = 2
    Foreach ($Manager in $PManagers){
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /t REG_SZ /d $manager /f | Out-Null
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /v $i /t REG_SZ /d $manager /f | Out-Null
    $i++
    }
    }
    }
    Else {
    Write-Host "Error: SNMP Services Not Installed"
    }

    Source: http://community.whatsupgold.com/library/powershellscripts/installandconfiguresnmpwithpowershell

    Update; Just ran the above on my 2012 Dev machine, with changed variables as described and it gives the desired output.


    • Edited by Jesper Arnecke Wednesday, December 10, 2014 7:20 AM
    • Marked as answer by k Ashwini Wednesday, December 10, 2014 10:28 AM
    Wednesday, December 10, 2014 6:59 AM
  • HI Jesper,

    Actually i have gone through all this..

    As i already told I want it for win 2012 server ,and this will not work for it as told in the note of the post itself..

    All I want is what changes should i do ,so that it works for 2012


    Ashwini


    • Edited by k Ashwini Wednesday, December 10, 2014 7:12 AM
    Wednesday, December 10, 2014 7:10 AM
  • The above script works on Server 2012, as I described I just tested it. (Not server 2012 R2, not server 2008 R2, exactly server 2012).  So if it's not working, please do give me the error so we can troubleshoot that.

    The script it self does work and does set the two configuration item that you request.

    Wednesday, December 10, 2014 7:56 AM
  • Hi Jesper,

    Thanks for the help,its actually working on both win 2012 and win 2012 r2!!

    Last doubt,will it not work on win2008?

    What changes should be made for win 2008 and win 2008 r2?

    Thanks,

    Ashwini


    Ashwini

    Wednesday, December 10, 2014 10:34 AM
  • Hiya,

    The configuration steps should work. The "reg add" is supported a long way back. I'm not sure the installation of SNMP will work. But you should be able to test that easily if you have a server 2008 and 2008 R2.

    Wednesday, December 10, 2014 11:15 AM
  • Hi Jesper,

    Thanks for the help.

    Ill check with win2008 and let you know..

    Also Could you let me know what exactly the commands reg add; reg add ,del; reg add add  in the last few lines of code are doing..

    I didn't get the concept or logic off it..It could be nice if you could explain it..


    Ashwini

    Thursday, December 11, 2014 5:56 AM
  • Hi Jesper,

    I notice a new problem,the ssecurity ,trap and agent tabs seems to be missing after i run the script in both win 2012 and 2008..This is not happening always..

    This is the script i'm running:

    #Add system to domain
    $_domainCredential = $Host.UI.PromptForCredential("Enter domain credentials", "Enter domain credentials to be used when joining computer to the domain", "", "NetBiosUserName")
    Add-Computer -DomainName 13gest.net -cred $_domainCredential


    #Rename hostname
    $computerName = Get-WmiObject Win32_ComputerSystem
    $name = Read-Host -Prompt "Please Enter the new Computer Name You want"
    $computerName.Rename($name,"dell@123","root") | Out-Null
    #Rename-Computer -NewName $name -DomainCredential root
    start-sleep -s 5
    Write-Host "Host name will be renamed to $name after reboot..."

    Write-Host `n

    #Uninstall-WindowsFeature -Name SNMP-Service -IncludeManagementTools -Restart

    # Description: Powershell script to install and configure SNMP Services (SNMP Service, SNMP WMI Provider)
    #Variables 
    $pmanagers = @("10.94.159.208","10.94.159.205") # ADD SNMP server IP here in format @("10.10.10.10","manager2")
    $CommString = @("t") # ADD YOUR COMM STRING(s) in format @("13gest","public")

    #Import ServerManger Module
    Import-Module ServerManager

    #Check if SNMP-Service is already installed
    $check = Get-WindowsFeature -Name SNMP-Service

    If ($check.Installed -ne "True") {
    #Install/Enable SNMP-Service
    Write-Host "SNMP Service Installing..."
    Get-WindowsFeature -name SNMP* | Add-WindowsFeature | Out-Null
    }

    $check = Get-WindowsFeature -Name SNMP-Service

    ##Verify Windows Services Are Enabled
    If ($check.Installed -eq "True"){
    Write-Host "Configuring SNMP Services..."
    #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 /t REG_SZ /d localhost /f | Out-Null

    #Set SNMP Traps and SNMP Community String(s) - *Read Only*
    Foreach ($String in $CommString){
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
    # Set the Default value to be null
    reg delete ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /ve /f | Out-Null
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\ValidCommunities" /v $String /t REG_DWORD /d 4 /f | Out-Null
    Write-Host "Community string -'$String' set"
    $i = 2
    Foreach ($Manager in $PManagers){
    reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v $i /t REG_SZ /d $manager /f | Out-Null
    reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /v $i /t REG_SZ /d $manager /f | Out-Null
    Write-Host " '$Manager' - SNMP manager added " 
    $i++
    }
    }
    }
    Else {
    Write-Host "Error: SNMP Services Not Installed"
    }

    Write-Host "`n"
     is there anything wrong with the script which is causing it?

    Will be grateful if you could help..


    Ashwini


    • Edited by k Ashwini Monday, December 22, 2014 12:08 PM
    Monday, December 22, 2014 12:06 PM
  • Hiya,

    you should post a separate thread, as the original question has been answered. Makes it easier for search engines to pick up the second question :)

    Monday, December 22, 2014 8:02 PM
  • Hi k Ashwini,

    You must install the RSAT-SNMP windows feature and restart the SNMP Service to view these missing tabs.

    You can install it with this command -> Install-WindowsFeature RSAT-SNMP

    Monday, April 27, 2015 2:43 PM