Using the `t in a hash table string output

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 10 months ago.

  • Author
    Posts
  • #5033

    by suave9987 at 2012-08-22 11:09:30

    Hello!
    I have posted this in Powershell.com as well but this seems the place to be.

    I have a hash table and are trying to put a tab space for all my lines for formatting with out-file. I tried putting the `t but it does not work.

    Here is the script for just the lines I want sent by out-file:

    foreach {
    if ($ht[$_]) {
    '{0} `t-> Available in "{1}"' -f $_,($ht[$_] -join ' **ALSO IN** ')
    }
    else {'{0} `t-> UNKNOWN' -f $_}
    }|out-file "$pathOfG\AID Integrity Check Report – $nameOnly1" -append

    here is the data which I have in the out file when it is sent:

    A000000018100203 `t-> Available in "AID List refrence Product.txt"
    A0000000620203 `t-> Available in "AID List refrence Product.txt"
    A000000018100118 `t-> Available in "AID List refrence Product.txt"
    A00000001810011A `t-> Available in "AID List refrence Product.txt"

    I think I may need to put double quotes for the `t to be recognized.

    I would like an equal amount of space to be displayed before -> so like this:

    A000000018100203 -> Available in "AID List refrence Product.txt"
    A0000000620203 -> Available in "AID List refrence Product.txt"
    A000000018100118 -> Available in "AID List refrence Product.txt"
    A00000001810011A -> Available in "AID List refrence Product.txt"

    I formated with the code selecter above because it was not originally showing what I wanted.

    I appreciate your help and suggestions!

    Thanks again!

    by willsteele at 2012-08-22 11:17:52

    You have to wrap the string in double quotes, not, single quotes. When you use single quotes, PowerShell interprets the string as a literal string and no evaluation is performed. Try this:
    foreach {
    if ($ht[$_]) {
    "{0} `t-> Available in `"{1}`"" -f $_,($ht[$_] -join ' **ALSO IN** ')
    }
    else {"{0} `t-> UNKNOWN" -f $_}
    }|out-file "$pathOfG\AID Integrity Check Report – $nameOnly1" -append

    by suave9987 at 2012-08-22 11:33:10

    Ok this does work except how can I get an even number of space for all of the out put. Right now it shows this:

    A000000018100203 -> Available in "AID List refrence Product.txt"
    A0000000620203 -> Available in "AID List refrence Product.txt"
    A000000018100118 -> Available in "AID List refrence Product.txt"
    A00000001810011A -> Available in "AID List refrence Product.txt"
    A0000000871005FFFFFFFF8913100000 -> Available in "AID List refrence Product.txt"
    A0000000871005FFFFFFFF8913200000 -> Available in "AID List refrence Product.txt"

    When I want the spacing to be at the same point as this:

    A000000018100203 -> Available in "AID List refrence Product.txt"
    A0000000620203 -> Available in "AID List refrence Product.txt"
    A000000018100118 -> Available in "AID List refrence Product.txt"
    A00000001810011A -> Available in "AID List refrence Product.txt"
    A0000000871005FFFFFFFF8913100000 -> Available in "AID List refrence Product.txt"
    A0000000871005FFFFFFFF8913200000 -> Available in "AID List refrence Product.txt"

    Should I use the -f to format and put an equal amount of space?

    Thanks for the quick response!

    by JeffH at 2012-08-22 11:48:39

    You are going to end up spending too much time trying to parse and format text. Build your hash table with the values you want, forgetting about tabs and formatting.

    $h=@{
    abcde=12343
    abd=333
    ddeeee=455
    fgj=4556
    }

    Then use PowerShell to format it.
    $h | format-table -hidetableheaders

    by willsteele at 2012-08-22 11:49:35

    There are some formatting tricks I learned from kazun. For instance, if you want to ensure a given column is always X characters wide, use this approach:

    "{0,-5}-{1,-5}" -f 123,12

    The gotcha here is ensuring the string is no longer than your space. So, if you put somethinn 6 characters long, it will automatically expand the format place holder to 6 spaces. In your case, this may work:

    foreach {
    if ($ht[$_]) {
    "{0,-41} -> Available in `"{1}`"" -f $_,($ht[$_] -join ' **ALSO IN** ')
    }
    else {"{0} `t-> UNKNOWN" -f $_}
    }|out-file "$pathOfG\AID Integrity Check Report – $nameOnly1" -append

    I set the width to 41 characters. So, as long your string is less than 41 characters wide, it will work as expected. This removes the need for `t characters.

    Look at this thread for more ideas: http://social.technet.microsoft.com/Forums/en-US/winserverpowershell/thread/b9cad1a7-42b8-4890-8863-ed464be3086b

    by JeffH at 2012-08-22 11:56:04

    That still seems like a lot of work. I see you are adding a custom "header". You can still doe that with my suggestion and still hide the headers.


    $h=@{
    abcde=12343
    abd=333
    ddeeee=455
    fgj=4556
    }

    $h | ft Name,@{name="modified";Expression={"-> Foo $($_.value)"}} -hidetableheaders

    fgj -> Foo 4556
    abd -> Foo 333
    ddeeee -> Foo 455
    abcde -> Foo 12343

    by suave9987 at 2012-08-22 12:18:13

    Ok I have been trying both and Jeff Im not sure what you mean by "a lot of work"? are you saying that this is inefficient because I am hard coding the spaced values and not just formating with powershell?

    Thanks so much for the replys!!

    by JeffH at 2012-08-22 13:16:04

    Look at your code compared to my code. It is possible to get close to the same results with the direction you are going compared to letting PowerShell do the work. Personally, I'd rather let PowerShell worry about how to make it pretty on the screen. If you keep thinking about objects and how to format them and not text, you'll start finding this easier to do.

    by suave9987 at 2012-08-22 13:45:45

    Ok yes this makes alot of sense and I can see that I will need to use this when I am formating my next hash table here:

    foreach {
    if ($ht[$_]){
    #If an App ID is found, remove this listfile from the collection of listfile names in the hash table for that key.
    $ht[$_].remove($Filename) | out-null
    }
    #Message if it can't find that App ID in the hash table

    else{"$_ `tfound in $Filename is not in the $namedOnly1"
    }
    }|out-file "$pathOfG\AID Integrity Check Report – $nameOnly1" -append

    My table $ht is a System.Collections.Hashtable type and the values for $ht[$_] are this System.Collections.Hashtable[A000000018820F01000000A0] . The number in the brackets is a application ID. Now when I just have the $_ I display the values in the bracket.

    Now I cannot use the trick from last time to format and because I am not directly dealing with a hash table and I believe are dealing with a collection, how can I format this the way I want. Is it still the same way you are saying?

    I tried this but no luck:

    $ht | ft Name,@{name="modified";Expression={"->is found in $_"}} -hidetableheaders

    Also I am sending this to an out-file so is this still achievable?

    Thanks so much!

    by JeffH at 2012-08-22 14:15:59

    Ahh..so your hash table values are in turn nested objects? Or is the value a collection of objects? Yes that does complicate things.

    by suave9987 at 2012-08-22 14:27:34

    Yes the hash table has the Keys set as the Application ID's and the values of the HT are a collection of file names.
    I know this is complicated but there must be a way to format this to the way I want...
    I am kind of confusing my self at this point..

    by JeffH at 2012-08-22 15:34:49

    Sure. You just need to know what you want from the value of each key. Do you want the first file name? You just have to treat the value as an object.


    $h=@{
    aaa=$(gwmi win32_logicaldisk -filter "deviceid='c:'")
    bb=$(gwmi win32_logicaldisk -filter "deviceid='e:'")
    ccccc=$(gwmi win32_logicaldisk -filter "deviceid='c:'" -comp serenity)
    }

    PS C:\> $h | Format-table Name,@{Name="Device";Expression={$_.value.DeviceID}},@{Name="Size";Expression={$_.value.Size}} -autosize

    Name Device Size
    ---- ------ ----
    ccccc C: 487439986688
    bb E: 4291817472
    aaa C: 26735538176

    by suave9987 at 2012-08-23 07:45:01

    Great Thanks Jeff! This works amazing! i appreciate it!
    I will just add the -hidetablenames to the end (Or I think this is what it is called)

    You are amazing!

You must be logged in to reply to this topic.