Overblog
Editer l'article Suivre ce blog Administration + Créer mon blog

Powershell: running command from windows console

Publié le par damcuvelier

 

 

start: cmd

C:\> powershell yourcommand

 

for example:

 

C:\>powershell dsquery user -samid username | dsget user -memberof | dsget group –samid

 

To find all groups of a user is memberof without the DN's

(http://msdn.microsoft.com/en-us/library/dd835506%28VS.85%29.aspx)

so usefull in vbscript:

 

to check if the current user is member of a group:

 

vbs code:

 

Dim oShell, oEnv, oFSO, here
Set oShell = CreateObject("WScript.Shell")
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oEnv = oShell.Environment("PROCESS")

here = left(WScript.ScriptFullName,len(WScript.ScriptFullName)-len(WScript.ScriptName)-1)

'Retrieve current username:
Dim vuser
vuser = oEnv("%USERNAME%”)

'Name of the group to check
Dim oCheckGrp
oCheckGrp=”grp-toto”

'Powershell command line
Dim sCmd
sCmd = "%comspec% /c powershell dsquery user –samid " & vuser & " | dsget user -memberof | dsget group –samid"

'Checking oCheckGrp
if instr(oCheckGrp,",")=0 then
    set grptocheck = oShell.Exec(sCmd)
    oCheck = grptocheck.StdOut.ReadAll
    For Each Line In Split(oCheck,vbCrLf)
        if trim(Line)="" _
        or ubound(split(ucase(Line),ucase(here))) < 1 then
        else
            CheckGrpReturn(oCheckGrp)
        end if
    Next
else
    ArrFilestoDelete=Array(FilestoDelete)
    For Each file In ArrFilestoDelete
        set grptocheck = oShell.Exec(sCmd)
        oCheck = grptocheck.StdOut.ReadAll
            For Each Line In Split(oCheck,vbCrLf)
            CheckGrpReturn(oCheckGrp)
            Next
    Next
end if


'Return of checking oCheckGrp
function CheckGrpReturn(oCheckGrp)
if line=oCheckGrp then
wscript.echo  "user " & vuser & "is member of the group " & oCheckGrp
else
wscript.echo  "user " & vuser & "is not member of the group " & oCheckGrp & vbcrlf _
            & "but user " & vuser & "is member of those groups:” & vbcrlf _
            & sCmd
end if
end function

 

On Error GoTo 0

Commenter cet article