none
File Compression Script Hangs RRS feed

  • Question

  • Hello,  I'm trying to put together a file cleanup and compress script and am having trouble getting the compression piece to work correctly.  I'm also not getting an error to go off of so I'm looking for some help and direction.  This is what I have right now.  My goal is to cycle through the paths listed and compress only those directories that are not already compressed.  When I run this I get no output or errors.  The script just hangs.  I've tested on several windows 2k8 servers with the same result.  Am I missing something here?  Any help would be greatly appreciated.

    $dr1="C:\windows\assembly" 
    $dr2="C:\windows\installer" 
    $dr3="C:\Windows\Logs\DISM" 
    $dr4="D:\Program Files\VERITAS\Patch"
    $dr5="D:\Interwoven\OpenDeployNG\log'"

    $folderstocompress = get-wmiobject -Query "select * from Win32_Directory where Name = 'dr1' or Name =  'dr2' or name = 'dr3' or name =  'dr4' or name = 'dr5'"

    foreach ($folder in $folderstocompress)
    {
    if($folder.compressed = $false)
    {
    $folder.compress()
    }
    }

    Tuesday, April 28, 2015 4:27 AM

Answers

  • Meh, either way works, although i agree that is the better way to go about it.
    • Marked as answer by Kreifer1 Wednesday, April 29, 2015 5:10 PM
    Wednesday, April 29, 2015 4:47 PM

