March 8, 2010

Powershell: List the size of folders or sub folders

Filed under: Script — Tags: — Kevmar @ 6:29 am

I was wanting to see how big my users’ home folders are.  With out quotas in place, I decided to use powershell.  I put together a function that will list the size of a folder that I pass to it. I wrote it in a way that if I pass a collection of folders to it, it will list the sizes of those folders individually.   This way I can spot check individual folders or check every folder in the home folders location.

This works even better for me because I need it to list the sub folder of each folder in the home folder location.  My home folders are placed in department folders.  So I have to add that extra step to every script I use to make sure it processes at the right level.  Allowing my function to work on a collection of folders allows powershell to handle this for me. Here is the syntax in action:

#Single folder
PS> Get-Item . | Get-FolderSize

#Sub folders
PS> Get-ChildItem . | Get-FolderSize

#Sub Sub Folders
PS> Get-ChildItem . | %{Get-ChildItem $_.FullName} | Get-FolderSize

It is required that you pass your directory objects to it.  I may update it to allow you to pass a folder path later. But for my needs, this is exactly how I will use it in my scripts.  Here is the full code for Get-FolderSize:

function Get-FolderSize ($_ = (get-item .))  {
  Process {
    $ErrorActionPreference = "SilentlyContinue"
    $length = (Get-ChildItem $_.fullname -recurse | Measure-Object -property length -sum).sum
    $obj = New-Object PSObject
    $obj | Add-Member NoteProperty Name ($_.Name)
    $obj | Add-Member NoteProperty FullName ($_.FullName)
    $obj | Add-Member NoteProperty Length ($length)
    $obj | Add-Member NoteProperty MB ("{0,13:N2}" -f ($length / 1MB))
    Write-Output $obj
  }
}

March 5, 2010

Powershell: Move files with the pipe

Filed under: Uncategorized — Kevmar @ 6:28 am

One of the games I play has a built in screen shot capture button. Every time you press it, a jpg gets saved into the screenshot folder. I have lots of files in this folder and its hard to manage. When I am taking screen shots for a blog post on another site, I will move all that I take that day to another folder. To day I broke out the powershell to do it for me. There are many ways to do this but I was playing with using the pipeline to do things tonight and I ended up with this command:

PS> ls C:\Program Files\World of Warcraft\Screenshots\*.jpg | sort-object lastwritetime | select -last 14 | move -Destination .\BlogFolder

March 4, 2010

Powershell: WGet as Get-WebPage

Filed under: Script — Tags: — Kevmar @ 5:10 am

WGet is an old favorite of mine.  It is very easy to replicate in powerhshell by calling out to the system.web.WebClient object.

function Get-WebPage{
  Process {
   if($_ -ne $null){
      Write-Output (new-object system.net.WebClient).DownloadString($_)
    }
    elseif ($args[0] -ne $null){
      Write-Output (new-object system.net.WebClient).DownloadString($args[0])
    }
  }
}

Get-WebPage "http://www.ithinkincode.com"

All this gives you is the raw text of the page. You can then pipe it into a file or into another function.

March 2, 2010

Powershell Grep to find old server names in batchfiles

Filed under: Script, Uncategorized — Tags: — Kevmar @ 11:40 am

I was troubleshooting a user access problem today and found that they had a old mapping to an old server in the login script.  We just moved all our data to a different server so it was expected to have a few issues like this.  We do use DFS for most of our shares so the transition was very smooth for most people.  Just a few exceptions needed fixed.

I think we just over looked the log in scripts so I needed a script to check them all.  This is where I should just use grep but I wanted to see how to do it in powershell.  Here is the command I came up with:

PS> get-childitem \\domain\netlogon -include *.bat -recurse | select-string "oldserver"

Here is another way to do the same thing with a few shorcuts that could be easier to remember at the command line:

PS> cd \\domain\netlogon
PS> dir -recurse | select-string "oldserver"

dir is short for get-childitem.  If all you have are .bat files in netlogon you can skip the include.  If you have other install files or packages, use the include to save time.

February 27, 2010

Powershell replaced my favorite DOS command

Filed under: Uncategorized — Kevmar @ 11:52 am

My favorite dos shell command is the for loop.  I have used that so many time to automate things for me.

C:\> for /f %i in ('dir /b') do echo %i

I would either pipe that into a file and use it in another for loop, or replace the echo with a command.  ’dir /b’ is the most common command for me to loop on.  That lists all the files in the location but only gives the file name.  I recently discovered powershell and it has this built in with more power.

PS> dir | %{echo $_.name}

Here is the full command without the shorthand.

PS> get-childitem | foreach-object{echo $_.name}

With powershell its much easier to chain things together.  In my CMD shell I would do a for loop and dump it to a file, then i would for loop on that file to a new file.  I would repeat that to get the data the way I want for my final loop command.

Lets say all of my home folders for my users are grouped in department folders and I want to list user folders where the user is not the owner.  I’m not even sure how I would do that off the command prompt but here is how I would do it with powershell.

PS> get-childitem . | foreach-object{$_.getdirectories()} | where-object {(get-acl $_.fullname).owner -ne "domain\" + $_.name} | format-table fullname

January 21, 2010

Tower Defender

Filed under: Uncategorized — Kevmar @ 11:45 am

One of my favorite mods for Warcraft 3 was tower defender.  I remember killing lots of time on that.  Several different tower defenders were made including a 3v3 where you attacked the other side.  I don’t remember the name of that one but it added a different strategy to it.  I took it and added something special.

I added better pathing logic and a computer AI option.  I thought the pathing was an issue because people could build towers in a way that would make the mobs walk bad directions that they should not be walking.  At this point no other warcraft tower defender had a need for computer AI.

The way the pathing worked was each mob was told to got to a point, then it got to that point, it was given a new point to walk to.  From point A to B to C to D to end.  This map could have just told the mob to go to end, but it would hug the wall as it turned the corners.  Sending the mob to points that were in the middle of the walk way was better game play.  This opened up a exploit where you could block off the next point and route the mob far away from it.  The mob would follow your path, then head back to the point for its next orders.  Once it got its new point, it would walk past all your towers again.  You could walk the mob all the way to the end and let him go, and he would still travel all the way back.  His order was to go to B not the end.

I still used the points as where to walk to but added large areas that would issue new commands.  If the mob was directed past point B it would pick up the new order to go to point C even if it never made it to B.  So I gave it new orders based on what section it was in.  I moved the commands back so they go the new order before they got close to the target points.  So now the points acted as a general direction and not an exact command.

The computer AI was fun to add.  Because I wanted to keep the tower placement logic simple, I made it very aggressive on the atacks.  I would place towers at random in a fairly tight grouping.  I did not do any pathing tricks, but it got the job done.  It would place a set amount when it did place them.   The types of towers it placed depended on the funds available.  The aggressive attack is what made this work. 

The AI could send as many mobs as possible at almost the exact same time.  So the grouping was very tight for all the mobs it sent.  It also had perfect timing of when to send an attack.  It would save for a full attack at the end of a round and then release a 2nd full attack at the start of the next round.  It would blow all the gold starting with the most expensive mobs first working down.  This strat sent as many of the strongest mobs possible as early as possible.

This way is the only way to send mobs at your opponent.  If you staggered when you send them during the round, the other side would easily pick them off.  My AI would brute force and early victory.  With optimal spending every single round, the gold available out produced the person over defending. 

While it was not the best opponent, it was able to fill in when the teams were not balanced.  It made a great team member because you could fill in towers where it was weak knowing it was keeping up the attack.  You could also get a feel for its attack cycle so you could send your troops at the same time.

January 14, 2010

Kevmar, Where are you?

Filed under: Warcraft — Tags: — Kevmar @ 9:16 am

I used to be just ok at making gold in WOW.  My biggest market would be patch notes.  I would figure out what changes caused market changes and invested like mad.  Sometimes I get burned but more often I would make bank.  This last year something changed.  The general population started doing that too.  They saw the results people like me made early on and everyone shared what they thought dream shards would do.  This did not take that market away, but it made it swing a little more and made it harder to predict.  If everyone invests into something, your payout is smaller.

So I went online looking for other things people did to make gold.  I revisited some sites I looked at before and it indicated that inscription was the new market to be in.  I also discovered the JMTC forums where there were a lot of very helpful people.  I learned as much as I could from them and became an active member helping others and sharing my ideas.  I found myself stalking the forums a little too much.  I probably should have asked to be a moderator.  I developed my inscription method that lead me to write the core logic in KTQ from those forums.  Many people there helped me beta test and refine it.

I was talking about inscription soo much on the forum that I decided to move it to a blog.  On my blog, I didn’t have to wait for someone to respond too.  I could also lay out my story.  My blog grew at a fairly steady rate.  I commented on other blogs and I released a few mods.  KTQ is the most well known that drives most people here.  I did enjoy sharing my ideas and the ways I made gold.  Even my competition told me they liked to read my blog. 

I had a nice solid system in place.  Bulk herbs, large crafting queues, lots of inventory, and tons of posted auctions.  While I was doing that stuff, I could write on my blog and I had a lot to talk about.  Then my system kind of fell apart on me.  I had a lot of issues in real life that needed my attention out of town and shortly after I returned my account had issues for a few days.  My guild was also only doing easy content to make everyone feel good about killing stuff.  It just got me out of my pattern. 

I did keep posting and I did work auctions from time to time.  I did less and less and over the holiday break, I didn’t even log into wow.  So my unofficial break is more official.  Its also hard for me to come up with good warcraft topics while I am not playing it. I am just shifting my focus to other things for a while.  I kind of did the same thing at the end of Buring Crusade.  The content is winding down and I will not miss much if I take a break now.  I expect a full return for the next expansion if not sooner.   You may still see my mid level warlock riding nude in the barrens when my daughter is online.

So what exactly is my focus now? Programming.  So I am still around writting code. I have just started on a few mini 3D games.  I think they will be a lot of fun if I can make them look as good as I see them in my head.  I’ll talk more about it once I get some core features implemented.  So be ready to see more of me in the future.

January 6, 2010

IE Protected Mode and Networked HP Printers Hang

Filed under: Error, Solutions — Kevmar @ 2:53 pm

I ran into a problem printing from IE8 protected mode to a networked HP printer where it would freeze IE and lockup printing.  I had 2 computers act differently with this problem.

The first one where the issue was reported would lock up not only IE but also other printing.  After the crash, the print queue would not act correctly.  Printing test pages would give me an error saying that the server was not responding and that I needed to reboot it.  Other people could still print and test prints originating from the print server would print.  I also could not add new printers while this was happening.  Relogging did nothing.  a full reboot would return things to normal until it locked up later.

After a few crashes, we connected it to IE8 printing.  With a little more testing I discovered it would print fine with protected mode off but failed with it on.  We are trying our hardest to run secure by default so we want this protection.  Too bad it protected us from printing.

I returned to my computer and mapped the printer to my computer.  I was easily able to reproduce the problem.  My print spooler recovered better and I could print other things, but IE crashed hard every time.  I did a little hunting on the web and had a hard time finding content on topic.  Here is what I did find.

When IE runs in protected mode, it runs with limited privileges.  In my case the print driver wanted to run an exe from a location that is restricted by those privileges.   With that in mind, here are your options when faced with this problem.

1) Turn protected mode off
2) Run IE as administrator
3) Reinstall with a basic driver
4) Install printer locally (skip print server)
3) Don’t print to that printer
4) Create rules to elevate the processes

Some people are able to install more basic drivers but that is hit or miss.  On my computer I was able to install it locally avoiding the print server and it worked fine.  That was an interesting detail.  Our print server is server 2003. 

I was able to resolve the issue by creating a rule to elevate the processes needed for printing.  The post that gave me this idea is here. I did exactly what was suggested and it fixed the problem.  There are registry keys for IE that tell it what to elevate under protected mode.  I created a rule for the same 3 files mentioned: HP1006MC.EXE, HP1006SM.exe, P1006SSL.exe.  After closing IE one more time, it picked up those rules and it worked fine.

I exported those keys to reg files for more testing and it worked on the original computer that the problem was reported on.  I merged those settings into our domain policy and I hope that’s the last I have to deal with that. 

I found those files in the C:\Windows\System32\spool\drivers\w32×86\3 folder.  If I have more problems like this, my plan is to create a rule for all the exe files in that folder.  I was able to do the exact same 3 that the other guy mentioned.  If the problem continued, I was prepared to make rules for all 10 files I found.

November 23, 2009

Creating a MST Transform from a MSI

Filed under: Uncategorized — Kevmar @ 4:24 pm

I had a vendor MSI that I needed to make changes too.  I wanted to push it out as a silent install but as it was configured, it required user input to install correctly.  I found a lot of information telling me that I needed a MSI transform (or MST) to do that.  But I found almost no details on how to create that transform.

