VMware Cloud Community
mazdajai
Contributor
Contributor

Problem sending output to file

write-output Hostname`tSizeGB`tCreatedOn > $outfile
write-output --------`t------`t----------- >> $outfile
foreach ( $snap in $snapshots ) {
     if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) ) {
     write-output $snap.vm`t([Math]::Round($snap.SizeGB))`t($snap.Created -replace $retime) >> $outfile
     $count++
     } 
}

Expected output -

AP       7       10/1/2012
BA      20      9/20/2012
AP      17      10/1/2012

If I use write-output and out-file, I cannot retain the format

Hostname      SizeGB      CreatedOn

--------      ------      -----------

Name                 PowerState Num CPUs MemoryGB      

----                 ---------- -------- --------      

AP           PoweredOn  4        32.000        

7

10/1/2012

Name                 PowerState Num CPUs MemoryGB      

----                 ---------- -------- --------      

BA          PoweredOn  4        8.000         

20

9/20/2012

Name                 PowerState Num CPUs MemoryGB      

----                 ---------- -------- --------      

AP           PoweredOn  4        32.000        

17

10/1/2012

Reply
0 Kudos
6 Replies
LucD
Leadership
Leadership

The Write-Output cmdlet produces specifally formatted objects to be outputted to the console.

Those objects are not intended to be placed in a text file.

Use the Out-File cmdlet instead.

For example:

"Hostname`tSizeGB`tCreatedOn" | Out-File $outfile


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mazdajai
Contributor
Contributor

I tried but didn't work...this is what I get -

"$snap.vm`t([Math]::Round($snap.SizeGB))`t($snap.Created -replace $retime)"|out-file $outfile

Output

20121001.vm ([Math]::Round(20121001.SizeGB)) (20121001.Created -replace \d{1,2}:\d{1,2}:\d{1,2}.*?$)
Reply
0 Kudos
RvdNieuwendijk
Leadership
Leadership

It is easier to let PowerCLI do the formatting. For example:

$snapshots = Get-VM | Get-Snapshot
$Age = 3
&{foreach ( $snap in $snapshots )
{
  if ( $snap.Created -lt (Get-Date).AddDays( -$Age ) )
  {
    $Snap | Select-Object -Property @{N="HostName";E={$_.VM.VMHost.Name}},
    @{N="SizeGB";E={[math]::Round($_.SizeGB)}},Created   
  } 
}} | Format-Table -AutoSize |
Out-File -FilePath Snapshots.txt

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
Reply
0 Kudos
LucD
Leadership
Leadership

Inside double quotes all variables will be resolved, not sttements or cmdlets.

This will show the text as entered

"[Math]::Round(1.25)"

But you can first store the value in a variable like this

$result = [Math]::Round(1.25)

"$result"

Or use this specifial notation which first forces the calculation

"$([Math]::Round(1.25))"


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
mazdajai
Contributor
Contributor

Thanks LucD and Robert. Got it!

Is there there any way to put the special characters in without declaring them into an variable? I miss perl - without have to delcare variable for minor item like this.

$time = "\d{1,2}:\d{1,2}:\d{1,2}.*?$"

$Snap | Select-Object -Property @{N="HostName";E={$_.vm}},
@{N="SizeGB";E={[math]::Round($_.SizeGB)}},@{N="CreatedOn";E={$($_.Created -replace $time)}}
Reply
0 Kudos
LucD
Leadership
Leadership

You can specify the format string as the right operand for the replace operator.

$Snap | Select-Object -Property @{N="HostName";E={$_.vm}}, 
@
{N="SizeGB";E={[math]::Round($_.SizeGB)}},
@
{N="CreatedOn";E={$($_.Created -replace "\d{1,2}:\d{1,2}:\d{1,2}.*?$")}}

But you can also use one of the builtin methods, which produces the same result

$Snap | Select-Object -Property @{N="HostName";E={$_.vm}}, 
@{N="SizeGB";E={[math]::Round($_.SizeGB)}},
@{N="CreatedOn";E={$_.Created.ToShortDateString()}}


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos