Anybody dabbling in Powershell?

stevo7624stevo7624 Member Posts: 14 ■□□□□□□□□□
I am new to Powershell and I am trying to get an understanding of it through borrowing script and making it work for my system. I have a script that I thought would take files from a target folder and zip them into a destination folder. It is creating zip folders in my destination folder based on the names of the files in the target folders, but the zipped folders are empty. How do I get the zipped folders to contain the files from the target folder? I am obviously missing some script or haven't manipulated it enough to work for me. Was hoping to get some expert advice to help aide my journey into Powershell scripting.
Here is the script:
[HTML]
[System.Reflection.Assembly]::LoadFrom("C:\Powershell\Ionic.Zip.dll") $Target = "C:\PSTarget" $Destination = "C:\PSDest" function ZipItUp { $outputFile = [IO.FileInfo] "$Destination\$fldr.zip" $zipfile = new-object Ionic.Zip.ZipFile $selfExtractOptions = New-Object Ionic.Zip.SelfExtractorSaveOptions $selfExtractOptions.Flavor = [Ionic.Zip.SelfExtractorFlavor]::ConsoleApplication $selfExtractOptions.DefaultExtractDirectory = $outputFile.Directory.FullName $selfExtractOptions.RemoveUnpackedFilesAfterExecute = $false $e = $zipfile.AddDirectory("$Target\$fldr") $zipfile.UseZip64WhenSaving = [Ionic.Zip.Zip64Option]::Always $zipfile.SaveSelfExtractor($outputFile.FullName, $selfExtractOptions) $zipfile.Dispose(); } foreach ($fldr in Get-ChildItem $Target) { Write-Host "Zipping up $fldr to PSDest" ZipItUp }[/HTML]


Thank you in advance for any help.

Steve

Comments

  • undomielundomiel Member Posts: 2,818
    Well your formatting makes it really difficult to look through that. I don't see where you are actually adding something to the zip file though. You have $e = $zipfile.AddDirectory("$Target\$fldr") but I don't see anything being done with $e. Also I don't know if AddDirectory will work with a single file as a quick search seems to say that it zips up directories recursively. AddFile seems to be more like what you are looking for if you're wanting one zip file per a file found. Where you may want to make your loop use Get-ChildItem | where {$_.psIsContainer -eq $false} so that you skip any directories in the target directory.
    Jumping on the IT blogging band wagon -- http://www.jefferyland.com/
  • FloOzFloOz Member Posts: 1,614 ■■■■□□□□□□
    i am taking a powershell class in college right now lol to bad i hate scripting icon_cheers.gif
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    You sample code is really hard to read. But that being said I think you need to restructure the code to use a function that is passed some params (ie the root folder to add to the zip file) rather than doing it the way you are. When I restructure your code like this:
    [System.Reflection.Assembly]::LoadFrom("C:\zip-v1.9\Release\Ionic.Zip.dll") 
    $Target = "C:\PSTarget" 
    $Destination = "C:\PSDest"   
    
    
        
        $outputFile = [IO.FileInfo] "$Destination\$fldr.zip"     
        $zipfile = new-object Ionic.Zip.ZipFile 
        $selfExtractOptions = New-Object Ionic.Zip.SelfExtractorSaveOptions     
        $selfExtractOptions.Flavor = [Ionic.Zip.SelfExtractorFlavor]::ConsoleApplication     
        $selfExtractOptions.DefaultExtractDirectory = $outputFile.Directory.FullName     
        $selfExtractOptions.RemoveUnpackedFilesAfterExecute = $false
        foreach ($fldr in Get-ChildItem $Target) 
        {
            $zipfile.AddDirectory("$Target\$fldr")
        }
        $zipfile.UseZip64WhenSaving = [Ionic.Zip.Zip64Option]::Always       
        $zipfile.SaveSelfExtractor($outputFile.FullName, $selfExtractOptions)       
        $zipfile.Dispose(); 
    
    It works fine. My suggestion is that you make this a function that takes $target and $destination as params and it should work as expected.
  • stevo7624stevo7624 Member Posts: 14 ■□□□□□□□□□
    I actually have it all figured out Thanks for the replies.

    Steve
  • cmitchell_00cmitchell_00 Member Posts: 252 ■■■□□□□□□□
    I have used Powershell for basic scripting i.e. AD. login or printer installs.
  • RobertKaucherRobertKaucher Member Posts: 4,299 ■■■■■■■■■■
    stevo7624 wrote: »
    I actually have it all figured out Thanks for the replies.

    Steve

    You should really post your code so that others can benefit from what you learned. It's a good way to increase the value of the community.
Sign In or Register to comment.