Windows Vista Forums
Vista Forums Home Join Vista Forums Windows 7 Forum Vista Tutorials Tags
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.

Go Back   Vista Forums > Misc Newsgroups > PowerShell

Vista - Formatting objects in a string

Reply
 
Old 08-06-2007   #1 (permalink)
olig


 
 

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


 
 

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


 
 

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


 
 

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
Reply

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


Vista Forums 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 Ltd

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