{petit casse-tete} requete WMI et filtre avec la commande if then
kerberos69
Messages postés21Date d'inscriptionmercredi 6 avril 2005StatutMembreDernière intervention 9 juillet 2008
-
18 juin 2008 à 17:30
kerberos69
Messages postés21Date d'inscriptionmercredi 6 avril 2005StatutMembreDerniè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 !
A voir également:
{petit casse-tete} requete WMI et filtre avec la commande if then
cs_Jack
Messages postés14006Date d'inscriptionsamedi 29 décembre 2001StatutModérateurDernière intervention28 août 201579 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)
cs_Jack
Messages postés14006Date d'inscriptionsamedi 29 décembre 2001StatutModérateurDernière intervention28 août 201579 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
Vous n’avez pas trouvé la réponse que vous recherchez ?
cs_JMO
Messages postés1854Date d'inscriptionjeudi 23 mai 2002StatutMembreDernière intervention24 juin 201827 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
kerberos69
Messages postés21Date d'inscriptionmercredi 6 avril 2005StatutMembreDerniè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)
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
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