$pass = ConvertTo-SecureString ("Paper123" + [char]33 + "@#") -AsPlainText -Force $cred = New-Object PSCredential("INTRANET\sysadmin", $pass) # Read the last 40 lines of the sync log $logContent = [System.IO.File]::ReadAllText("\\192.168.0.6\C$\Shares\test\scripts\sync-from-nas.log") $lines = $logContent -split "`n" $lastLines = $lines | Select-Object -Last 40 Write-Output "=== LAST 40 LOG LINES ===" $lastLines | ForEach-Object { Write-Output $_ } # Check process status $procs = Get-WmiObject -ComputerName 192.168.0.6 -Credential $cred -Class Win32_Process -Filter "Name='powershell.exe'" 2>$null $syncRunning = $false foreach ($p in $procs) { if ($p.CommandLine -match "BulkSync|86400") { $syncRunning = $true } } Write-Output "" if ($syncRunning) { Write-Output "STATUS: Still running..." } else { Write-Output "STATUS: COMPLETED" }