82 lines
2.5 KiB
PowerShell
82 lines
2.5 KiB
PowerShell
|
|
#Requires -Version 5.1
|
||
|
|
[CmdletBinding()]
|
||
|
|
param(
|
||
|
|
[string]$Solution = $null,
|
||
|
|
[string]$TargetNetId = "10.0.0.106.1.1"
|
||
|
|
)
|
||
|
|
|
||
|
|
. "$PSScriptRoot\tc-common.ps1"
|
||
|
|
if (-not $Solution) { $Solution = $script:DefaultTsproj }
|
||
|
|
|
||
|
|
New-Log $script:DefaultLogDir
|
||
|
|
$LogFile = Join-Path $script:DefaultLogDir "activate-restart.log"
|
||
|
|
|
||
|
|
Write-Log "=== Activate + Restart TwinCAT (direct TcSysManager) ===" $LogFile
|
||
|
|
|
||
|
|
# 1. Open project using TcSysManCmdHelper (avoids DTE COM issues)
|
||
|
|
$helper = "C:\Program Files (x86)\Beckhoff\TwinCAT\3.1\Components\Base\TcSysManCmdHelper.exe"
|
||
|
|
Write-Log "Opening project with TcSysManCmdHelper..." $LogFile
|
||
|
|
& $helper -v 4026.0 -o $Solution 2>&1 | Tee-Object -FilePath $LogFile -Append
|
||
|
|
Write-Log "Open exit code: $LASTEXITCODE" $LogFile
|
||
|
|
|
||
|
|
# 2. Get TcSysManager COM object directly (not via DTE.Object)
|
||
|
|
function Get-TcSysManagerDirect {
|
||
|
|
$progIds = @("TcSysManager", "TcXaeShell.TcSysManager", "TcXaeMgmt.TcSysManager", "TwinCAT.SystemManager")
|
||
|
|
foreach ($id in $progIds) {
|
||
|
|
try {
|
||
|
|
$obj = [System.Runtime.InteropServices.Marshal]::GetActiveObject($id)
|
||
|
|
if ($obj) { return @{ Object = $obj; ProgId = $id } }
|
||
|
|
} catch { }
|
||
|
|
try {
|
||
|
|
$obj = New-Object -ComObject $id -ErrorAction Stop
|
||
|
|
if ($obj) { return @{ Object = $obj; ProgId = $id } }
|
||
|
|
} catch { }
|
||
|
|
}
|
||
|
|
return $null
|
||
|
|
}
|
||
|
|
|
||
|
|
$smInfo = Get-TcSysManagerDirect
|
||
|
|
if (-not $smInfo) {
|
||
|
|
Write-Log "FATAL: Cannot get TcSysManager COM object" $LogFile
|
||
|
|
Write-Error "Cannot get TcSysManager COM object"
|
||
|
|
exit 1
|
||
|
|
}
|
||
|
|
|
||
|
|
Write-Log "TcSysManager obtained via $($smInfo.ProgId)" $LogFile
|
||
|
|
$sysMgr = $smInfo.Object
|
||
|
|
|
||
|
|
try {
|
||
|
|
# Set target
|
||
|
|
try {
|
||
|
|
$sysMgr.SetTargetNetId($TargetNetId)
|
||
|
|
Write-Log "SetTargetNetId $TargetNetId OK" $LogFile
|
||
|
|
} catch {
|
||
|
|
Write-Log "SetTargetNetId warning: $_" $LogFile
|
||
|
|
}
|
||
|
|
|
||
|
|
# Activate
|
||
|
|
Write-Log "Calling ActivateConfiguration()..." $LogFile
|
||
|
|
try {
|
||
|
|
$sysMgr.ActivateConfiguration()
|
||
|
|
Write-Log "ActivateConfiguration() OK" $LogFile
|
||
|
|
} catch {
|
||
|
|
Write-Log "ActivateConfiguration() error: $_" $LogFile
|
||
|
|
}
|
||
|
|
|
||
|
|
Start-Sleep -Seconds 5
|
||
|
|
|
||
|
|
# Restart TwinCAT
|
||
|
|
Write-Log "Calling StartRestartTwinCAT()..." $LogFile
|
||
|
|
try {
|
||
|
|
$sysMgr.StartRestartTwinCAT()
|
||
|
|
Write-Log "StartRestartTwinCAT() OK" $LogFile
|
||
|
|
} catch {
|
||
|
|
Write-Log "StartRestartTwinCAT() error: $_" $LogFile
|
||
|
|
}
|
||
|
|
|
||
|
|
Start-Sleep -Seconds 10
|
||
|
|
Write-Log "=== Activate+Restart complete ===" $LogFile
|
||
|
|
} finally {
|
||
|
|
# Don't release COM object aggressively; let XAE process keep it alive
|
||
|
|
}
|