SNMP安装和配置递归脚本

时间:2019-05-02 09:53:18

标签: powershell

我必须在许多服务器上安装和配置SNMP服务,并且我已经准备了一个powershell脚本来通过从文本文件中获取各种服务器的名称来递归地执行操作。显然,该脚本可以工作,但是除了从中启动该脚本的服务器以外,没有其他人可以安装该服务。我附上了我使用的脚本,并请了解我以外的人告诉我错误。预先感谢。

$pollers = @("XXX.XXX.XXX.XXX") # indirizzi ip Pollers (IP o DNS name) - esempio: @("monitorserv1","10.10.5.2")
 $CommunityStr = @("NOME_COMMUNITY") # nome community - esempio:  @("Secretcommunity","private2")

 Import-Module ServerManager

 #foreach ($ServerName in Get-Content .\Listaservers.txt){

    Enter-PSSession -ComputerName $ServerName

 #testo se SNMP VMI è installato
 $test = Get-WindowsFeature -ComputerName $ServerName -Name SNMP-Service

 #Installa/abilita SNMP-Service se il test precedente fallisce
 If ($test.Installed -ne "True") {
 Write-Host "Installo SNMP-Service su $ServerName..."
 Get-WindowsFeature -name SNMP* | Add-WindowsFeature -IncludeManagementTools | Out-Null
 }

 #ri-testo se SNMP-Service Feature è abilitato ed aggiorno le variabili
 $test = Get-WindowsFeature -Name SNMP-Service

 #scrivo le chiavi di registro se SNMP-Service è attivo
 If ($test.Installed -eq "True"){
 Write-Host "Configuro SNMP-Services con la stringa Community e gli indirizzi pollers"
 #Setto SNMP Permitted Manager(s) ** ATTENZIONE : questa operazione sovrascrive i parametri attuali **
  reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v 1 /t REG_SZ /d localhost /f | Out-Null

 #Setto SNMP Traps e SNMP Community in *Read Only*
 Foreach ($String in $CommunityStr){
 reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
 # Setto la Default value a 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 $pollers){
 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++
 }
 }
 }
 #riavvio il servizio dopo averlo configurato
 Restart-Service "SNMP"
{
 Write-Host "Configurazione completata"
 }

1 个答案:

答案 0 :(得分:0)

它仅在本地计算机上有效,因为在Enter-pssession之后,远程会话无法访问您在本地定义的变量。

我已经编辑了您的脚本,更改了远程执行方法并使用$using:variable提取了局部变量。

注意:我没有测试安装和配置SNMP的实际脚本逻辑。

$pollers = @("XXX.XXX.XXX.XXX") # indirizzi ip Pollers (IP o DNS name) - esempio: @("monitorserv1","10.10.5.2")
$CommunityStr = @("NOME_COMMUNITY") # nome community - esempio:  @("Secretcommunity","private2")

$installSNMPsb = { 
     Import-Module ServerManager

     #testo se SNMP VMI è installato
     $test = Get-WindowsFeature -ComputerName $ServerName -Name SNMP-Service

     #Installa/abilita SNMP-Service se il test precedente fallisce
     If ($test.Installed -ne "True") {
         Write-Host "Installo SNMP-Service su $ServerName..."
         Get-WindowsFeature -name SNMP* | Add-WindowsFeature -IncludeManagementTools | Out-Null
     }

     #ri-testo se SNMP-Service Feature è abilitato ed aggiorno le variabili
     $test = Get-WindowsFeature -Name SNMP-Service

     #scrivo le chiavi di registro se SNMP-Service è attivo
     If ($test.Installed -eq "True"){
         Write-Host "Configuro SNMP-Services con la stringa Community e gli indirizzi pollers"
         #Setto SNMP Permitted Manager(s) ** ATTENZIONE : questa operazione sovrascrive i parametri attuali **
          reg add "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\PermittedManagers" /v 1 /t REG_SZ /d localhost /f | Out-Null

         #Setto SNMP Traps e SNMP Community in *Read Only*
         Foreach ($String in $using:CommunityStr){
             reg add ("HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\services\SNMP\Parameters\TrapConfiguration\" + $String) /f | Out-Null
             # Setto la Default value a 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 $using:pollers){
                 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++
             }
         }
     }
     #riavvio il servizio dopo averlo configurato
     Restart-Service "SNMP"
     Write-Host "Configurazione completata"
}

 foreach ($ServerName in $(Get-Content .\Listaservers.txt)){
    invoke-command -ScriptBlock $installSNMPsb -ComputerName $ServerName
}