I have some folder, inside those folder has a file call it SSID_LST. Inside the SSID_LST file there is a string. I want to match the SSID_LST with $SSIDUnit. If it is match, It will return the folder path. If its not match, it will show the GUI and do looping to check which SSID_LST match with the $SSIDUnit. In this code, I can get the folder which match with $SSIDUnit. But once it is not matched and show GUI for doing check match $SSIDUnit, and then I drop a new folder contain of SSID_LST and inside the SSID_LST match with $SSIDUnit, it does not check the SSID_LST in new folder that I drop. It only checking the old folder. Its like how to update the $Path to check the existing folder to find SSID_LST file.
I have to do updating in this part, if there is a new folder drop to $JobFolder, it will include the new folder for checking the SSID_LST file.
$Path = Get-ChildItem "$JobFolder\*\$SSIDName\"
Write-Output "Path SSID : $Path"
Anyone has an idea about it? I refer to this answer https://stackoverflow.com/a/57524097/11099245
function Test-FileWithGui {
[CmdletBinding()]
Param(
[Parameter(Mandatory = $true, Position = 0)]
$Path,
[Parameter(Mandatory = $true, Position = 2)]
[string]$Pattern,
[int]$MaxAttempts = 50
)
# set up an 'empty' $global:Result object to return on failure
$global:Result = '' | Select-Object @{Name = 'Exists'; Expression = {$false}}, FileName, Directory, @{Name = 'Attempts'; Expression = {1}}
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]@{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = 1
}
} else {
& ".\Wait_GUI.ps1" -Path $Path -Pattern $Pattern -MaxAttempts $MaxAttempts
}
}
function MAPPING_JOB {
$SSIDUnit = "1234"
Write-Output "Strating mapping the job folder"
$JobFolder = "D:\Process"
$SSIDName = "SSID_LST"
Write-Output "SSIDNAME : $SSIDName"
$Path = Get-ChildItem "$JobFolder\*\$SSIDName\" # the path to look for files
Write-Output "Path SSID : $Path"
$Wait_Job = "1000"
Test-FileWithGui -Path $Path -Pattern "$SSIDUnit" -MaxAttempts $Wait_Job
Pause
# show the $global:Result object with all properties
$global:Result | Format-List
# check the Global result object
if ($global:Result.Exists) {
Write-Output "File '$($global:Result.FileName)' Exists. Found after $($global:Result.Attempts) attempts." -ForegroundColor Green
Write-Output "Continue to check the job.que"
pause
CALL_JOBQUE
} else {
Write-Output "File not found after $($global:Result.Attempts) attempts." -ForegroundColor Red
Write-Output "Go to Reboot"
pause
}
}
MAPPING_JOB
GUI Script
Param (
$Path,
[string]$Pattern,
[int]$MaxAttempts = 10
)
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
# set things up for the timer
$script:nAttempts = 0
$timer = New-Object System.Windows.Forms.Timer
$timer.Interval = 1000 # 1 second
$timer.Add_Tick({
$global:Result = $null
$script:nAttempts++
# use the same test as you did outside of the GUI
# try and find the first file that contains your search pattern
$file = Select-String -Path $Path -Pattern $Pattern -SimpleMatch -ErrorAction SilentlyContinue | Select-Object -First 1
if ($file) {
$file = Get-Item -Path $file.Path
$global:Result = [PSCustomObject]@{
Exists = $true
FileName = $file.FullName
Directory = $file.DirectoryName
Attempts = $script:nAttempts
}
$timer.Dispose()
$Form.Close()
} elseif ($script:nAttempts -ge $MaxAttempts) {
$global:Result = [PSCustomObject]@{
Exists = $false
FileName = $null
Directory = $null
Attempts = $script:nAttempts
}
$script:nAttempts = 0
$timer.Dispose()
$Form.Close()
}
})
Add-Type -AssemblyName System.Windows.Forms
[System.Windows.Forms.Application]::EnableVisualStyles()
Add-Type -Assembly System.Windows.Forms
$form = New-Object Windows.Forms.Form
$Form.WindowState = 'Maximized'
$img = [Drawing.Image]::FromFile('.\ATGM.png')
$form.BackgroundImage = $img
$form.BackgroundImageLayout = 'Center'
$Form.BackColor = "#ffffff"
$Form.Add_Shown({ $timer.Start() })
[void]$Form.ShowDialog()
# clean up when done
$Form.Dispose()