2017-07-18 31 views
1

我有一個包含日誌條目的CSV文件的文件夾。對於CSV的每個條目,如果風險屬性不是低而不是無,則將其放入累積CSV對象中。從那裏,我想直接導入到Excel工作簿,而不必將CSV保存到文件將CSV傳遞到Excel工作簿(不是來自文件)

$CSVPaths = (Split-Path $PSCommandPath) 
$AccumulateExportPath = (Split-Path $PSCommandPath) 
$FileName="Accumulate" 
[email protected]() 

Foreach ($csv in (Get-ChildItem C:\Scripts\Nessus\Sheets |? {$_.Extension -like ".csv" -and $_.BaseName -notlike "$FileName"})) 
{ 
    $Content = Import-CSV $csv.FullName 
    Foreach ($Log in $Content) 
    { 
     If ($Log.Risk -ne "None" -and $Log.Risk -ne "Low") 
     { 
      $Acc+=$Log 
     } 
    } 
} 

$CSV = $ACC |ConvertTo-CSV -NoTypeInformation 

Add-Type -AssemblyName Microsoft.Office.Interop.Excel 
$Script:Excel = New-Object -ComObject Excel.Application 
$Excel.Visible=$True 
#$Excel.Workbooks.OpenText($CSV) What should replace this? 

有沒有像OpenText()一個方法,讓我傳遞一個CSV 對象而不是文件路徑到一個CSV文件還是我將不得不寫我自己轉換功能?

+1

爲什麼你不想將csv保存爲臨時文件?對我來說似乎是最簡單的方法。 – FunThomas

+0

@FunThomas因爲如果有一個函數允許我傳遞一個CSV對象,爲什麼我應該創建一個臨時文件?如果沒有可以做到的功能,那麼我將使用臨時文件。 –

+1

腳本編寫人員爲此編寫了一個插件,但我不知道在一天結束時是否更容易使用。請參閱https://blogs.technet.microsoft.com/heyscriptingguy/2015/11/25/introducing-the-powershell-excel-module-2/ – FunThomas

回答

1

有趣的問題。我不知道有一種方法可以讓您通過CSV 對象

但是,如果你的結果CSV是不是太大了,你正在使用PowerShell的5.0+,你可以將對象轉換爲一個字符串槓桿Set-Clipboardmore info

$headers = ($csv | Get-Member | Where-Object {$_.MemberType -eq "NoteProperty"}).Name 
$delim = "`t" 

# headers 
foreach($header in $headers){ 
    $myString += $header + $delim 
} 

# trim delimiter at the end, and add new line 
$myString = $myString.TrimEnd($delim) 
$myString = $myString + "`n" 

# loop over each line and repeat 
foreach($line in $csv){ 
    foreach($header in $headers){ 
     $myString += $line.$header + $delim 
    } 
    $myString = $myString.TrimEnd($delim) 
    $myString = $myString + "`n" 
} 

# copy to clipboard 
Set-Clipboard $myString 

# paste into excel from clipboard 
$Excel.Workbooks.Worksheets.Item(1).Paste() 
0

這裏是另一種方式來創造無需編寫.csv文件即可從PowerShell獲取Excel電子表格。

$dirs = 'C:\src\t', 'C:\src\sql' 

$records = $() 
$records = foreach ($dir in $dirs) { 
    Get-ChildItem -Path $dir -File '*.txt' -Recurse | 
     Select-Object @{Expression={$_.FullName}; Label="filename"} 
    } 

#open excel 
$excel = New-Object -ComObject excel.application 
$excel.visible = $false 

#add a default workbook 

$workbook = $excel.Workbooks.Add() 
#remove worksheet 2 & 3 

$workbook.Worksheets.Item(3).Delete() 
$workbook.Worksheets.Item(2).Delete() 

#give the remaining worksheet a name 
$uregwksht = $workbook.Worksheets.Item(1) 
$uregwksht.Name = 'File Names' 

# Start on row 1 
$i = 1 

# the .appendix to $record refers to the column header in the csv file 
foreach ($record in $records) { 
    $excel.cells.item($i,1) = $record.filename 
    $i++ 
} 

#adjusting the column width so all data's properly visible 
$usedRange = $uregwksht.UsedRange 
$usedRange.EntireColumn.AutoFit() | Out-Null 

#saving & closing the file 
$outputpath = Join-Path -Path $Env:USERPROFILE -ChildPath "desktop\exceltest.xlsx" 
$workbook.SaveAs($outputpath) 
$excel.Quit() 
相關問題