Link to home
Start Free TrialLog in
Avatar of SSR-IS
SSR-ISFlag for Germany

asked on

Remote activation of 2008 Feature "SNMP" via powershell

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
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image


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
Avatar of SSR-IS

ASKER

Uh, no and not really...

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
Avatar of SSR-IS

ASKER

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
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of SSR-IS

ASKER

Okay, I´ll give this a try. Is it possible to set SNMP-Settings this way?

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
Avatar of SSR-IS

ASKER

Ah okay. The settings can be set via registry.
Could you give me short code example how to manipulate several registry settings by script?

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

Avatar of SSR-IS

ASKER

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...

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
Avatar of SSR-IS

ASKER

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...
Avatar of SSR-IS

ASKER

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?

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
Avatar of SSR-IS

ASKER

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?

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
Avatar of SSR-IS

ASKER

Okay.

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

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
Avatar of SSR-IS

ASKER

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


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
Avatar of SSR-IS

ASKER

Okay, I see.
I´m at home now and will give it a try tomorrow morining. Thanks!
Avatar of SSR-IS

ASKER

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?

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
Avatar of SSR-IS

ASKER

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!