Windows Vista Forums
Vista Forums Home Join Vista Forums Donate Vista Tutorials Tags

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.
Register at Vista forums...the world biggest Windows Vista resource Join Vista Forums Now

Go Back   Vista Forums > Microsoft Technical Newsgroups > PowerShell

Formatting objects in a string

Update your Vista Drivers Update Your Drivers Now!!
Closed Thread
 
Thread Tools Display Modes
Old 08-06-2007   #1 (permalink)
olig
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 SpecsSystem Spec
Old 08-06-2007   #2 (permalink)
Thomas Lee
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 SpecsSystem Spec
Old 08-06-2007   #3 (permalink)
olig
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 SpecsSystem Spec
Old 08-07-2007   #4 (permalink)
Kiron
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 SpecsSystem Spec
Closed Thread

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


Vistax64.com is an independent web site and has not been authorized,
sponsored, or otherwise approved by Microsoft Corporation.
"Windows Vista", the Start Orb, and related materials are trademarks of Microsoft Corp.
© Designer Media 2005-2008

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51