This function adds a domain group to a local group but uses alternate credentials than that of the user account running the script.
This is handy when you’re doing automated deployments and you’re using auto admin login for post-install configuration.
Here’s the syntax (Note the forward slash to specify the name of the group you want to add the back slash for the credential). The function returns 0 if all is well.
AddToLocalGroup(LocalGroup, DomainGroup, DomainUser, Password) e.g. AddToLocalGroup("Administrators", "domain/groupToAdd","MyCompany\RhysAdmin","secretepassword")
And here’s the function
Function AddToLocalGroup(strLocalGroup,strDomainSlashGroup,strUsername,strPassword) on error resume next Dim strDctrl, strServerName, objDomainGroup, objLocalAdmGroup Const ADS_SECURE_AUTHENTICATION = &H1 Set oLocalAdmGroup = GetObject("WinNT://localhost/"&strLocalGroup&",group") Set objNS = GetObject("WinNT:") Set oDomainGroup = objNS.OpenDSObject("WinNT://" & strDomainSlashGroup & ",group", strUsername, strPassword, ADS_SECURE_AUTHENTICATION) oLocalAdmGroup.Add(oDomainGroup.AdsPath) AddToLocalGroup = Err End Function
Sometimes you just can’t find the exact snippet that you need. That happened to me today so there it is.
[New Post] vbScript – Add to Local Group with Alternate Credentials – https://blog.rhysgoodwin.com/windows-admi…