All replies

  • Backslashes need to be escaped and you aren't using the actual variables in the query, try this - 

    $dr1="C:\\windows\\assembly" 
    $dr2="C:\\windows\\installer" 
    $dr3="C:\\Windows\Logs\\DISM" 
    $dr4="D:\\Program Files\\VERITAS\\Patch"
    $dr5="D:\\Interwoven\\OpenDeployNG\\log'"
    
    $folderstocompress = get-wmiobject -Query "select * from Win32_Directory where Name = '$dr1' or Name =  '$dr2' or name = '$dr3' or name =  '$dr4' or name = '$dr5'"
    
    foreach ($folder in $folderstocompress)
    {
    if($folder.compressed = "False")
    {
    $folder.compress()
    }
    }


    • Edited by Braham20 Tuesday, April 28, 2015 8:13 AM
    Tuesday, April 28, 2015 8:09 AM
  • You should not alter the files in c:\windows.  This area is owned and managed by the system.  Changes can have unforeseen consequences.


    \_(ツ)_/

    Tuesday, April 28, 2015 8:37 AM
  • Thats seems to get me further but now I'm getting the error below.

    Tuesday, April 28, 2015 1:42 PM
  • Thank you for the response.  I'll take this into consideration.
    Tuesday, April 28, 2015 1:42 PM
  • You've still got a single slash in one of your paths, it should be 
    "D:\\Program Files\\VERITAS\\Patch"
    Tuesday, April 28, 2015 1:48 PM
  • I added the missing slash.  Now I get no error but the script seems to hang again.  I confirmed that none of the folders show compressed.

    Tuesday, April 28, 2015 3:22 PM
  • It takes a while to compress a folder, you will need to wait for it to finish. Try it with a test folder with only a couple of files in, it definitely works.
    Tuesday, April 28, 2015 6:29 PM
  • I've tried this on several windows 2k8 servers in our environment and even on my personal PC but I still get no result as if it hangs.  I've been copy/pasting the code into PS so I figured I would try to call it instead.  This is the result.  Any thoughts?  Am I calling this correctly?

    Wednesday, April 29, 2015 4:13 PM
  • It will hang until it finishes, nothing will be returned. The code in the file needs to be:

    If($folder.compressed ="false") not if($folder.compress="false")

    Wednesday, April 29, 2015 4:27 PM
  • Syntax is faulty:

    foreach ($folder in $folderstocompress){
        if($folder.compressed){
            Write-Host "$folder is already compressed" -fore green
        }else{
            Write-Host "Compressing $folder" -fore green
            #$folder.compress()
        }
    }


    \_(ツ)_/



    • Proposed as answer by jrv Wednesday, April 29, 2015 4:35 PM
    • Edited by jrv Wednesday, April 29, 2015 4:47 PM
    Wednesday, April 29, 2015 4:35 PM
  • It takes a while to compress a folder, you will need to wait for it to finish. Try it with a test folder with only a couple of files in, it definitely works.

    You cannot do this:

    if($folder.compressed = 'false'){

    It will always be true.

    Try it:

    It really should be

    if($folder.compressed -eq 'true'){


    \_(ツ)_/

    Wednesday, April 29, 2015 4:45 PM
  • Meh, either way works, although i agree that is the better way to go about it.
    • Marked as answer by Kreifer1 Wednesday, April 29, 2015 5:10 PM
    Wednesday, April 29, 2015 4:47 PM
  • It will hang until it finishes, nothing will be returned. The code in the file needs to be:

    If($folder.compressed ="false") not if($folder.compress="false")

    That will not work either.  $folder.compressed is a booolean and can and should be directly tested.

    if($folder.compressed ){

    Doing tests of Booleans against a value will, most often, create havoc as you can see.


    \_(ツ)_/

    Wednesday, April 29, 2015 4:49 PM
  • Meh, either way works, although i agree that is the better way to go about it.

    Either way does NOT work.

    if($folder.compressed = 'true') tests nothing and wil always be true no matter what compressed is set to.

    if($true='true')


    \_(ツ)_/

    Wednesday, April 29, 2015 4:51 PM
  • Note that the following are always true:

    PS C:\scripts> $test='true'
    PS C:\scripts> if($test='true'){'true'}else{'false'}
    true
    PS C:\scripts> if($test='false'){'true'}else{'false'}
    true
    PS C:\scripts> $test='false'
    PS C:\scripts> if($test='true'){'true'}else{'false'}
    true
    PS C:\scripts> if($test='false'){'true'}else{'false'}
    true
    PS C:\scripts>
    


    \_(ツ)_/

    Wednesday, April 29, 2015 4:53 PM
  • I should note that WMI lets us assign properties with illegal values until we do a "Put()" which will then record the error.

    PS C:\scripts> $folder.compressed='false'
    PS C:\scripts> $folder.put()
    Exception calling "Put" with "0" argument(s): "Provider is not capable of the attempted operation "
    At line:1 char:1
    + $folder.put()
    + ~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : DotNetMethodException


    \_(ツ)_/

    Wednesday, April 29, 2015 4:56 PM
  • I shall have to take your word for it, i'm on my holidays now and i've no access to a computer until i get back! I won't be at all suprised to find you are correct though.
    Wednesday, April 29, 2015 4:57 PM
  • Get-Member will show us the property:
    Compressed                  Property      bool Compressed {get;set;}

    Note that it is a "bool"

    See what happens when assigned:

    PS C:\scripts> $folder.Compressed='hello'
    PS C:\scripts> $folder.Compressed
    True
    PS C:\scripts> $folder.Compressed='false'
    PS C:\scripts> $folder.Compressed
    True
    PS C:\scripts> $folder.Compressed=$null
    PS C:\scripts> $folder.Compressed
    PS C:\scripts> $folder.Compressed='false'
    PS C:\scripts> $folder.Compressed
    True

    PS C:\scripts> $folder.Compressed=$false
    PS C:\scripts> $folder.Compressed
    FalsePS C:\scripts>


    \_(ツ)_/

    Wednesday, April 29, 2015 5:01 PM
  • Note that the following are always true:

    PS C:\scripts> $test='true'
    PS C:\scripts> if($test='true'){'true'}else{'false'}
    true
    PS C:\scripts> if($test='false'){'true'}else{'false'}
    true
    PS C:\scripts> $test='false'
    PS C:\scripts> if($test='true'){'true'}else{'false'}
    true
    PS C:\scripts> if($test='false'){'true'}else{'false'}
    true
    PS C:\scripts>
    


    \_(ツ

    Sorry, i'm about 3 posts behind on this conversation and using my mobile to reply, it is a frustrating experience!


    Wednesday, April 29, 2015 5:02 PM
  • Sorry, i'm about 3 posts behind on this conversation and using my mobile to reply, it is a frustrating experience!

    Braham - once you get back in front of a PC you may want to vote up this suggestion:

    https://social.technet.microsoft.com/Forums/en-US/79ce8e12-53b9-433f-a240-73c518e70fa3/please-create-a-mobile-website-version-of-the-technet-forums?forum=suggest

    I think this would be great, but I doubt we'll get traction on it unless there's much more of a demand.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Wednesday, April 29, 2015 5:19 PM
  • I updated the script with this code and It appears to work going off of the write-host, but when I check the folders the txt is not blue, denoting compression, and when I go into the properties the option to compress is not checked.

    To double check I ran the script again, and the writeout shows that it is attempting to compress the same folders that it did during the first run.  This confirms that they were not compressed.  See screen shots below.  I checked all directories listed in the script with the same result.  


    Wednesday, April 29, 2015 5:44 PM
  • Sorry, i'm about 3 posts behind on this conversation and using my mobile to reply, it is a frustrating experience!

    Braham - once you get back in front of a PC you may want to vote up this suggestion:

    https://social.technet.microsoft.com/Forums/en-US/79ce8e12-53b9-433f-a240-73c518e70fa3/please-create-a-mobile-website-version-of-the-technet-forums?forum=suggest

    I think this would be great, but I doubt we'll get traction on it unless there's much more of a demand.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Thanks Mike. I will definitely add a vote, the current mobile experience is horrible.
    Wednesday, April 29, 2015 5:44 PM
  • I updated the script with this code and It appears to work going off of the write-host, but when I check the folders the txt is not blue, denoting compression, and when I go into the properties the option to compress is not checked.

    To double check I ran the script again, and the writeout shows that it is attempting to compress the same folders that it did during the first run.  This confirms that they were not compressed.  See screen shots below.  I checked all directories listed in the script with the same result.  


    You need to remove the hash symbol before $folder.compress(), the # is used as a comment character, anything that comes after it is ignored.
    Wednesday, April 29, 2015 5:52 PM
  • This worked!  Thank you to everyone who responded to my post!
    Wednesday, April 29, 2015 8:23 PM
  • Glad it's sorted :) Jrv should be marked as the answer really, his logic is strong.
    Wednesday, April 29, 2015 9:04 PM
  • Glad it's sorted :) Jrv should be marked as the answer really, his logic is strong.

    Thanks.  Not strong - just logical.   (Hey that's funny.  Imagine... logical logic. Who would have guessed.

    \_(ツ)_/

    Wednesday, April 29, 2015 9:47 PM
  • Hey one last question.  I have a line to remove temp files using %windir% but getting "path does not exist."

    We have some servers where the windows directory was renamed to the asset tag during build.  I found some posts online but nothing that seems to be working.

    Friday, May 1, 2015 6:47 PM
  • Hi,

    PowerShell doesn't use variables in the same way. You want $env:windir instead of %windir%.

    If you continue to have problems, I suggest starting a new thread.


    Don't retire TechNet! - (Don't give up yet - 13,225+ strong and growing)

    Friday, May 1, 2015 6:51 PM
  • thanks
    Friday, May 1, 2015 7:00 PM