0

I am trying to implement LAPS (Local Admin Password Solution) on our network and I am having issues integrating LAPS with the ADU&C right-click context menu. My goal is for our Helpdesk staff to be able to right-click on a computer object within ADU&C and force LAPS to reset the local Administrator password using a LAPS PowerShell script supplied with it. I understand that you cannot run PS directly from the ADU&C context menu itself but I should be able to create a batch file and have it run the desired PS script. Everything works correctly but the problem that I have is when I right-click a computer object in ADU&C and then launch the batch file from the context menu, it passes the computer name as a LDAP formatted string. I am trying to parse that string within the batch file itself and then pass the parsed string to PS but I haven’t had any success doing so.

Generally, the batch file that I am using (launched from the ADU&C adminContextMenu) is:

@echo off
Title "Reset local Administrator password"
For /F "tokens=2 delims==," %%A In ("%1%") Do Set CompName=%%%A
PowerShell.exe -ExecutionPolicy Unrestricted -Command "Import-Module AdmPwd.PS; Reset-AdmPwdPassword" -ComputerName:%CompName%
Set CompName=

An example of the string being passed to the batch file as the %1% command-line argument is (the quotes are part of the string also):

"LDAP://SERVER.contoso.com/CN=IT-SYSTEM,OU=ITusers,DC=contoso,DC=com"

What I want to get out of the above string is the computer name itself (IT-SYSTEM), which would be all of text after the first = (equal) sign up until the first comma (so my delims are set for that and my token is 2). I want to store that in a new variable called CompName and then I should be able to pass that variable along to the PS command within the batch file. I’ve tried a few variations of the FOR command without success. I think my problem might be that the original string being passed to the batch file has quotes wrapped around it already. Is it possible to pick up the desired info within the above string using a batch file if the string being parsed already has quotes around it?

Thanks in advance

2

Use %~1 to strip surrounding double quotes from the string %1, if present (requires Command extensions enabled). Moreover, there are some (key mistaken?) harmful extra % percent signs in your code:

For /F "tokens=2 delims==," %%A In ("%1%") Do Set CompName=%%%A
                                       ^                   ^

Next script could work:

@echo off
SETLOCAL EnableExtensions DisableDelayedExpansion
Title "Reset local Administrator password"
For /F "tokens=2 delims==," %%A In ("%~1") Do Set "CompName=%%A"
ECHO PowerShell.exe -ExecutionPolicy Unrestricted -Command "Import-Module AdmPwd.PS; Reset-AdmPwdPassword" -ComputerName:%CompName%
Set CompName=

Note that PowerShell.exe line is merely ECHOed for debugging purposes (as I do not have AdmPwd module installed to debug it); make it operational by removing leading ECHO word no sooner than debugged:

PowerShell.exe -ExecutionPolicy Unrestricted -Command "Import-Module AdmPwd.PS; Reset-AdmPwdPassword -ComputerName:%CompName%"

Resources (required reading):

  • You are AWESOME - worked great - thanks much!! – STGdb Apr 1 '16 at 21:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.