Solved

Remote activation of 2008 Feature "SNMP" via powershell

Posted on 2010-11-23
24
1,512 Views
Last Modified: 2012-05-10
Hi,

Í´m looking for a opportunity to use some powershell commands regarding SNMP to activate SNMP Feature on 2008 Servers. Servername will be provided by an input-txt-file.

I want to use add-windowsfeature SNMP-Services and need to customize the SNMP-Setting even also.

Anyone did this before and could provide me with some code?

Regards
Steffen
0
Comment
Question by:SSR-IS
  • 13
  • 11
24 Comments
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34195518

It could be easy... but...

You'll need to use Windows Remoting because you need to execute the command you have there as if you were logged onto that machine.

Have you configured remoting? Have you even heard of it? :)

Chris
0
 

Author Comment

by:SSR-IS
ID: 34195535
Uh, no and not really...
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34195602

Not to worry, it's not exactly obvious.

Okay so, we have a few choices:

1. Set up Windows Remoting (can be done in Group Policy, although this isn't exactly going to take effect quickly)
2. We can use a tool like PSExec. Have you used that before?

Chris
0
 

Author Comment

by:SSR-IS
ID: 34195924
This GPO thing will become difficult, I have no rights to edit or create GPOs. So I would rather use PSExec - which I used sometimes before
0
 
LVL 70

Accepted Solution

by:
Chris Dent earned 500 total points
ID: 34195944

In that case this command should work (if you can PSExec it):

PowerShell.exe -command "Import-Module ServerManager; Add-WindowsFeature SNMP-Services"

Just watch out for the quotes if PSExec needs you to quote everything we have here.

Chris
0
 

Author Comment

by:SSR-IS
ID: 34196000
Okay, I´ll give this a try. Is it possible to set SNMP-Settings this way?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34196014

I'm not sure I'm afraid, I don't use it here and I'm not sure where it hides it's settings. How would you normally set them?

Chris
0
 

Author Comment

by:SSR-IS
ID: 34196042
Ah okay. The settings can be set via registry.
Could you give me short code example how to manipulate several registry settings by script?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34196061

Registry settings... local stuff is dead easy, you can get at them like this:

Get-Item HKLM:\Software\Somewhere\Whatever

Then Set-ItemProperty if you want to set or add something.

Alas, remotely is more difficult. I have a function... Used like this:
Set-RegistryValue -Key software\somewhere\whatever -Name "SomeSetting" -Value "SomeValue"

Open in new window

Chris
Function Set-RegistryValue
{
  <#
    .Synopsis
      Sets an arbitrary registry value.
    .Description
      Set-RegistryValue attempts to write the specified values to the registry.
    .Parameter Key
      A registry key relative to the Hive.
    .Parameter Name
      Specifies the name of the registry value to udpate.
    .Parameter Value
      Specifies the value to set.
    .Parameter Type
      Specifies the value type. By default this function writes a String type.
    .Parameter Hive
      The Registry Hive to add the key to. The default value is LocalMachine.
    .Parameter Computer
      The target system, by default the change is made on the local machine.
  #>

  [CmdLetBinding()]
  Param(
    [Parameter(Mandatory = $True, Position = 0, HelpMessage = "Enter a registry key (relative to Hive)")]
    [String]$Key,
    [Parameter(Mandatory = $True, Position = 1, HelpMessage = "Enter a value name")]
    [String]$Name,
    [Parameter(Mandatory = $True, Position = 2, HelpMessage = "Specify a value to set")]
    [Object]$Value,
    [Microsoft.Win32.RegistryValueKind]$Type = "String",
    [Microsoft.Win32.RegistryHive]$Hive = "LocalMachine",
    [String]$ComputerName = $Env:ComputerName
  )

  Write-Verbose "Attempting to set value on $ComputerName"

  Try {
    $BaseKey = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey($Hive, $ComputerName)
  }
  Catch [IO.IOException]
  {
    Write-Error "Unable to connect to $($ComputerName): $($Error[0].Exception.InnerException.Message.TrimEnd())"
  }

  If ($BaseKey -ne $Null)
  {
    Try {
      # Get the current key or open an existing key for write access
      $RegistryKey = $BaseKey.CreateSubKey($Key)
    }
    Catch [UnauthorizedAccessException]
    {
      Write-Error "Access Denied: The script was unable to set $Name on $ComputerName."
    }
  }

  If ($RegistryKey -ne $Null)
  {
    $RegistryKey.SetValue($Name, $Value, $Type)
  }
}

Open in new window

0
 

Author Comment

by:SSR-IS
ID: 34196099
Okay, thanks again. I need to check this out later.
I have a lot of stuff to do... You remember my Powershell question from yesterday? The one with OS, SNMP and local Admin query? I need to check and install SNMP on 150 different server, with 2003 or 2008 installed and I need to add a special user to local admin group...

So I´m trying to get some scripts together...
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34196201

Aye, I do remember :)

The addition of the user to the group is, fortunately, nice and simple:

([ADSI]"WinNT://$ComputerName/Administrators").Add("WinNT://$Env:UserDomain/testuser")

Will this be based on the output from the previous script?

Chris
0
 

Author Comment

by:SSR-IS
ID: 34196235
Yes it will. I did an Excel import to see on what hosts the user needs to be added. Maybe we should have added this to yesterdays script?

I just managed to install and configure SNMP on the 2003 machines using sysocmgr.exe. I just have to figure out how to use this tool together with PSExec...
0
Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

 

Author Comment

by:SSR-IS
ID: 34196816
I´m sorry for coming back with this:
I can not get psexec to run together with sysocmgr.exe. Do you know the correct usage of both together?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34196822

I don't I'm afraid. Feel free to post the syntax you're using, I don't use either, but I can do sanity checking :)

Chris
0
 

Author Comment

by:SSR-IS
ID: 34197815
No problem. I quit the work on SNMP, sysocmgr and psexec for today and went to our 2008 servers.

the installation and configuration via powershell and psexec works fine. There one thing which is a bit strange. the psexec console never comes back after executing the powershell commands. I always have to quit it manually. This turned out to be a problem when I tried to use the @File Option from psexec.

Have you ever had this problem?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34197972

Afraid not, my desktop guy uses PSExec a lot, but I use remoting or WMI since I have remoting set up on my servers.

Chris
0
 

Author Comment

by:SSR-IS
ID: 34198176
Okay.

Is it a big deal to activate ps remoting on our servers? Maybe this would be a better approach...
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34198357

Not particularly, Group Policy can do it :)

If not that, make sure PowerShell 2 is deployed, then make them run this on them (PsExec will do):

winrm qc -q

Where:

winrm = Windows Remoting
qc = Quick Config
-q = Quiet

Then you can do fun things:

New-PsSession -ComputerName someserver | Enter-PsSession

That gives you a PowerShell prompt on that machine (interactive). Or you can just execute a command against it:

Invoke-Command { Get-ChildItem C:\ } -Computer someserver

Roughly equivalent to what we've been doing with PsExec here.

Chris
0
 

Author Comment

by:SSR-IS
ID: 34198416
Hm, I´ll have to think about this.

With your User-Add command, I get an error:
Exception calling "Add" with "1" argument(s): "An invalid directory pathname was passed
"
At W:\ps\2008inst\addadmin2.ps1:2 char:51
+ ([ADSI]"WinNT://$ComputerName/Administrators").Add <<<< ("WinNT://$Env:DOMAIN_ADS/Testuser")
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34198464

Sorry, I should have explained:

$Env:UserDomain is an environmental variable, run this and you'll see the value it stores:
Write-Host $Env:UserDomain

Open in new window

You should see it comes back with your domain name automatically. You can hard-code the domain name like this:
$ComputerName = "SomeTestComputer"
([ADSI]"WinNT://$ComputerName/Administrators").Add("WinNT://DOMAIN-NAME/Testuser")

Open in new window

Chris
0
 

Author Comment

by:SSR-IS
ID: 34198885
Okay, I see.
I´m at home now and will give it a try tomorrow morining. Thanks!
0
 

Author Comment

by:SSR-IS
ID: 34206935
Hi Chris,

sorry for coming back that late, I had a lot of stuff to do today...
The little Admin-User-Script works fine now!

Do you mind me coming up with another question regarding the SNMP install?
I´ve done a few 2008 R2 Servers with your script - very fine so far. Now I wanted to run it against 2008 servers (not R2) and then I noticed that the servermanager module is not available in 2008.

Is there a chance to use powershell in that case for installing SNMP?
0
 
LVL 70

Expert Comment

by:Chris Dent
ID: 34207930

You can do it with ocsetup or whatever it's called on 2008 (regular) can't you?

Right now all we're doing is passing an arbitrary command off to another system, there's no reason that must be a PowerShell command.

Chris
0
 

Author Comment

by:SSR-IS
ID: 34208805
I found out that a tool called servermanagercmd.exe is present on all 2008 Server. You can install features and services with it.

Thanks for you hard work with me ;-)

I guess I close this question, pass the 500 hard earned points to you and if neccessary, I´ll open up a new one!

Thanks Chirs!
0

Featured Post

Are end users causing IT problems again?

You’ve taken the time to design and update all your end user’s email signatures, only to find out they’re messing up the HTML, changing the font and ruining the imagery. What can you do to prevent this? Find out how you can save your signatures from end users today.

Join & Write a Comment

Know what services you can and cannot, should and should not combine on your server.
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video discusses moving either the default database or any database to a new volume.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

708 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now