![]() |
![]() | ![]() | ![]() | ![]() | ![]() | ![]() | ![]() |
| Welcome to Windows Vista Forums. Our forum is dedicated to helping you find solutions with any problems, errors or issues you are experiencing with Windows Vista. The Vista forum also covers news and updates and has an extensive Windows Vista tutorial section that covers a wide range of tips and tricks. |
| |||||||
![]() |
| |
| | #1 (permalink) |
| | 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) |
| | 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) |
| | 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) |
| | 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 | |
| |
Similar Threads | ||||
| Thread | Forum | |||
| Creating dattime objects from string data | PowerShell | |||
| problems with $var | select-string -pattern $string -q | PowerShell | |||
| Using $matches in string formatting via -f | PowerShell | |||
| String formatting of piped dates | PowerShell | |||
| String PRODUCT_NAME was not found in string table | Vista General | |||