Jump to content

Recommended Posts

Posted

Good Morning all,

 

Looking for some assistance in writing a powershell script (Only has to be a line or two if needed) to enable AD Advanced Features. My workplace run on VDI's, meaning that we have to set these settings everytime we log on. So trying to avoid this.

 

Any help and guidance would be appreciated.

 

K

Posted

Chat GPT's finest:

# Save the current settings for dsa.msc
$currentKey = Get-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer"
$backup = @{
   NoViewOnDrive = $currentKey.NoViewOnDrive
}

# Enable advanced features by setting the appropriate registry key
Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" -Name "NoViewOnDrive" -Value 0

# Launch Active Directory Users and Computers with advanced features
Start-Process dsa.msc

# Pause for the user to close dsa.msc
Write-Host "Press any key to revert the registry settings and exit..."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

# Revert the registry settings
foreach ($key in $backup.Keys) {
   Set-ItemProperty -Path "HKCU:\Software\Microsoft\Windows\CurrentVersion\Policies\Explorer" -Name $key -Value $backup[$key]
}

Write-Host "Registry settings reverted. Exiting..."

 

"Explanation:

 

  1. Saving Current Settings: The script saves the current registry settings related to the advanced features to ensure it can revert them after usage.
  2. Enable Advanced Features: It modifies the registry settings to enable advanced features.
  3. Launch dsa.msc: It starts the dsa.msc console.
  4. Revert Settings: After the user closes dsa.msc, the script will revert the registry settings back to their original state.

Running the Script:

 

  1. Open PowerShell as an administrator.
  2. Copy and paste the script into the PowerShell window or save it as a .ps1 file and execute it.

This script ensures that the changes are temporary and are reverted back to their original state after usage."

 

This wont work as it's not the correct registry key - a quick google didn't reveal the correct registry key. So I told Chat GPT that the registry key was incorrect and this is the next result:

 

 

"Apologies for that oversight. Here’s a corrected PowerShell script to enable advanced features for Active Directory Users and Computers (dsa.msc):To enable the advanced features in dsa.msc, you typically use the GUI to check the "Advanced Features" option in the "View" menu. However, there isn't a direct registry key to enable this feature, but you can use PowerShell to open dsa.msc and automate the enabling of advanced features if needed.

Here is a script that launches dsa.msc and interacts with the UI to enable advanced features:

 

# Add-Type for using Windows API functions
Add-Type @"
using System;
using System.Runtime.InteropServices;

public class User32 {
   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

   [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
   public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

   [DllImport("user32.dll")]
   [return: MarshalAs(UnmanagedType.Bool)]
   public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

   public const int WM_COMMAND = 0x111;
   public const int ID_VIEW_ADVANCED_FEATURES = 0x2D3; // Assuming the ID for Advanced Features
}
"@

# Start dsa.msc
Start-Process dsa.msc

# Give it some time to launch
Start-Sleep -Seconds 5

# Find the dsa.msc window
$windowHandle = [user32]::FindWindow("MMCMainFrame", "Active Directory Users and Computers")

if ($windowHandle -ne [intPtr]::Zero) {
   # Find the View menu (assuming it's the third item in the menu)
   $viewMenuHandle = [user32]::FindWindowEx($windowHandle, [intPtr]::Zero, "MMCChildFrm", $null)

   if ($viewMenuHandle -ne [intPtr]::Zero) {
       # Post message to enable Advanced Features
       [user32]::PostMessage($viewMenuHandle, [user32]::WM_COMMAND, [intPtr]::op_Explicit([user32]::ID_VIEW_ADVANCED_FEATURES), [intPtr]::Zero)
       Write-Host "Advanced Features enabled in dsa.msc"
   } else {
       Write-Host "View menu not found."
   }
} else {
   Write-Host "dsa.msc window not found."
}

 

 

Explanation:

 

  1. Add-Type Definition: Adds the necessary Windows API functions using Add-Type to interact with the GUI.
  2. Launch dsa.msc: Uses Start-Process to open the Active Directory Users and Computers management console.
  3. Sleep: Adds a sleep period to give the application time to open.
  4. Find the Window: Utilizes the FindWindow and FindWindowEx functions to locate the dsa.msc window and its menus.
  5. Post Message: Sends a message to the application to enable the Advanced Features.

Running the Script:

 

  1. Save the script as a .ps1 file, for example, Enable-AdvancedFeatures.ps1.
  2. Open PowerShell as an administrator.
  3. Run the script by executing .\Enable-AdvancedFeatures.ps1.

Note that UI automation can be brittle and might need adjustments based on the actual menu structure and window hierarchy, which can vary between versions and configurations."

 

 

 

Run at your own risk! ...I'd just click the advanced features button from the menu tbh.

Posted

The preferences file is %AppData%\Roaming\Microsoft\MMC\dsa"

 

Appears to be xml/binary combo, do a diff to see what changes, or enable profile roaming

Posted

I'm very much in the same boat as you, I'd just click the menu.

 

When running the second script, it's always coming back as it cant find the view menu.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...