Windows-Server-2003/tools/postbuildscripts/add2grp.wsf

443 lines
9.8 KiB
XML

<?XML version="1.0" ?>
<package>
<job>
<resource id="computer">I32bt00X1</resource>
<resource id="domain">ntdev</resource>
<resource id="domainuser">winbld</resource>
<resource id="localgroup">Administrators</resource>
<resource id="Usage">
Name:
%SELF%
Adding the user account to the localgroup
Usage:
%SELF% -h -d -u:%USER% g:%GROUP%
Where
-h show this message
-d enable debugging
-u username. Default is "%DOMAINUSER%"
-g the name for the localgroup. Default: "%LOCALGROUP%"
</resource>
<script language="VBScript">
<![CDATA[
option explicit
Public Function reQuoteObjArgs(objArgs)
Dim sLabelVar, sFoundLabel, sArgsDictionary, sArg, oArgs, nArg
Dim poQuotedCheckRegEx, poStringCheckRegEx, psQuotedCheckPt
Dim sConcatArgs, aRepArgs, spBadQuotedPart, spGoodQuotedPart, apQuoted
sConcatArgs = join(objArgs, " ")
psQuotedCheckPt = "\'[^\']+\'"
Set poQuotedCheckRegEx = New RegExp
With poQuotedCheckRegEx
.Global = True
.IgnoreCase = True
.Pattern = psQuotedCheckPt
End with
Set poStringCheckRegEx = New RegExp
With poQuotedCheckRegEx
.Global = True
.IgnoreCase = True
.Pattern = psQuotedCheckPt
End with
If True = poQuotedCheckRegEx.Test(sConcatArgs) Then
Set apQuoted = poQuotedCheckRegEx.Execute(sConcatArgs)
For each spBadQuotedPart in apQuoted
spGoodQuotedPart = Replace(spBadQuotedPart," ", "0x20", 1, 1000)
spGoodQuotedPart = Mid(spGoodQuotedPart,2, Len(spGoodQuotedPart) -2 )
poStringCheckRegEx.Pattern = Replace(spBadQuotedPart, "\", "\\", 1 ,100)
sConcatArgs = poStringCheckRegEx.Replace(sConcatArgs, spGoodQuotedPart)
Next
aRepArgs = Split(sConcatArgs, " ")
oArgs = Array()
Redim oArgs (ubound(aRepArgs) + 1)
For nArg = 0 To ubound(aRepArgs)
oArgs(nArg) = Replace(aRepArgs(nArg), "0x20", " ", 1, 100)
Next
reQuoteObjArgs = oArgs
Else
reQuoteObjArgs = objArgs
End If
End Function
Public Sub GetParams(sFlags, sVarNames, objArgs)
Dim sLabelVar, sFoundLabel, sArgsDictionary
Dim poKnownSwitchRegEx, psMaskKnownSwitch, nArg, mma, nma, nextlen, objargsnamed
Dim aFlags, aVarNames ' Arrays to split the sFlags and sVarNames
Set poKnownSwitchRegEx = New RegExp
Set sArgsDictionary = CreateObject("Scripting.Dictionary")
psMaskKnownSwitch = "[-/]\w+:?\b"
poKnownSwitchRegEx.Global = True
poKnownSwitchRegEx.IgnoreCase = True
poKnownSwitchRegEx.Pattern = psMaskKnownSwitch
If 0 = ubound(objArgs) Then
Exit Sub
End If
For nArg = 0 to uBound(objArgs)
If True = poKnownSwitchRegEx.Test(objArgs(nArg)) Then
Set mmA = poKnownSwitchRegEx.Execute(objArgs(nArg))
If mma.Count <> 1 Then
WScript.echo "Bad RegExp Count"
Err.Raise (1937)
Else
For each nma in mma
NextLen = nma.FirstIndex + nma.Length
sFoundLabel = CStr(Mid(objArgs(nArg), _
2 + nma.FirstIndex, _
nma.Length - 1))
' This is not If Right(sFoundLabel,1) = ":" Then
' required sFoundLabel = Left(sFoundLabel, len(sFoundLabel) - 1 )
' End If
If NextLen <> len(objArgs(nArg)) Then
sLabelVar = CStr(Mid(objArgs(nArg),NextLen + 1 ))
Else
sLabelVar = True
End If
sArgsDictionary.add sFoundLabel, sLabelVar
Next
End If
End If
Next
aFlags = split(sFlags)
aVarNames = split(sVarNames)
If Ubound(aFlags) <> Ubound(aVarNames) Then
Exit Sub
End If
Dim tnCnt
For tnCnt = 0 to Ubound(aFlags)
If sArgsDictionary.Exists(aFlags(tnCnt)) Then
FeedVar sArgsDictionary(aFlags(tnCnt)), aVarNames(tnCnt)
End If
Next
Set sArgsDictionary = Nothing
Set poKnownSwitchRegEx = Nothing
End Sub
Private Sub FeedVar(sLabelVar, sLabel)
If VarType(sLabelVar) <> vbString Then
Execute(sLabel & "=" & sLabelVar)
Else
Execute(sLabel & "=" & """" & sLabelVar & """" )
End If
End Sub
]]>
</script>
<script language="VBScript">
<![CDATA[
Function ReadResource(sPresId)
On Error resume next
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
Dim sPRes, asRes, uRes, tsRes
sPREs = GetResource(sPresId)
If Err.number <> 0 Then
Wscript.echo err.number & " " & err.description
' The Error description are not informative when failed getresource
IF Err.number = 7 Then
Wscript.echo chr(9) & "Null resource: " & _
sPresId
End If
IF Err.number = 5 Then
Wscript.echo chr(9) & "Undefined resource: " & _
sPresId
End If
Wscript.quit
End If
asRes = Split(WshShell.ExpandEnvironmentStrings(sPRes), VBNEWLINE)
Set ures = CreateObject("Scripting.Dictionary")
For Each tsRes in asRes
If 0 <> Len(tsRes) Then
uRes(tsRes) = 1
End If
Next
Dim tResource
tResource = uRes.Keys
If uBound(tResource) = 0 Then
ReadResource = tResource(0)
Else
ReadResource = tResource
End If
Set WshShell = Nothing
Set uRes = Nothing
End Function
sub showUsage
Const VBEXPECTED = 5
on error resume next
Dim oGenRplaceVarNamXp
Dim oExRplaceVarNamXp
Dim aoRplaceMatches, oMatch, sMatch, sLabel, sResource
Set oGenRplaceVarNamXp = new RegExp
Set oExRplaceVarNamXp = new RegExp
With oGenRplaceVarNamXp
.Global = True
.IgnoreCase = True
.Pattern = "%\w+%"
End With
With oExRplaceVarNamXp
.Global = True
.IgnoreCase = True
.Pattern = "%\w+%"
End With
Dim sLine,asTest
asTest = ReadResource("Usage")
For Each sLine in asTest
Set aoRplaceMatches = oGenRplaceVarNamXp.Execute(sLine)
For Each oMatch in aoRplaceMatches
sMatch = oMatch.value
sMatch = UCase(Mid(sMatch, 2, Len(sMatch)-2))
sLabel = LCase(sMatch)
Err.clear
sResource = GetResource(sLabel)
If err = 0 Then
sMatch = sResource
ElseIf err = VBEXPECTED Then
sMatch = "<" & sMatch & ">"
Else
Raise Err
End If
oExRplaceVarNamXp.Pattern = oMatch.value
sLine = oExRplaceVarNamXp.Replace(sLine, sMatch)
Next
WScript.echo sLine
Next
WScript.Quit(0)
End sub
]]>
</script>
<script language="VBScript">
<![CDATA[
Dim Debug, Help, siLocalGroup, siDomainUser
Dim sLocalGroup, sDomainUser, sLocalComputer, oNetwork
Dim apCopyArgs, nRawArg
sDomainUser = CStr(GetResource("domainuser"))
sLocalGroup = CStr(GetResource("localgroup"))
sLocalComputer = CStr(GetResource("computer"))
sDomain = CStr(GetResource("domain"))
Set oNetwork = CreateObject("WScript.Network")
sLocalComputer = CStr(oNetwork.ComputerName)
apCopyArgs = Array()
Redim apCopyArgs (Wscript.arguments.Count)
For nRawArg = 0 to Wscript.arguments.Count - 1
apCopyArgs(nRawArg) = Wscript.arguments(nRawArg)
Next
apCopyArgs = reQuoteObjArgs(apCopyArgs)
Call GetParams("h d u: g:", _
"Help Debug siDomainUser siLocalGroup", _
apCopyArgs)
if Help <> "" Then
ShowUsage
End If
if siDomainUser <> "" Then
sDomainUser = siDomainUser
End If
if siLocalGroup <> "" Then
sLocalGroup = siLocalGroup
End If
Call AddUser(sLocalComputer, sDomain, sLocalGroup, sDomainUser)
WScript.quit(0)
private sub AddUser (sLocalComputer, sDomain, sLocalGroup, sDomainUser)
on error resume next
err.clear
set objGroup = GetObject("WinNT://" & sLocalComputer & "/" & sLocalGroup)
objGroup.Add "WinNT://" & sDomain & "/" & sDomainUser
if err.number <> 0 then
If True = Debug then
WScript.Echo "Unable to add " & _
sDomainUser & _
" to group " & _
sLocalGroup & _
VBNEWLINE & _
Err.number & _
VBNEWLINE & _
err.Description
End If
''
''
Const cMode ="WQL"
Const lsWMIMoniker = "winmgmts:{impersonationLevel=impersonate}"
sPGroupComponentTemplate = _
"Win32_Group.Domain=\""" & _
Ucase(sLocalComputer) & _
"\"",Name=\"""& _
sLocalGroup & _
"\"""
sPWMIQueryString="SELECT * FROM Win32_GroupUser " &_
"WHERE GroupComponent=""" & sPGroupComponentTemplate & """"
Set atPResWMIObjS = GetObject(lsWMIMoniker)._
ExecQuery(sPWMIQueryString, _
cMode)
sPPartComponentTemplate="Win32_UserAccount.Domain=\""" & _
UCase(sDomain) & _
"\"",Name=\""" & _
UCase(sDomainUser) & _
"\"""
For Each tPResWMIObjS In atPResWMIObjS
Set oPPartComponentRegEx = New RegExp
oPPartComponentRegEx.Global = True
oPPartComponentRegEx.IgnoreCase = True
oPPartComponentRegEx.Pattern = sPPartComponentTemplate
Set aDummy = oPPartComponentRegEx.Execute(tPResWMIObjS.PartComponent)
If aDummy.Count <> 0 Then
If Debug = True Then
WScript.echo """" & _
sPWMIQueryString & _
"""" & _
VBNEWLINE & _
tPResWMIObjS.PartComponent
End If
End If
Next
Else
If True = Debug Then
Wscript.Echo "Finished adding " & sDomainUser & " to group " & sLocalGroup
End If
end if
end sub
]]>
</script>
</job>
</package>