none
how to get a percent sign when it exports to csv RRS feed

  • Question

  • Hi,

    I am stuck maybe you can help me I am trying to get the percent sign to export to csv when it writes to the host I see the percentages when it exports to csv I get no percentages next to the number. can you please help

    $computers=get-contentD:\OPS_PS_Scripts\Servers\Serv1.txt


     

    $newarray=@()

     

    foreach($computerin$computers) {

       

    $disks=  gwmi-computername$computerwin32_logicaldisk-filter"drivetype=3"


       

    foreach($diskin$disks) {

     

       

        

    if($disk.diskid -ne"C:"-and$disk.diskid -ne"D:"){

        

        

    $size="{0:0.0} gb"-f($disk.size/1gb)

        

    $freespace="{0:0.0} gb"-f($disk.freespace/1gb)

        

    $used=([int64]$disk.size -[int64]$disk.freespace)

        

    $percent=($used*100)/$disk.size

        

    $percent="{0:0}"-f$percent

        

    write-host$disk.systemname

        

    write-host$disk.deviceid

        

    write-host$disk.volumename

        

    write-host"size : "$size


        

    write-host"free space : "$freespace


        

    write-host"percentage used : "$percent"%"


    $newobject=""|selectSystemname,deviceid,volumename,size,freespace,percent_used


    $newobject.systemname =$disk.systemname

    $newobject.deviceid =$disk.deviceid

    $newobject.volumename =$disk.volumename

    $newobject.size =$size


    $newobject.freespace =$freespace


    $newobject.percent_used =  " $percent"

     

    $newarray+=$newobject

        }

        }

      }



    $newarray

    |export-csvD:\OPS_PS_Scripts\report.csv-force


    • Moved by Bill_Stewart Tuesday, December 11, 2018 8:55 PM Unanswerable drive-by question
    Thursday, July 12, 2018 4:58 PM

All replies

  • Please edit your post. Do not post pre colored code. Use the code posting tool instead. And please do not post that much white space.

    Best regards,

    (79,108,97,102|%{[char]$_})-join''

    Thursday, July 12, 2018 5:24 PM
  • look at the first portion of code you stole from someone; and compare it to the "new array objects".

    Thursday, July 12, 2018 5:48 PM
  • Please.  Never post colorized code.  Use the tool provided for posting code.


    \_(ツ)_/

    Thursday, July 12, 2018 5:53 PM
  • Lucky you. I'm in a good mood ... ;-)  ... you could start with something liek this ... at least it should work.

    $ComputerList = 'Computer1','Computer2'
    $CompleteDiskList = foreach($ComputerName in $ComputerList) {
        Get-WmiObject -Class win32_logicaldisk -filter "drivetype = 3" -ComputerName $ComputerName -ErrorAction SilentlyContinue |
            ForEach-Object {
                [PSCustomObject]@{
                    ComputerName     = $ComputerName
                    DeviceID         = $_.DeviceID
                    'Size (GB)'      = [MATH]::Round( $_.Size / 1GB, 0)
                    'FreeSpace (GB)' = [MATH]::Round( $_.FreeSpace / 1GB, 0)
                    'FreeSpace %'    = [MATH]::Round(100/$_.Size * $_.FreeSpace, 0)
                }
            }
    }
    $CompleteDiskList | Format-Table -AutoSize

    Best regards,

    (79,108,97,102|%{[char]$_})-join''



    • Edited by BOfH-666 Thursday, July 12, 2018 6:14 PM
    Thursday, July 12, 2018 6:12 PM