Power Shell - Write-Host & Out-File

notarat

2[H]4U
Joined
Mar 28, 2010
Messages
2,501
So...

I have 3 variables

$rt = Date/Time a script is run
$a = Milliseconds to copy from local computer to remote computer
$b = Milliseconds to copy from remote computer to local computer

The write-host command (shown below) displays the info properly, like so (01/06/2015, 1.278789, 2.18896)

write-host $rt','$a','$b

I'm trying to output those values to either a text file or CSV file in that exact format but out-file either results in an error or exports each of the values on a separate line in the CSV or text file.

I tried combining them

$strCombined =$rt+$a+$b

but get the error about converting timespan to string

Okay...Let's convert each value to a string
$aa = ($a | Out-string)
$bb = ($b | Out-String)
$rtc = ($rt | Out-string)

Combining these converted values still results in each value being displayed on a separate line.

I know it's s stupid question and I'm overlooking something simple, but I'm fried and could use some assistance in getting these 3 values outputted to a text/csv in the following format:

01/06/2015, 1.278789, 2.18896
 
You should be able to just do a .ToString() on the $rt when combining them into $strCombined. Here's a sample of what I did which worked fine. I just have the write-host line in there to see what I was going to be writing to the file before I wrote it so I could double check it when I opened the file.

Code:
PS C:\Users> $rt = Get-Date
PS C:\Users> $a = 1.22
PS C:\Users> $b = 1.33
PS C:\Users> $output = $rt.ToString() + " - " + $a + " - " + $b
PS C:\Users> write-host $output
1/6/2015 1:09:21 PM - 1.22 - 1.33
PS C:\Users> out-file -filepath C:\Users\test.txt -inputobject $output -encoding ASCII
 
You should be able to just do a .ToString() on the $rt when combining them into $strCombined. Here's a sample of what I did which worked fine. I just have the write-host line in there to see what I was going to be writing to the file before I wrote it so I could double check it when I opened the file.

Code:
PS C:\Users> $rt = Get-Date
PS C:\Users> $a = 1.22
PS C:\Users> $b = 1.33
PS C:\Users> $output = $rt.ToString() + " - " + $a + " - " + $b
PS C:\Users> write-host $output
1/6/2015 1:09:21 PM - 1.22 - 1.33
PS C:\Users> out-file -filepath C:\Users\test.txt -inputobject $output -encoding ASCII

Thanks, Mang!

I'll give it a shot. I'm not all that familiar with .tostring so the first time I tried to use it. it autopopulated the opening parenthesis and I had no clue so I was like, "hmm...I think I gotta use something else." and went the | out-string route...
 
Back
Top