powershell get-exchangeserver output not in desired format


hello friend ,

have built small script , working fine if see result in powershell window. if try send output in csv or txt via out-file cmdlet , not want.

script

$exch=get-exchangeserver
out-file c:\exchtest\output.csv
$file ="c:\exchtest\output.csv"

foreach ($srv in $exch) {if ($srv.serverrole -match "edge") {write-host "cant go further"} else

{write-host  "server name " $srv.name " , role :"  $srv.serverrole}

get-wmiobject -computer $srv.name -class win32_volume -filter "drivetype=3" | select-object  name,label ,  @{name="capacity(gb)";expression={[math]::round($_.capacity/1gb,2)}}, @{name="free_space (gb)";expression={[math]::round($_.freespace/1gb,2)}},@{name="free_percentage";expression={[math]::round(([int64]$_.freespace/[int64]$_.capacity)*100,2)}} | ft | out-file $file -append}


above syntax in bold want not coming in output file.
if try "" >>" , shows whole sentence in breakups :

server name is
value
$srv.name
and role :
value
$srv.serverrole

kindly me in resolving issue.

you can there's no point exporting csv:

get-exchangeserver | where-object {$_.serverrole -notmatch 'edge'} | foreach-object{
  
    $server = $_
    $capacity = @{name="capacity(gb)";expression={[math]::round($_.capacity/1gb,2)}}
    $freespace = @{name="freespace(gb)";expression={[math]::round($_.freespace/1gb,2)}}
    $freepercentage = @{name="freepercentage";expression={[math]::round(([int64]$_.freespace/[int64]$_.capacity)*100,2)}}
    $servername =  @{name="servername";expression={$server.name}}
    $serverrole = @{name="serverrole";expression={$server.serverrole}}
  
    write-host ("servername "+$server.name+" , role "+$server.role)
    get-wmiobject -computer $server.name -class win32_volume -filter "drivetype=3" | select-object name,label,$servername,$serverrole,$capacity,$freespace,$freepercentage | format-table
}
shay levy [mvp]
http://blogs.microsoft.co.il/blogs/scriptfanatic
powershell toolbar


Windows Server  >  Windows PowerShell



Comments

Popular posts from this blog

CRL Revocation always failed

Failed to query the results of bpa xpath

0x300000d errors in Microsoft Remote Desktop client