Param (
  [string]$exe = ".\dd.exe",
  [string]$target = "C:\DriverPackBackup"
)

$sig=@'
[DllImport("user32.dll", CharSet = CharSet.Auto)]
public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

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

[DllImport("user32.dll")]
public static extern IntPtr GetWindow(IntPtr hWnd, uint uCmd);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int hMsg, int wParam, int lParam);

[DllImport("user32.dll")]
public static extern int SendMessage(int hWnd, int hMsg, int wParam, String lParam);

[DllImport("user32.dll")]
public static extern bool PostMessage(int hWnd, int hMsg, int wParam, int lParam);
'@

# Убиваем все Double Driver, если они были
cmd.exe /c "taskkill /F /IM dd.exe"

# Запускаем новый процесс Double Driver в скрытом режиме
$startinfo = New-Object System.Diagnostics.ProcessStartInfo
$startinfo.FileName = $exe
$startinfo.Arguments = ""
$startinfo.WindowStyle = 'Hidden'
$startinfo.CreateNoWindow = $True
$process = [System.Diagnostics.Process]::Start($startinfo)


Add-Type -MemberDefinition $sig -name NativeMethods -namespace Win32

sleep 1


# Находим основное окно приложения, затем находим ToolBar
$MainWindow = [Win32.NativeMethods]::FindWindow('TMainDlg', 'Double Driver [Home]')
$findCoolBar = [Win32.NativeMethods]::FindWindowEx($MainWindow, 0, "TCoolBar", $null)
$ToolBar = [Win32.NativeMethods]::GetWindow($findCoolBar,5);
$ToolBar


# Внутри тулбара кликаем на вторую вкладку "Backup"
[Win32.NativeMethods]::SendMessage($ToolBar, 0x0201, 1, 1638475); # WM_LBUTTONDOWN с координатами 75x35
[Win32.NativeMethods]::SendMessage($ToolBar, 0x0202, 0, 1638475); # WM_LBUTTONUP с координатами 75x35
sleep -m 100;

# Находим кнопки "Scan Current System" и " Backup Now"
# Кликаем на "Scan Current System", ждем 3 секунды пока пройдет сканирование
# Кликаем на " Backup Now"
$findTPanel = [Win32.NativeMethods]::FindWindowEx($MainWindow, 0, "TPanel", $null)
$findButtonScanSystem = [Win32.NativeMethods]::FindWindowEx($findTPanel, 0, "TButton", "Scan Current System")
$findButtonBackupNow = [Win32.NativeMethods]::FindWindowEx($findTPanel, 0, "TButton", " Backup Now")
[Win32.NativeMethods]::SendMessage($findButtonScanSystem, 0x00F5, 0, 0);
sleep 3
[Win32.NativeMethods]::PostMessage($findButtonBackupNow, 0x00F5, 0, 0);
#sleep -m 100;

# Находим диалоговое окно подтверждения и прячем его
$findBackupDlg = [Win32.NativeMethods]::FindWindow("TBackupDlg", "Double Driver")
[Win32.NativeMethods]::SendMessage($findBackupDlg, 0x18, 0, 1);

$findBackupDlgOK = [Win32.NativeMethods]::FindWindowEx($findBackupDlg, 0, "TButton", "OK")
$findBackupDlgPanel = [Win32.NativeMethods]::FindWindowEx($findBackupDlg, 0, "TPanel", "Panel1")
$findBackupDlgGroupBox = [Win32.NativeMethods]::FindWindowEx($findBackupDlgPanel, 0, "TGroupBox", "Destination")
$findBackupDlgEditButton = [Win32.NativeMethods]::GetWindow($findBackupDlgGroupBox,5);
$findBackupDlgEdit = [Win32.NativeMethods]::GetWindow($findBackupDlgEditButton,2);

# Меняем путь для бекапа в DlgEdit и кликаем "ОК"
[Win32.NativeMethods]::SendMessage($findBackupDlgEdit, 0x000C, 0, $target);
[Win32.NativeMethods]::SendMessage($findBackupDlgOK, 0x00F5, 0, 0);

# Ожидаем, пока идет процесс бэкапа (отсюда можно даже проценты парсить, если надо)
sleep -m 100;
while ((Get-Process -id $process.id | %{$_.MainWindowTitle}) -like '* Backing-up *') {
    sleep -m 50;
    'tick...'
}

# Убиваем процесс Double Driver
$process.kill()
cmd.exe /c "taskkill /F /IM dd.exe"
