0

I have a question. I'm trying to change an AD user password in cmd with vb.net My command:

dsmod user "CN=admin,OU=Admins,OU=Users,DC=DOMAIN,DC=LOCAL" -pwd test1

In a regular cmd not as admin (WIN+R - cmd) it works perfectly

dsmod succeeded:CN=admin,OU=Admins,OU=Users,DC=DOMAIN,DC=LOCAL

But when I run the same command through vb.net:

Process.Start("cmd.exe", "/k dsmod user " & """" & "CN=admin,OU=Admins,OU=Users,DC=DOMAIN,DC=LOCAL" & """" & " -pwd " & TextBox1.Text & """")

I get:

'dsmod' is not recognized as an internal or external command,
operable program or batch file.

How can I fix this? Thanks

user7406533
  • 73
  • 1
  • 9
  • 1
    It is the kind of command that tends to be only available as a 64-bit program. Project > Properties > Compile tab, untick the "Prefer 32-bit" checkbox. The docs do mention that UAC elevation is required, https://stackoverflow.com/a/2818776/17034 – Hans Passant Dec 26 '17 at 00:30

1 Answers1

0

By default any process you start through Process.Start() gets the same working directory as your application. The dsmod.exe file is most likely located in System32, so you've got to set the new process's working directory to that in order for it to find the file.

Dim psi As New ProcessStartInfo("cmd.exe", "/k dsmod user " & """" & "CN=admin,OU=Admins,OU=Users,DC=DOMAIN,DC=LOCAL" & """" & " -pwd " & TextBox1.Text)
psi.WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32")

Process.Start(psi)

I also took the liberty of removing the extra double quote you put at the end of the arguments since it had no matching starting quote.

NOTE: Since the system folder might be located on a different drive than the C:\ drive, I used this code:

Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32")

in order to dynamically get the System32 folder of the current system. So for instance if the system drive has the letter D:\, this code will return D:\Windows\System32.


EDIT:

It appears that dsmod is only available in the 64-bit system directory. If your application is a 32-bit app it will also start a 32-bit version of CMD (located in SysWOW64), so you've got to force it to open the 64-bit version that's located in the System32 directory. To do this you've got to specify the full path of CMD, but in the Sysnative directory.

By specifying Sysnative instead of System32 you stop the system from redirecting you to the SysWOW64 directory for your 32-bit app.

Dim CmdPath As String = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), If(Environment.Is64BitProcess OrElse Environment.Is64BitOperatingSystem = False, "System32", "Sysnative"), "cmd.exe")

Dim psi As New ProcessStartInfo(CmdPath, "/k dsmod user " & """" & "CN=admin,OU=Admins,OU=Users,DC=DOMAIN,DC=LOCAL" & """" & " -pwd " & TextBox1.Text)
psi.WorkingDirectory = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "System32")

Process.Start(psi)
Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • Thanks for the reply, I do get System32 as the working directory, but I still get the same error... – user7406533 Dec 26 '17 at 13:53
  • That worked, Thanks! Until I saw this answer I found a shorter and simpler way to do this (with PowerShell) it is only 2 lines... Dim psi As New ProcessStartInfo("powershell.exe", "$newpwd = ConvertTo-SecureString -String " & """" & TextBox1.Text & """" & " -AsPlainText –Force ; Set-ADAccountPassword admin -NewPassword $newpwd –Reset") and 2nd line Dim process As Process = Process.Start(psi)... But thanks for the reply... – user7406533 Dec 26 '17 at 17:38
  • @user7406533 : Glad I could help! I intentionally made my solution longer so that it would be easier to read. – Visual Vincent Dec 26 '17 at 18:03
  • @user7406533 : Actually you can shorten that solution of yours to one line: `Dim p As Process = Process.Start("powershell.exe", "$newpwd = ConvertTo-SecureString -String " & """" & TextBox1.Text & """" & " -AsPlainText –Force ; Set-ADAccountPassword admin -NewPassword $newpwd –Reset")` (remove `Dim p As Process` if you don't need access to it after starting it) – Visual Vincent Dec 26 '17 at 18:05
  • BTW, (I know this is not vb related) in both cmd and PS after changing the password, the user is still accessible with the old password until you enter the user with the new password... – user7406533 Dec 26 '17 at 18:40
  • @user7406533 : I don't work with Active Directory so I wouldn't know. Though I did manage to find [this](https://community.spiceworks.com/topic/214779-ad-accounts-can-log-in-with-two-passwords-after-a-pw-change) and [this](https://www.ibm.com/support/knowledgecenter/SSPREK_9.0.2/com.ibm.isam.doc/wrp_config/reference/ref_pw_change_issue_ad_win.html). If you don't seem to be able to find a solution you might wanna ask on either [Super User](https://superuser.com/) or [Server Fault](https://serverfault.com). – Visual Vincent Dec 26 '17 at 19:55