{petit casse-tete} requete WMI et filtre avec la commande if then

kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008 - 18 juin 2008 à 17:30
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008 - 19 juin 2008 à 14:13
Bonjour a tous

voila je suis en plein dans la redaction d'un script vbs, je me debrouille mais je ne suis pas un expert.

l'objectif est de lister tous les shares d'un serveur , a l'exception des shares administratifs (style admin$, ipc$, etc ..)

donc, je me dis que je vais faire un select * from win32_share where not name like '%$' dans ma requete WMI

vous pouvez tester, ça ne marche pas sous 2000, et j'ai des serveurs en 2000, mais ça marche nickel sous 2003.

donc, soit je passe tous mes 2000 en 2003 (^^), soit je trouve une astuce.

voici un bout de mon code, en rouge mon astuce qui ne marche pas !

...For Each strComputer In aFileServerLine
    strWMIQuery = "SELECT * FROM Win32_Share"
    Set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set colItems = objWMI.ExecQuery(strWMIQuery)
   

' display ACE
   For Each Share In colItems
      if Share.name <> (share.name & "$") then
            Set objWMIsvc = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
        Set wmiShareSec = objWMIsvc.ExecQuery("select Win32_LogicalShareSecuritySetting.Name='" & Share.Name & "'")
        RetVal = wmiShareSec.GetSecurityDescriptor(wmiSecurityDescriptor)
            DACL = wmiSecurityDescriptor.DACL
...

En gros, a partir du resultat de ma requete (lister tous les shares) je souhaite exclure les noms de share contenant le symbole $

voila messieurs dames, est-il possible de m'aider ?

Merci d'avance !

9 réponses

cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
18 juin 2008 à 19:14
Salut
if Share.name <> (share.name & "$") then
fonctionnera toujours puisque tu lui demande si    123 <> 123S
--> If Share.Name Not Like "$*" Then
sera vrai lorsque le Name ne commencera pas par un dollar $.
Pas pu tester

Vala
Jack, MVP VB
NB : Je ne répondrai pas aux messages privés

<hr />Le savoir est la seule matière qui s'accroit quand on la partage (Socrate)
0
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
18 juin 2008 à 21:25
 Bonsoir,

Sur le principe de l'astuce

If Right(Trim(Share.Name),1) <> "$" Then

jean-marc
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
18 juin 2008 à 22:36
bonsoir,

merci jean-marc, ça a l'air de fonctionner ! je viens de tester sur une copie du code que j'ai emmené chez moi

je ferais le test au travail demain sur des postes en 2000

