Text Share Online

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# CONFIG
$WinSCP = Join-Path $PSScriptRoot "WinSCP.com"
$InputFile = Join-Path $PSScriptRoot "liste_ips.csv"

if (!(Test-Path $WinSCP)) {
[System.Windows.Forms.MessageBox]::Show("WinSCP.com introuvable")
return
}

$script:StopScan = $false
$script:CurrentProcess = $null

# FORM
$form = New-Object System.Windows.Forms.Form
$form.Text = "Kioskium Check"
$form.Size = New-Object System.Drawing.Size(1100,600)

# TOP PANEL
$panelTop = New-Object System.Windows.Forms.Panel
$panelTop.Dock = "Top"
$panelTop.Height = 60
$form.Controls.Add($panelTop)

$btnStart = New-Object System.Windows.Forms.Button
$btnStart.Text = "Start"
$btnStart.Location = "10,10"
$panelTop.Controls.Add($btnStart)

$btnStop = New-Object System.Windows.Forms.Button
$btnStop.Text = "Stop"
$btnStop.Location = "100,10"
$panelTop.Controls.Add($btnStop)

# BOTTOM PANEL
$panelBottom = New-Object System.Windows.Forms.Panel
$panelBottom.Dock = "Bottom"
$panelBottom.Height = 35
$form.Controls.Add($panelBottom)

$progress = New-Object System.Windows.Forms.ProgressBar
$progress.Dock = "Fill"
$panelBottom.Controls.Add($progress)

# SPLIT
$split = New-Object System.Windows.Forms.SplitContainer
$split.Dock = "Fill"
$split.Orientation = "Horizontal"
$split.SplitterDistance = 420
$form.Controls.Add($split)

# TABLE
$table = New-Object System.Windows.Forms.DataGridView
$table.Dock = "Fill"
$table.ColumnCount = 3

$table.Columns[0].Name = "IP"
$table.Columns[1].Name = "Connexion"
$table.Columns[2].Name = "Diagnostic"

$table.Columns[0].Width = 200
$table.Columns[1].Width = 120
$table.Columns[2].AutoSizeMode = "Fill"

$table.AllowUserToAddRows = $false
$table.RowHeadersVisible = $false
$table.ReadOnly = $true
$table.SelectionMode = "FullRowSelect"

$table.DefaultCellStyle.Font = New-Object System.Drawing.Font("Segoe UI",10)
$table.ColumnHeadersDefaultCellStyle.Font = New-Object System.Drawing.Font(
"Segoe UI",
10,
[System.Drawing.FontStyle]::Bold
)

$split.Panel1.Controls.Add($table)

# LOGS
$logbox = New-Object System.Windows.Forms.RichTextBox
$logbox.Dock = "Fill"
$logbox.Font = New-Object System.Drawing.Font("Consolas",9)
$split.Panel2.Controls.Add($logbox)

function Add-Log {
param([string]$Text)

$logbox.AppendText("$Text`r`n")
$logbox.SelectionStart = $logbox.TextLength
$logbox.ScrollToCaret()
}

# STOP
$btnStop.Add_Click({

$script:StopScan = $true

if (
$script:CurrentProcess -and
-not $script:CurrentProcess.HasExited
) {
$script:CurrentProcess.Kill()
}

Add-Log ">>> STOP DEMANDE <<<"
})

# START
$btnStart.Add_Click({

$script:StopScan = $false

$table.Rows.Clear()
$logbox.Clear()

$lines = Get-Content $InputFile |
Where-Object { $_.Trim() -ne "" }

$progress.Maximum = $lines.Count
$progress.Value = 0

foreach ($entry in $lines) {

if ($script:StopScan) {
break
}

$parts = $entry.Split(",")

if ($parts.Count -lt 3) {
continue
}

$IP = $parts[0].Trim()
$User = $parts[1].Trim()
$Pass = $parts[2].Trim()

Add-Log "===== $IP ====="

$tmpScript = Join-Path $env:TEMP "winscp_$($IP.Replace('.','_')).txt"
$tmpOutput = Join-Path $env:TEMP "winscp_out_$($IP.Replace('.','_')).txt"

@"
option batch abort
option confirm off
open sftp://$User`:$Pass@$IP/ -hostkey=*
call test -f /home/siindus/Documents/instructions.html && echo FILE_OK || echo FILE_MISSING
exit
"@ | Out-File $tmpScript -Encoding ASCII

try {

$proc = Start-Process `
-FilePath $WinSCP `
-ArgumentList "/script=$tmpScript" `
-RedirectStandardOutput $tmpOutput `
-NoNewWindow `
-PassThru

$script:CurrentProcess = $proc

while (-not $proc.HasExited) {

if ($script:StopScan) {
$proc.Kill()
break
}

Start-Sleep -Milliseconds 200
[System.Windows.Forms.Application]::DoEvents()
}

if ($script:StopScan) {
break
}

$result = ""

if (Test-Path $tmpOutput) {
$result = Get-Content $tmpOutput -Raw
}

Add-Log $result

# CONNEXION KO
if (
$result -match "Authentication failed" -or
$result -match "Network error" -or
$result -match "Connection failed" -or
$result -match "timed out" -or
$result -match "refused" -or
$result -match "No route"
) {

$row = $table.Rows.Add(
$IP,
"KO",
"Connexion echouee"
)

$table.Rows[$row].DefaultCellStyle.BackColor =
[System.Drawing.Color]::LightCoral
}

# FICHIER PRESENT
elseif ($result -match "instructions.html") {

$row = $table.Rows.Add(
$IP,
"OK",
"Present"
)

$table.Rows[$row].DefaultCellStyle.BackColor =
[System.Drawing.Color]::LightGreen
}

# FICHIER ABSENT
else {

$row = $table.Rows.Add(
$IP,
"OK",
"Fichier absent"
)

$table.Rows[$row].DefaultCellStyle.BackColor =
[System.Drawing.Color]::Orange
}
# ---------------------------------------------------
# CAS INCONNU
# ---------------------------------------------------
else {

$row = $table.Rows.Add(
$IP,
"?",
"Analyse manuelle"
)

$table.Rows[$row].DefaultCellStyle.BackColor =
[System.Drawing.Color]::Khaki

Add-Log ">>> RESULTAT NON RECONNU <<<"
}

}
catch {

$row = $table.Rows.Add(
$IP,
"KO",
"Erreur script"
)

$table.Rows[$row].DefaultCellStyle.BackColor =
[System.Drawing.Color]::LightCoral

Add-Log $_.Exception.Message
}
finally {

Remove-Item $tmpScript -ErrorAction SilentlyContinue
Remove-Item $tmpOutput -ErrorAction SilentlyContinue

if ($progress.Value -lt $progress.Maximum) {
$progress.Value++
}
}
}

Add-Log "===== TERMINE ====="
})

$form.ShowDialog()
  • auto
  • 1h
Share This: