PowerShell Get-Counter to CSV Script,Encoding Problem

问题描述 投票:0回答:1

下午好。我正在尝试从Windows计算机将性能计数器读取到CSV文件。我成功地做到了。问题是,尽管我将Encoding设置为UTF8,但在CSV结果中仍然无法获得'ç'或'õ'之类的字符。取而代之的是,当我使用Excell查看提取时,我得到了“ ??”字符替换。

从与网络相关的问题中,我找不到适合我的“问题”的解决方案。预先感谢大家。

...    
$CounterPT = @" 
    Processor(_Total)\*
    "@ 

            (Get-Counter -ComputerName $ComputerName -Counter (Convert-HString -HString $CounterPT)) |  
            ForEach-Object { 
            $path = $_.path 
            New-Object PSObject -Property @{
            computerName=$ComputerName
            Counter        = ($path  -split "\\")[-2,-1] -join "-" 
            Item        = $_.InstanceName 
            Value = [Math]::Round($_.CookedValue,2)
            datetime=(Get-Date -format "yyyy-MM-d hh:mm:ss")
            } 

            }


    } 

    #Collecting counter information for target servers

    foreach($server in $Servers)
    {
    $d=Get-CounterStats -ComputerName $server |Select-Object computerName,Counter,Item,Value,datetime
    $d |Export-CsvFile $outfile  -Append -NoTypeInformation -Encoding UTF8

    }

    #End of Script
powershell performancecounter cmdlet export-csv
1个回答
0
投票

除非您要附加到文件开头没有Utf8 BOM(EF BB BF)的文件,否则它应该起作用。

get-content file.csv -Encoding byte | select -first 3 | format-hex


           Path:

           00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F

00000000   EF BB BF                                         
© www.soinside.com 2019 - 2024. All rights reserved.