Skip to content

Instantly share code, notes, and snippets.

@benhar-dev
Created January 17, 2023 03:15
Show Gist options
  • Save benhar-dev/b8a5b992f661cdcaa5639668bf06d1d2 to your computer and use it in GitHub Desktop.
Save benhar-dev/b8a5b992f661cdcaa5639668bf06d1d2 to your computer and use it in GitHub Desktop.
Powershell - ADS Command
param (
[Parameter()]
[string]$NetId,
[string]$args
)
$path = 'C:\Windows\System32\cmd.exe'
$dir = 'C:\Windows\System32'
if (-not $NetId) {$NetId= Read-Host -Prompt "AmsNetId of the TwinCAT computer on which the application is to be started"}
if ($NetId -eq '') {$NetId= '127.0.0.1.1.1'}
if (-not $args) {$args= Read-Host -Prompt $dir}
if (-not $NetId -eq ''){$args = "/c" + $args}
$path = 'C:\Windows\System32\cmd.exe'
$dir = 'C:\Windows\System32'
$Data = [byte[]]::new(777)
[BitConverter]::GetBytes($path.Length).CopyTo($Data, 0)
[BitConverter]::GetBytes($dir.Length).CopyTo($Data, 4)
[BitConverter]::GetBytes($args.Length).CopyTo($Data, 8)
[System.Text.Encoding]::ASCII.GetBytes($path).CopyTo($Data, 12)
[System.Text.Encoding]::ASCII.GetBytes($dir).CopyTo($Data, 12 + $path.Length + 1)
[System.Text.Encoding]::ASCII.GetBytes($args).CopyTo($Data, 12 + $path.Length + 1 + $dir.Length + 1)
$AMSPORT_R3_SYSSERV = 10000
$SYSTEMSERVICE_STARTPROCESS = 500
$session = New-TcSession -NetId $NetId -Port 10000
DO
{
Write-TcValue -session $session -IndexGroup $SYSTEMSERVICE_STARTPROCESS -IndexOffset 0 -Value $Data -Force
$args = Read-Host -Prompt $dir
if (-not $args -eq ''){$args = "/c" + $args}
$Data = [byte[]]::new(777)
[BitConverter]::GetBytes($path.Length).CopyTo($Data, 0)
[BitConverter]::GetBytes($dir.Length).CopyTo($Data, 4)
[BitConverter]::GetBytes($args.Length).CopyTo($Data, 8)
[System.Text.Encoding]::ASCII.GetBytes($path).CopyTo($Data, 12)
[System.Text.Encoding]::ASCII.GetBytes($dir).CopyTo($Data, 12 + $path.Length + 1)
[System.Text.Encoding]::ASCII.GetBytes($args).CopyTo($Data, 12 + $path.Length + 1 + $dir.Length + 1)
$args
} Until ('' -eq $args)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment