0

I have a log file that is generated by a scheduled ps1 script. It was working fine when it was just writing lines to the file, but now when I include some file manipulation it's got some fowled/fouled up format that prevents the execution from operating correctly.

The first two lines of the image below are how I would like to see and process the file, the last two are a sample of how its doing it now.

enter image description here

As a result the deletion of the X for the previous record has no effect:

$bootTime = (Get-WmiObject Win32_OperatingSystem).LastBootUpTime
$formBootTime = [Management.ManagementDateTimeConverter]::ToDateTime($bootTime)
$file = Get-Content c:\myUptimeLogFile.txt
$fileMeasure = ($file | Measure-Object)
$numberOfLines = $fileMeasure.Count
$numberOfWords = ($file | Select -Index ($numberOfLines -1) | Measure-Object -Word)
$Line = $file | Select -Index ($numberOfLines -2)
$Line.Split(",")
$wordArray = $Line.Split(",")
$LastLineBT = $wordArray[0]
if ($LastLineBT -eq $formBootTime) {
    $unmark = "true"
} else {
    $unmark = "false"
}

if($unmark -eq "true") {
    $output = "C:\myUptimeLogFile_NEW.txt"

    $file[-2] = $file[-2] -replace 'X', ' '
    $file | Set-Content $output -Encoding ASCII
    Remove-Item -Path "C:\myUptimeLogFile_SBY.txt"
    Rename-Item -Path "C:\myUptimeLogFile.txt" -NewName "C:\myUptimeLogFile_SBY.txt"
    Rename-Item -Path "C:\myUptimeLogFile_NEW.txt" -NewName "C:\myUptimeLogFile.txt"
}

$uptime = (Get-Date) - $formBootTime
"$formBootTime,$(Get-Date),{0:00},{1:00},{2:00},{3:00},X" -f $uptime.Days,$uptime.Hours,$uptime.Minutes,$uptime.Seconds >> C:\myUptimeLogFile.txt

enter image description here

If the $unmarked is true that's when the files shuffle and and the format apparently shifts. Until then, the >> uses the correct format.

As per comments, checking Add-Content for the final line of the code.

Update - solution:

$newLine = "$formBootTime,$(Get-Date),{0:00},{1:00},{2:00},{3:00},X" -f $uptime.Days,$uptime.Hours,$uptime.Minutes,$uptime.Seconds

Add-Content -Path "C:\TestLog1.txt" -Value $newLine -Encoding "ASCII"
Ansgar Wiechers
  • 193,178
  • 25
  • 254
  • 328
wolfsshield
  • 757
  • 5
  • 14
  • 2
    If you using `-encoding ASCII` as a parameter for the output command does that fix the issue? – Riley Carney Apr 30 '19 at 19:29
  • Where are you importing and exporting the file in code? – Riley Carney Apr 30 '19 at 19:31
  • @Riley Carney Edited the code snippet. – wolfsshield Apr 30 '19 at 19:36
  • [1] `Rename-Item` does NOT have a `-Encoding` parameter. [2] if you use CIM instead of WMI you will get datetime objects instead of filetime objects. [3] what on earth is the part of your code before `if($unmark -eq "true"){ ` _doing_? i am unable to figure it out ... [*blush*] – Lee_Dailey Apr 30 '19 at 19:42
  • @Lee_Dailey Took them out, it's not throwing errors save on the last line, but it isn't having any impact. When I change the names of the file manually and run the script it loads the first few lines correctly, but anytime it it goes through the renaming process when the final line write it writes in the funky format. Then once that format is there, every new line is in that format – wolfsshield Apr 30 '19 at 19:47
  • 2
    @wolfsshield - it looks like LotPings has pointed out the problem - using redirection instead of `Add-Content` ... [*grin*] – Lee_Dailey Apr 30 '19 at 19:48
  • @LotPings Thanks to each of you. That combination of stuff is what I needed and with the search terms I used I would never have found LotPings link – wolfsshield Apr 30 '19 at 20:05

0 Answers0