Cambiar permisos WMI mediante script

Seguro que alguna vez os habéis enfrentado a la tarea de abrir wmimgmt.msc y cambiar la seguridad. El procedimiento no es difícil, y está documentado en perfecto castellano (ver artículo más abajo), pero si esto lo tenemos que repetir en cientos de equipos, la cosa se vuelve más tediosa.

https://support.microsoft.com/kb/325353/es

Para facilitar esta tarea utilizaremos el script de ejemplo siguiente:

 

'==================================================

'= =

'= Hardcoded Namespace security script generator =

'= =

'==================================================

namespace = InputBox("Create hardcoded Namespace security script for what namespace?" & vbcrlf & vbcrlf & "For example..." & vbcrlf & "root" & vbcrlf & "root\default")

DumpSD(namespace)

Sub DumpSD(strNamespace)

Set objSecurity=GetObject("WinMgmts:{impersonationLevel=impersonate}!" & strNamespace & ":__SystemSecurity=@")

ret = objSecurity.GetSD(strSD)

if ret > 0 then

wscript.echo "Failed to get Namespace Security : Return Code " & ret

wscript.quit

end if

Const ForReading = 1, ForWriting = 2, ForAppending = 8

Dim fso, f

Set fso = CreateObject("Scripting.FileSystemObject")

Set f = fso.OpenTextFile("SetNamespaceSDScript_vbs.txt", ForWriting, True)

f.Writeline ""

f.writeline "Set objSecurity=GetObject(""WinMgmts:{impersonationLevel=impersonate}!" & strNamespace & ":__SystemSecurity=@"")"

f.Writeline ""

init = false

text = "strSD = Array("

start = 1

for each byteval in strSD

if init = true then

text = text & "," & byteval

else

text = text & byteval

init = true

end if

next

text = text & ")" & vbcrlf

f.Writeline text

f.writeline "ret = objSecurity.SetSD(strSD)" & vbcrlf

f.writeline "wscript.Echo ""__SystemSecurity.SetSD() completed : Return Code = "" & ret" & vbcrlf

f.Close

end sub

'Disclaimer

'The sample scripts are not supported under any Microsoft standard support program or service. 'The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims 'all implied warranties including, without limitation, any implied warranties of merchantability or 'of fitness for a particular purpose. The entire risk arising out of the use or performance of the 'sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or 'anyone else involved in the creation, production, or delivery of the scripts be liable for any 'damages whatsoever (including, without limitation, damages for loss of business profits, 'business interruption, loss of business information, or other pecuniary loss) arising out of the use 'of or inability to use the sample scripts or documentation, even if Microsoft has been advised of 'the possibility of such damages.

Pasos a seguir

1.- Configurar manualmente un equipo con el procedimiento habitual

CÓMO: Configurar la seguridad del espacio de nombres WMI en Windows Server 2003

2.- Crear un vbs en el equipo conteniendo el código del script de ejemplo

3.- Desde una ventana de comandos ejecutaremos lo siguiente “cscript permwmi_vbs.vbs”

Una vez le suministremos el nombre namespace del que vamos a copiar los permisos, este generará un fichero de salida llamado SetNamespaceSDScript_vbs.txt

clip_image002

Ejemplo de SetNamespaceSDScript_vbs.txt generado:

Set objSecurity=GetObject("WinMgmts:{impersonationLevel=impersonate}!root:__SystemSecurity=@")

strSD = Array(1,0,4,129,148,0,0,0,164,0,0,0,0,0,0,0,20,0,0,0,2,0,128,0,5,0,0,0,0,2,24,

0,63,0,6,0,1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0,0,2,20,0,19,0,0,0,1,1,0,0,0,0,0,1,0,0,0,0,0,

2,20,0,19,0,0,0,1,1,0,0,0,0,0,5,19,0,0,0,0,2,20,0,19,0,0,0,1,1,0,0,0,0,0,5,20,0,0,0,0,0,

36,0,1,0,0,0,1,5,0,0,0,0,0,5,21,0,0,0,113,186,132,153,107,218,182,220,46,52,126,188,

242,3,0,0,1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0,1,2,0,0,0,0,0,5,32,0,0,0,32,2,0,0)

ret = objSecurity.SetSD(strSD)

wscript.Echo "__SystemSecurity.SetSD() completed : Return Code = " & ret

4.- Ya con el fichero anterior, solo nos faltaría renombrar el fichero a .vbs, distribuirlo y ejecutarlo en los equipos donde deseemos cambiar los permisos.

 

¿Fácil no?

- Paloma García Martín

Soporte Microsoft Premier