juste une question :quelle est la signification de  if right(trim(Share.name),1 <>"$" ...

je ne connaissais pas ...

grand merci !
0
cs_Jack Messages postés 14006 Date d'inscription samedi 29 décembre 2001 Statut Modérateur Dernière intervention 28 août 2015 79
18 juin 2008 à 23:33
Re
Right, comme son nom l'indique, isole les xièmes caractères à la droite d'une chaine.
Trim supprime les éventuels espaces devant et derrière la chaine
0

Vous n’avez pas trouvé la réponse que vous recherchez ?

Posez votre question
cs_JMO Messages postés 1854 Date d'inscription jeudi 23 mai 2002 Statut Membre Dernière intervention 24 juin 2018 27
18 juin 2008 à 23:48
Re,

Suppression du Trim superflu.
Config xp

strComputer = "."

strWMIQuery = "SELECT * FROM Win32_Share"
Set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
Set colItems = objWMI.ExecQuery(strWMIQuery)
  
For Each Share In colItems
    If Right(Share.Name,1) <> "$" Then
       MsgBox "Le dernier caractère à droite n'est pas le caractère $",vbExclamation,Share.Name
    Else
       MsgBox "Le dernier caractère à droite est le caractère $",vbCritical,Share.Name
    End If
Next
Set objWMI = Nothing
Set colItems = Nothing

Liens:
http://www.secretswindows.com/index.php?rubrique=scripts&ssrubrique=WMI&ID=161&page=./scripts/wmi/shares.htm
http://support.microsoft.com/kb/889405/fr 

jean-marc
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
19 juin 2008 à 07:59
merci pour vos reponses.

que ne faut-il pas faire pour contourner les imprevus !

tout ça pour exclure les partages administratifs du "* from win32_share"

petite
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
19 juin 2008 à 09:19
bon cela ne fonctionne pas sous 2000

voici le code complet. a partir d'un fichier txt, mon script recupere la liste des shares et affiche les permissions de partage. ce script doit exclure les partages administratifs. ça gonfle sous XP et 2003 ça marche nickel, j'ai juste a faire un filtre au niveau de ma requete WMI

Inputfile = "F:\ListShares\Servers.txt"




' collect computer accounts
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
If objFSO.FileExists(Inputfile) Then
    Set objTextFile = objFSO.OpenTextFile(Inputfile, ForReading)
    Do Until objTextFile.AtEndOfStream
        ReDim Preserve aFileServerLine(i)
        aFileServerLine(i) = objTextFile.ReadLine
        i = i + 1
    Loop
End If
objTextFile.Close






For Each strComputer In aFileServerLine
    strWMIQuery = "select * from win32_share" 'where not name like '%$'
    Set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set colItems = objWMI.ExecQuery(strWMIQuery)






' display ACE
   For Each Share In colItems
    'WScript.Echo "Share Name:" & Share.Name
    'WScript.Echo " Path: " & Share.Path






    'If Right(Trim(Share.Name),1) <> "$" Then
       Set objWMIsvc = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
       Set wmiShareSec = objWMIsvc.get("Win32_LogicalShareSecuritySetting.Name='" & Share.Name & "'")
        RetVal = wmiShareSec.GetSecurityDescriptor(wmiSecurityDescriptor)
           DACL = wmiSecurityDescriptor.DACL
                For Each wmiACE In DACL
                        Set Trustee = wmiACE.Trustee
                        WScript.Echo Share.name
                        WScript.Echo (" Account: " & Trustee.Domain & "" & Trustee.Name)
                        AceType = wmiACE.AceType
                        Select Case Int(wmiACE.AceType)
                                Case 0 PermType = "Allow"
                                Case 1 PermType = "Deny"
                        End Select
                        WScript.Echo (" Permission Type: " & PermType)
                        Select Case Int(wmiACE.AccessMask)
                                Case 1179817 SharePerm = "Read"
                                Case 1245631 SharePerm = "Change"
                                Case 2032127 SharePerm = "Full Control"
                                Case Else SharePerm = "Access Mask " & wmiACE.AccessMask
                        End Select
                        WScript.Echo (" Assigned Permission: " & SharePerm)






               Next
        'end if
   Next












' All done!
WScript.Echo ("Audit Complete!")
Next
0
PCPT Messages postés 13272 Date d'inscription lundi 13 décembre 2004 Statut Membre Dernière intervention 3 février 2018 47
19 juin 2008 à 11:18
salut

euh... il est complet ton code là? et il marche sous win xp et 2003 ?

objTextFile.Close
-> sort d'où ? ah oui forcément rien est déclaré?!!.....

For Each strComputer In aFileServerLine
-> c'est une syntaxe de collection, pas de tableau, non?

je fais le teste sous WIN2000, là j'ai les bons noms de partage. par contre DACL est NULL

Dim Inputfile
Dim objFSO
Dim objTextFile
Dim i
Dim aFileServerLine()
Dim strComputer
Dim strWMIQuery
Dim objWMI
Dim colItems
Dim Share
Dim objWMIsvc
Dim wmiShareSec
Dim RetVal
Dim wmiSecurityDescriptor
Dim DACL
Dim wmiACE
Dim Trustee
Dim AceType
Dim PermType
Dim SharePerm

Inputfile = "C:\Servers.txt"

' collect
computer accounts
Set objFSO = CreateObject("Scripting.FileSystemObject")
Const ForReading = 1
If objFSO.FileExists(Inputfile) Then
    Set objTextFile = objFSO.OpenTextFile(Inputfile,
ForReading)
    Do Until objTextFile.AtEndOfStream
        ReDim Preserve aFileServerLine(i)
        aFileServerLine(i) = objTextFile.ReadLine
        i = i + 1
    Loop
End If
objTextFile.Close

'On Error Resume Next '*VB6....
'For Each
strComputer In aFileServerLine
For i = 0 To UBound(aFileServerLine)
    strComputer = aFileServerLine(i)

    strWMIQuery = "select * from
win32_share" 'where not name like
'%$'
    Set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
    Set colItems = objWMI.ExecQuery(strWMIQuery)

' display ACE
   For Each Share In colItems
    'WScript.Echo "Share Name:" & Share.Name
    'WScript.Echo " Path: " &
Share.Path

    If Right(Trim(Share.Name), 1) <> "$" Then
       Set objWMIsvc = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
       Set wmiShareSec = objWMIsvc.get("Win32_LogicalShareSecuritySetting.Name='" & Share.Name & "'")
        RetVal = wmiShareSec.GetSecurityDescriptor(wmiSecurityDescriptor)
           DACL = wmiSecurityDescriptor.DACL
                For Each wmiACE In DACL
                        Set Trustee = wmiACE.Trustee
                        WScript.Echo Share.Name
                        WScript.Echo " Account: " & Trustee.Domain & "" & Trustee.Name
                        AceType = wmiACE.AceType
                        Select Case Int(wmiACE.AceType)
                                Case 0: PermType = "Allow"
                                Case 1: PermType = "Deny"
                        End Select
                        WScript.Echo " Permission Type:
" & PermType
                        Select Case Int(wmiACE.AccessMask)
                                Case 1179817: SharePerm = "Read"
                                Case 1245631: SharePerm = "Change"
                                Case 2032127: SharePerm = "Full
Control"
                                Case Else: SharePerm = "Access Mask " & wmiACE.AccessMask
                        End Select
                        WScript.Echo " Assigned Permission:
" & SharePerm

               Next
        End If
   Next

' All done!
WScript.Echo "Audit
Complete!"
Next

par contre je n'ai même pas le nom de partage sous Win2003

à tester....
<hr size="2" width="100%" />Prenez un instant pour répondre à [infomsg_SONDAGE-POP3-POUR-CS_769706.aspx ce sondage] svp
0
kerberos69 Messages postés 21 Date d'inscription mercredi 6 avril 2005 Statut Membre Dernière intervention 9 juillet 2008
19 juin 2008 à 14:13
probleme contourné, je fais un select * from win32_share where type ='0'

en revanche j'ai une erreur sur la partie en rouge

' shares sur serveurs et NTFS permissions






InputFile = "F:\Docs\Professionnal Docs\Boiron\Scripts\SECBOI\ListShares\Servers.txt"





Const ForReading = 1
Const ForWriting = 2





Set objFSO = CreateObject("Scripting.FileSystemObject")
        If objFSO.FileExists(InputFile) Then
        Set objTextFile = objFSO.OpenTextFile(Inputfile,ForReading)






        Do Until objTextFile.AtEndOfStream
                Redim Preserve aFileServerLine(i)
                aFileServerLine(i) = objTextFile.ReadLine
                i = i + 1
        Loop
        End If
       
objTextFile.Close






For Each strComputer in aFileServerLine        strWMIQuery "SELECT * FROM Win32_Share where type'0'"
        Set objWMI = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
        Set colItems = objWMI.ExecQuery(strWMIQuery)
 
        For Each Share In colItems






        Set objDictionary = CreateObject ("Scripting.Dictionary")
       objDictionary.Add "Share1", share.name
 






        Next
Next








'-----------------------------------------------'
'Initialisation du tableau EXCEL'
'-----------------------------------------------'






Set oXL = WScript.CreateObject("EXCEL.application")
oXL.Visible = True
oXL.screenupdating=true
oXL.Workbooks.Add
oXL.Cells.Select
With oXL.Selection.Font
    .Name = "Tahoma"
    .Size = 8
End With






oXL.Cells(1,1).Value = "Liste des Partages"
oXL.Range("A1").Select
With oXL.Selection.Font
        .Name = "Tahoma"
        .Size = 12
        .Bold = True
End With






NL=2






oXL.Cells(NL,1).Value = "SERVEUR"
oXL.Cells(NL,2).Value = "Nom du partage"
oXL.Cells(NL,3).Value = "Droits du partage"
oXL.Cells(NL,4).Value = "Acces NTFS"
oXL.Cells(NL,5).Value = "Droits NTFS"






oXL.Range("A2:E2").Select
oXL.Selection.Font.Bold = True
oXL.Selection.Interior.ColorIndex = 37








'-----------------------------------------------'
'-----------------------------------------------'
'-----------------------------------------------'
'-----------------------------------------------'








' display ACE
Set objWMIsvc = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
For Each Item In objDictionary







       Set objWMIsvc = GetObject("winmgmts:\" & strComputer & "\root\cimv2")
       Set wmiShareSec = objWMIsvc.get("Win32_LogicalShareSecuritySetting.Name='" & Share.Name & "'")
        RetVal = wmiShareSec.GetSecurityDescriptor(wmiSecurityDescriptor)
           DACL = wmiSecurityDescriptor.DACL
                For Each wmiACE In DACL
                        Set Trustee = wmiACE.Trustee
                        WScript.Echo Share.name
                        WScript.Echo (" Account: " & Trustee.Domain & "" & Trustee.Name)
                        AceType = wmiACE.AceType
                        Select Case Int(wmiACE.AceType)
                                Case 0 PermType = "Allow"
                                Case 1 PermType = "Deny"
                        End Select
                        WScript.Echo (" Permission Type: " & PermType)
                        Select Case Int(wmiACE.AccessMask)
                                Case 1179817 SharePerm = "Read"
                                Case 1245631 SharePerm = "Change"
                                Case 2032127 SharePerm = "Full Control"
                                Case Else SharePerm = "Access Mask " & wmiACE.AccessMask
                        End Select
                        WScript.Echo (" Assigned Permission: " & SharePerm)






               Next
    'end if
Next






WScript.Echo ("Audit Complete!")
'Next






pfiou mon cerveau chauffe trop par ce beau soleil
0
Rejoignez-nous