Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get get-item cmdlet's output to variable as string

i mean when i call get-item with directory it dump to console like this

    ----                -------------     ------ ----
d----         2/16/2011   8:27 PM            2011-2-16
-a---         2/13/2011   8:24 PM 3906877184 SWP-Full Database Backup_2011-02-13
                                           0
-a---         2/16/2011   8:23 PM 3919766476 SWP-Full Database Backup_2011-02-16.bak
                                           8
-a---         2/12/2011   8:18 PM 3906877747 SWP-Full Database Backup_2011-02-12
                                           2
-a---         2/14/2011   8:21 PM 3875484467 SWP-Full Database Backup_2011-02-14
                                           2

but when i convert to string it changes as

\\192.168.2.89\BwLive\2011-2-16 \\192.168.2.89\BwLive\SWP-Full Database Backup_2011-02-13 \\192.168.2.89\BwLive\SWP-Full
 Database Backup_2011-02-16.bak \\192.168.2.89\BwLive\SWP-Full Database Backup_2011-02-12 \\192.168.2.89\BwLive\SWP-Full
 Database Backup_2011-02-14

i mean length,size,time attributes is omitted how can i keep these attributes while converting to string?

thanks.

like image 313
aeon Avatar asked Feb 12 '26 11:02

aeon


2 Answers

If I understand what you're after, this should work:

$a = get-childitem <filespec> |
  select name,length,lastwritetime |
   format-table | out-string

Then put $a in your email body.

like image 168
mjolinor Avatar answered Feb 15 '26 05:02

mjolinor


You should look into the various options Powershell gives you for formatting the results you receive from the pipeline.

If you declare a string variable for later usage as your E-Mail body you will receive the same format like your output to console. The following

$body = Get-ChildItem | Out-String

You can customise your result by adding the Format-Table CmdLet:

$body = Get-ChildItem | Format-Table -Property Name, Length | Out-String

Your script for sending could look something like this

$body = Get-ChildItem | Format-Table -Property Name, Length | Out-String

$SmtpClient = New-Object System.Net.Mail.SmtpClient
$MailMessage = New-Object System.Net.Mail.MailMessage

$SmtpClient.Host = "my.smtp.host.com"
$MailMessage.from = "[email protected]"
$MailMessage.To.Add("[email protected]")
$MailMessage.Subject = “Verzeichnisinhalt”
$MailMessage.IsBodyHtml = $false
$MailMessage.Body = $body
$SmtpClient.Send($MailMessage)
like image 24
Filburt Avatar answered Feb 15 '26 03:02

Filburt



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!