Help Understanding and Simplifying an Expression

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Karson Van Meeteren Karson Van Meeteren 1 year, 11 months ago.

  • Author
    Posts
  • #21615
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    My ultimate goal in scripting is to avoid complex expressions at all costs – not only because I really don't understand them, but also because I don't expect someone after I'm long gone to just look at it and quickly see what it's purpose is.

    That being said, there are times where Googling for help on a script results in use of one just to get the script off the ground and working. One of those cases is a script I"m running as a scheduled task for the creation of an empty zip file to soon thereafter begin putting archived event logs into.

    #tests if the zip is present, if not create it, if present, append
    #creating sz alias for 7-zip
    Set-Alias sz "\\server\d$\powershell\scheduled\lenel\7z.exe"
    If (!(Test-Path $archiveFile)) { Set-Content $archiveFile ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) } #creates empty zip file - DO NOT MODIFY EXPRESSION.  I HAVE NO IDEA WHAT IT DOES, BUT COMMENTING THIS OUT MAKES SCRIPT NOT WORK!
    $evtxFiles | %{sz a -tzip $archiveFile $_.FullName}
    
    #Commented out - couldn't get appending to work using write-zip
    #write-zip $evtxFiles "\\iaamlenels10\d$\os_logs\Monthly_Lenel_Archives_$timestamp.zip" -level 9 -append -flattenpaths
    

    Can anyone offer any insight on what this does, and perhaps a way to simplify it?

    Thank you!
    Karson

  • #21617
    Profile photo of Dave Wyatt
    Dave Wyatt
    Moderator

    If you're talking about that Set-Content line, it's creating an empty zip file with the proper header bytes (or at least I assume it is; I haven't actually tested this code, but I know that's what needs to happen.)

    For code clarity, you could put that expression into a function called something like New-ZipFile, or Set-EmptyZipFileHeaderBytes, or whatever. That way the function name gives you some context around what this really means: ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) , and the code that calls the function is also much more tidy to read.

  • #21619
    Profile photo of Karson Van Meeteren
    Karson Van Meeteren
    Participant

    If you're talking about that Set-Content line, it's creating an empty zip file with the proper header bytes (or at least I assume it is; I haven't actually tested this code, but I know that's what needs to happen.)

    For code clarity, you could put that expression into a function called something like New-ZipFile, or Set-EmptyZipFileHeaderBytes, or whatever. That way the function name gives you some context around what this really means: ("PK" + [char]5 + [char]6 + ("$([char]0)" * 18)) , and the code that calls the function is also much more tidy to read.

    That makes sense and is certainly something I'll investigate on using. Right now, I'm trying to see if there's any way to massage built-in cmdlets like Write-Zip to accomplish the same task. Unfortunately,

    write-zip $evtxFiles "\\sourceserver\d$\os_logs\Monthly_Archives_$timestamp.zip" -level 9 -append -flattenpaths 

    isn't doing the trick.

You must be logged in to reply to this topic.