I eventually discovered the ORCA MSI Editor.  I think its a Microsoft tool but I found it on this Technipage.  ORCA looks at the MSI like its a database and exposes all the tables and records to you.  It is a bit overwhelming to get into.  After messing up a few MSI packages (make backups first) I discovered how easy it was to create a transform. Making changes is where it gets hard.

To create a mst transform, first open the MSI. Then under Transform, choose New Transform.  Now make any changes or updates that you want to make.  When you are done, under the Transform menu choose Generate Transform and save it.  This will create a MST with your changes.  To test the transform you can run your command like this: base.msi TRANSFORMS=transform1.mst

That looks easy and once you figure out what changes you are going to make then it is easy.  Here are two links you should look at.  Using a Sequence Table  and the List of Database Tables. They were a huge help to me.  Here are a few examples of changes that I made.

The first one was to fix the message “This installation cannot be run directly launching the MSI package. You must run setup.exe“.  In the Properties table add a row called ISSETUPDRIVEN with a value of 1.  This will allow the msi to run on its own.  Remember they added this check to make sure your version of Microsoft Installer supported the MSI.  If it fails, you may have to install the correct files yourself.

My setup had one section with 3 options and I need to disable one of those options.  I tracked that option down to a entry in the Feature table.  Looking at the online documentation it looks like setting the Level value to 0 from 1 will do that for me.

Registry settings was also something I needed to mess with.  All the registry settings were in the Registry table.  I found the key I wanted to change and was able to change the value.  There was a second key I had to add in by hand.  It is one that the installer would prompt the user for and it had no default.  If I want my silent install to work, I have to make sure that value gets set.  The component column puts the key in a group that matches an installed component.  I reused the component value of the other key I changed.  I should never see one without the other.  I set Root = 2 because that’s what the other key had. 

That was all the changed I needed to make.  It was a lot to take in at first.  I had never created a MST before.  It was a great way to work around the vendor.  I know I have ran into this before where a MST was needed but I was able to just use a batchfile instead.  Now that I am running things with a bit more security with windows 7, batch files just don’t cut it anymore.

November 19, 2009

Extracting a MSI from a EXE

Filed under: Solutions — Kevmar @ 4:19 pm

I ran into a situation where the vendor would only provide a exe as an installer and not a MSI.  If you work with Active Directory for software deployment, you know how important a MSI can be.  We also want to customize a few options and make it silent.  Our goal is something we can deploy to machines and not have to install or configure by hand.

When I run the EXE, I see that it is running a standard Microsoft installer.  After looking around I saw lots of comments that indicate that some EXE files like I had could be holding the MSI inside it.  So I started my search for ways to extract the MSI. 

My early search results pointed me to a Universal Extractor.  It did indicate that it was a Windows Installer package but both of its methods failed to extract any usable files.  I also found that the Windows SDK had some scripts that could export from Microsoft Installer files.  After I installed the SDK, I discovered that it didn’t work so well on a EXE. 

I did find a very easy way to get the MSI out of the EXE though.  I cleared out the temp folder and ran the install.  It first decompressed the files into the temp folder.  The MSI I needed was one of them.   There were a few MSI files so I made a copy of all of them.  One of them was important later.

When I tried to run the MSI directly, it gave the message that I had to run setup.exe to start the install.  It looks like it requires the shell EXE to run because it verifies the environment.  One thing it does is check to make sure the correct Microsoft Installer is loaded.  ISScript9.msi was the MSI file it ran when the environment was not correct.  I did end up needing to install that, but I still got the error about needing to run the EXE first.

I saw was to add ISSETUPDRIVEN=1 to the command line or into a transform.  I ended up adding it to the transform and it solved that issue.  Creating the transform was a lot more involved that I expected.  The largest problem was the lack of information on it.  I am going to do another write up on that.

Older Posts »

Powered by WordPress