![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
|
Welcome to Vista Forums we are your forum to discuss Windows Vista x64 and x86 systems. Whether you need help or just want to post an idea you have on Vista, this is the forum for you.
br> br> |
| |||||||
![]() |
| | Thread Tools | Display Modes |
| | #1 (permalink) |
| Guest | Formatting objects in a string Hi, I have a list of object which I pipe to a "select" which gets me a nice format for the console. However, I want to send the info by email so I would need to format the data in a string. I tried assigning the result to a variable, if a display the variable in the console it is fine, but when I send it by email, all I get is the name of formatting objects. |
My System Specs![]() |
| | #2 (permalink) |
| Guest | Re: Formatting objects in a string In message <1186395133.180955.66280@q75g2000hsh.googlegroups.com>, olig <olig@monimap.com> writes >Hi, > >I have a list of object which I pipe to a "select" which gets me a >nice format for the console. However, I want to send the info by email >so I would need to format the data in a string. I tried assigning the >result to a variable, if a display the variable in the console it is >fine, but when I send it by email, all I get is the name of formatting >objects. You can use the -f switch as follows: PSH [D:\foo]: $x=12; $y=22.234 PSH [D:\foo]: $str = "this is X {0}, and this is y: {1}" -f $x, $y PSH [D:\foo]: $str this is X 12, and this is y: 22.234 Is this what you wanted to do?? -- Thomas Lee doctordns@gmail.com MVP - Admin Frameworks and Security |
My System Specs![]() |
| | #3 (permalink) |
| Guest | Re: Formatting objects in a string On 6 août, 06:38, Thomas Lee <t...@psp.co.uk> wrote: > In message <1186395133.180955.66...@q75g2000hsh.googlegroups.com>, olig > <o...@monimap.com> writes > > >Hi, > > >I have a list of object which I pipe to a "select" which gets me a > >nice format for the console. However, I want to send the info by email > >so I would need to format the data in a string. I tried assigning the > >result to a variable, if a display the variable in the console it is > >fine, but when I send it by email, all I get is the name of formatting > >objects. > > You can use the -f switch as follows: > > PSH [D:\foo]: $x=12; $y=22.234 > PSH [D:\foo]: $str = "this is X {0}, and this is y: {1}" -f $x, $y > PSH [D:\foo]: $str > this is X 12, and this is y: 22.234 > > Is this what you wanted to do?? > > -- > Thomas Lee > doctor...@gmail.com > MVP - Admin Frameworks and Security Thanks for the help, unfortunately,this is not what I am looking for. I joined my sample script to further clarify what I mean. #script to calculate size of some directories and send report by email #list of directories to calculate size $dirs = @("c:\windows", "c:\temp") $smtpserver = "127.0.0.1" $maildest = "email@example.com" $res = @{} #calculate the size of each dir and put it in hashtable foreach ($dir in $dirs) { $res[$dir] = gci $dir -recurse | measure-object -property length -sum } $body = ($res.keys | select {$_}, {$res."$_".Count}, @{name="Size in GB";Expression={[System.Math]::Round($res."$_".sum/1073741824,1)}}) # up to this point if I print $body in the console it looks fine # but if I try to pass the variable as a string to the send function it doesn't work $sc = new-object Net.Mail.SmtpClient -arg $smtpserver $sc.Send($maildest, $maildest, "Backup information", $body) |
My System Specs![]() |
| | #4 (permalink) |
| Guest | Re: Formatting objects in a string Select-Object returns a PSCustomObject: $body.GetType().fullName System.Management.Automation.PSCustomObject You can cast it to a string: [string]$body1 = $body ....pipe it to Out-String: $body2 = $body | out-string ....use variable expansion notation: $body3 = "$body" $body, $body1, $body2, $body3 | % {$_.GetType().fullName} System.Management.Automation.PSCustomObject System.String System.String System.String In your script you could: [string]$body = ($res.keys | select {$_}, {$res."$_".Count}, @{name="Size in GB";Expression={[System.Math]::Round($res."$_".sum/1073741824,1)}}) ....or: $body = ($res.keys | select {$_}, {$res."$_".Count}, @{name="Size in GB";Expression={[System.Math]::Round($res."$_".sum/1073741824,1)}}) | Out-String ....or: $sc.Send($maildest, $maildest, "Backup information", "$body") -- Kiron |
My System Specs![]() |
![]() |
| Thread Tools | |
| Display Modes | |
| |
Similar Threads | ||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| problems with $var | select-string -pattern $string -q | Ben Christian | PowerShell | 3 | 02-08-2008 12:41 PM |
| Using $matches in string formatting via -f | Altraf | PowerShell | 5 | 10-25-2007 01:50 PM |
| String formatting of piped dates | Altraf | PowerShell | 11 | 10-09-2007 03:41 PM |
| How export-csv deals with string versus string[] | Marco Shaw | PowerShell | 2 | 07-13-2007 12:18 PM |
| String PRODUCT_NAME was not found in string table | Extracampine | Vista General | 3 | 02-12-2007 06:15 AM |