PSCX Tips Part 2

This is a continuation of PSCX Tips Part 1.  Here are some more things you can do with the PowerShell Community Extensions.

If you do .NET software development and partially strong name your assemblies then you know that at some point in your official build process you need to finish the strong-naming process.  When we have done this, we cycle through all the exes and dlls in a common output dir and apply the full strong name.  Problem is that we have some native binaries mixed in with the managed binaries.  So we need some quick way to test if a file is a managed assembly.  This is where Test-Assembly comes in handy:

PS> Get-ChildItem | Where {$_ –match ‘\.(dll|exe)$’ –and (Test-Assembly $_)} |
    Foreach {sn.exe /Ra $_.fullname $pathToKeyFile}

On 64-bit Windows you may want to know whether a binary is a 32-bit or 64-bit binary.  That’s easy with Get-PEHeader:

PS> Get-PEHeader .\wordpad.exe

Type                    : PE64
LinkerVersion           : 9.0
OperatingSystemVersion  : 6.1
ImageVersion            : 6.1
SubsystemVersion        : 6.1
SizeOfCode              : 604672
SizeOfInitializedData   : 3915776
SizeOfUninitializedData : 0
AddressOfEntryPoint     : 65624
BaseOfCode              : 4096
BaseOfData              : 0
ImageBase               : 4294967296
SectionAlignment        : 4096
FileAlignment           : 512
Win32VersionValue       : 0
SizeOfImage             : 4538368
SizeOfHeaders           : 1536
Checksum                : 4583061
Subsystem               : Windows
DllCharacteristics      : 33088
SizeOfStackReserve      : 524288
SizeOfStackCommit       : 8192
SizeOfHeapReserve       : 1048576
SizeOfHeapCommit        : 4096
LoaderFlags             : 0

Need to test if the current user is elevated on Vista or Windows 7?  Use Test-UserGroupMembership e.g.:

PS> Test-UserGroupMembership -GroupName Administrators

Need to know what the 8.3 filename is for a file?  Use Get-ShortPath e.g.:

PS> Get-ShortPath ‘.\Windows Azure SDK’

Need a touch like utility to change a file’s various time fields?  Try Set-FileTime e.g.:

PS> Set-FileTime .\test.csv -Modified (Get-Date).AddDays(-2)

Need to compute the hash for a string or an entire file using various algorithms e.g.:

PS> "hello world" | Get-Hash

Algorithm: MD5

Path       :
HashString : E42B054623B3799CB71F0883900F2764

PS> Get-Hash .\uszip.wsdl -Algorithm SHA1

Algorithm: SHA1

Path       : C:\Users\Keith\uszip.wsdl
HashString : 74910AE77B4089496D5EF829DF3F0485BF855EB9

Need to convert between different OS style line endings in files?  Check out these cmdlets;

  • ConvertTo-MacOs9LineEnding
  • ConvertTo-UnixLineEnding
  • ConvertTo-WindowsLineEnding

Need to convert from or to Base64?  Try out the ConvertTo-Base64 and ConvertFrom-Base64 cmdlets e.g.:

PS> [byte[]](1..255) | ConvertTo-Base64

When parsing legacy exe text, do you ever need to skip either the first couple of lines or perhaps the last line (or both)?

Skip-Object is very convenient here because you don’t need to know the length of the sequence you are dealing with.  Here’s an example:

PS> 1..10 | Skip-Object -First 2 -Last 1 -Index 4,6

That’s enough for this post but there’s still plenty more in PSCX.  Stay tuned.

This entry was posted in PSCX. Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s