# Этот скрипт почему то очень долго работает при запуске из 64-битного приложения 1С, а из 32-битного вообще зависает. При этом независимый запуск выполняется быстро.
param (
[string]$outFileName = $(throw "Укажите файл для вывода списка главных окон конфигураторов 1С с помощью ключа -outFileName.")
)
#$outFileName = "C:\TerminalDisk\1.txt"
#REQUIRES -Version 3.0
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClient")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationTypes")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationProvider")
[void][System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClientsideProviders")
$client = [System.Reflection.Assembly]::LoadWithPartialName("UIAutomationClientsideProviders")
try
{
    [Windows.Automation.ClientSettings]::RegisterClientSideProviderAssembly($client.GetName()) # WORKAROUND: There is a weird bug: first call fails ... https://gist.github.com/xiongjia/6749035
}
catch {}
[Windows.Automation.ClientSettings]::RegisterClientSideProviderAssembly($client.GetName()) # second call succeeds
$rootElement = [Windows.Automation.AutomationElement]::RootElement
$nameType = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ClassNameProperty, "V8TopLevelFrame")
$condType = New-Object Windows.Automation.PropertyCondition([Windows.Automation.AutomationElement]::ControlTypeProperty, [Windows.Automation.ControlType]::Window)
$condFinal = New-Object Windows.Automation.AndCondition($condType, $nameType)
$windows = $rootElement.FindAll([Windows.Automation.TreeScope]::Children, $condFinal)
$outText = ""
foreach($window in $windows)
{
    $windowName = $window.Current
    $windowName = $windowName.Name 
    if($windowName -like "*Конфигуратор*" -or $windowName -like "*Designer*")
    {
        $outText += "" + $window.Current.ProcessId + " " + $window.Current.Name + "`n"
    }
}
Set-Content $outFileName $outText -Force
