Battle Faction Puzzle 9

This topic contains 5 replies, has 2 voices, and was last updated by  Axel Bøg Andersen 2 months, 1 week ago.

  • Author
    Posts
  • #95640

    Axel Bøg Andersen
    Participant

    A take on Puzzle 9

    It does not make much sense to log the success or failure of the scheduled job from the job. If it fails, the code will not execute and if it runs, it will be in a running state, when it's supposed to log the success. The log in Task Scheduler will suffice.

  • #95655

    Axel Bøg Andersen
    Participant

    Hmm, there is in fact a different log on ScheduledJobs – have to dig in to that.

    So far Clear-RecycleBin produces an error and the log-file gets overwritten for every run.

  • #95658

    Axel Bøg Andersen
    Participant

    Fixed logging, append works now. For some reason, $LogFile.exists did not return true when running as ScheduledJob.

    Recyclebin gets emptied but not according to the ScheduledJob log.

    Log cleanup needs to be done.

  • #95688

    Yubu
    Participant

    "https://powershell.org/forums/topic/get-directory-size/"
    "https://www.powershelladmin.com/wiki/Get_Folder_Size_with_PowerShell,_Blazingly_Fast"

    I've faced the issue of getting dir size on our prod machines few years ago. The (dir | measure length -sum) was incredibly slow on large and deep directories with lots of files. I've found Joakim's blog with COM object: $FSO = New-Object -ComObject Scripting.FileSystemObject; $FSO.GetFolder('C:\temp').Size. This method works blazingly fast.

    Run on C:\:
    measure-command {dir -dir -force | select fullname,@{n="dirSize";e={$FSO.GetFolder("$_").Size}},@{n='subFolders';e={(dir -force -dir $_).count}},@{n="files";e={(dir -force $_ | ? mode -notmatch "d").count}}} | select Milliseconds
    vs
    measure-command {dir -dir -force | select fullname,@{n="dirSize";e={(dir -force -rec $_ | measure length -sum).sum}},@{n='subFolders';e={(dir -force -dir $_).count}},@{n="files";e={(dir -force $_ | ? mode -notmatch "d").count}}} | select Milliseconds

    The first gives 665ms on my 1TB m.2 SSD, the second ... i've Ctrl-C'ed after more than 5 min of waiting.

    So all credit goes to Joakim Svendsen, who wrote many excellent stuff, including SSH module, which saves me tons of time.

    #c:\tmp\lesson9.ps1
    $script=($MyInvocation.MyCommand).Name
    $scriptPath=($MyInvocation.MyCommand).Definition
    $scriptLog=$scriptPath+".log"
    
    $timeStart=(get-date)
    (Get-Date -format 'yyyy/MM/dd-hh:mm:ss') + "  --- start ---" > $scriptLog
    
    $FSO = New-Object -ComObject Scripting.FileSystemObject
    $env:temp | select FullName,@{n="dirSize(Bytes)";e={$FSO.GetFolder("$_").Size}},@{n='subDirs#';e={(dir -force -dir $_).count}},@{n="Files#";e={(dir -force $_ | ? mode -notmatch "d").count}} | tee -append $scriptLog
    dir -force -rec $env:temp | ? LastWriteTime -lt (get-date).addhours(-24) | del -force -rec -ea ignore
    dir -force -rec "$($env:temp | split-path -Qualifier)\`$Recycle.Bin" | del -force -rec -ea ignore
    $env:temp | select FullName,@{n="dirSize(Bytes)";e={$FSO.GetFolder("$_").Size}},@{n='subDirs#';e={(dir -force -dir $_).count}},@{n="Files#";e={(dir -force $_ | ? mode -notmatch "d").count}} | tee -append $scriptLog
    
    $timeStop=(get-date)
    $timeElapsed=$timeStop-$timeStart
    "Time Elapsed: " + $timeElapsed >> $scriptLog
    (Get-Date -format 'yyyy/MM/dd-hh:mm:ss') + "  --- start ---" >> $scriptLog
    
    Register-ScheduledTask -TaskName Lesson9 -InputObject (New-ScheduledTask -Action (New-ScheduledTaskAction –Execute "powershell -nologo -noprofile -file c:\tmp\lesson9.ps1") -Trigger (New-ScheduledTaskTrigger -Daily -At 3am) -Settings (New-ScheduledTaskSettingsSet))
    
  • #95726

    Axel Bøg Andersen
    Participant

    Log cleanup is automated in ScheduledJob. Default is 32. Since there are no details on how many runs is to be maintained, I'll just stick with 32 🙂

    Still get this error eventhough the recycle bin is emptied:
    @{Exception=System.Management.Automation.RemoteException: The system cannot find the path specified; TargetObject=RecycleBin; FullyQualifiedErrorId=FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand; InvocationInfo=; ErrorCategory_Category=7; ErrorCategory_Activity=Clear-RecycleBin; ErrorCategory_Reason=Win32Exception; ErrorCategory_TargetName=RecycleBin; ErrorCategory_TargetType=String; ErrorCategory_Message=InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception; SerializeExtendedInfo=False; ErrorDetails_ScriptStackTrace=at , : line 17} System.Management.Automation.RemoteException System.Management.Automation.RuntimeException System.SystemException System.Exception System.Object System.Management.Automation.RemoteException: The system cannot find the path specified Deserialized.System.ComponentModel.Win32Exception Deserialized.System.Runtime.InteropServices.ExternalException Deserialized.System.SystemException Deserialized.System.Exception Deserialized.System.Object System.ComponentModel.Win32Exception (0x80004005): The system cannot find the path specified 3 -2147467259 The system cannot find the path specified Deserialized.System.Collections.ListDictionaryInternal Deserialized.System.Object -2147467259 Deserialized.System.Management.Automation.InvocationInfo Deserialized.System.Object System.Management.Automation.InvocationInfo Clear-RecycleBin Deserialized.System.Collections.Generic.Dictionary`2[[System.String, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089],[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Deserialized.System.Object Deserialized.System.Collections.Generic.List`1[[System.Object, mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] Deserialized.System.Object 17 1 1 Clear-RecycleBin -DriveLetter C -Force_x000A_ At line:17 char:1_x000D__x000A_+ Clear-RecycleBin -DriveLetter C -Force_x000D__x000A_+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ Clear-RecycleBin 0 0 false Internal The system cannot find the path specified false The system cannot find the path specified System.Collections.ListDictionaryInternal System.Object -2146233087 RecycleBin FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand 7 Clear-RecycleBin Win32Exception RecycleBin String InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception false at , : line 17 RecycleBin FailedToClearRecycleBin,Microsoft.PowerShell.Commands.ClearRecycleBinCommand 7 Clear-RecycleBin Win32Exception RecycleBin String InvalidOperation: (RecycleBin:String) [Clear-RecycleBin], Win32Exception false at , : line 17

  • #95955

    Axel Bøg Andersen
    Participant

    Really odd. The ScheduledJob does not run at the scheduled time ...

You must be logged in to reply to this topic.