File Compression Script Hangs

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()
}
}

April 28th, 2015 12:30am

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 22 hours 55 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 4:10am

You should not alter the files in c:\windows.  This area is owned and managed by the system.  Changes can have unforeseen consequences.

April 28th, 2015 4:38am

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
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 8:09am

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
April 28th, 2015 8:09am

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
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 8:09am

Thats seems to get me further but now I'm getting the error below.

April 28th, 2015 9:43am

Thank you for the response.  I'll take this into consideration.
Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 9:43am

You've still got a single slash in one of your paths, it should be 
"D:\\Program Files\\VERITAS\\Patch"
April 28th, 2015 9:49am

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.

Free Windows Admin Tool Kit Click here and download it now
April 28th, 2015 11:23am

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.
April 28th, 2015 2:30pm

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?

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:15pm

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")

April 29th, 2015 12:29pm

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()
    }
}
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:36pm

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'){

April 29th, 2015 12:47pm

Meh, either way works, although i agree that is the better way to go about it.
  • Marked as answer by Kreifer1 13 hours 57 minutes ago
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:49pm

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.

April 29th, 2015 12:51pm

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')

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:53pm

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>

April 29th, 2015 12:54pm

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

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 12:58pm

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.
April 29th, 2015 12:59pm

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>

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 1:02pm

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>

April 29th, 2015 1:04pm

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.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 1:20pm

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.  


April 29th, 2015 1:45pm

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.

Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 1:46pm

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.
April 29th, 2015 1:54pm

This worked!  Thank you to everyone who responded to my post!
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 4:25pm

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
April 29th, 2015 4:47pm

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
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 4:47pm

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
April 29th, 2015 4:47pm

Glad it's sorted :) Jrv should be marked as the answer really, his logic is strong.
Free Windows Admin Tool Kit Click here and download it now
April 29th, 2015 5:05pm

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.
April 29th, 2015 5:49pm

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.

Free Windows Admin Tool Kit Click here and download it now
May 1st, 2015 2:48pm

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.

May 1st, 2015 2:51pm

thanks
Free Windows Admin Tool Kit Click here and download it now
May 1st, 2015 3:01pm

You're welcome.
May 1st, 2015 3:02pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics