在PowerShell中读取时CSV不对齐

时间:2018-03-22 12:53:46

标签: powershell csv

我有一个包含大量标题的CSV。范围从A列到AZ。我只需要阅读CSV的第一列。

我的问题是在notepad ++中打开时,最后一列(注释)渗入第一列,它似乎表明它是第一列(主机名)的一部分。当我运行我的代码时,它会像记事本++那样读取它。

我最初将它从.xls转换为CSV文件,有没有办法确保最后一列不会回绕?

#region - Convert to CVS 
$strFileName = "C:\working\Server Count & Status Check ARC 0219183.xlsx"
$strSheetName = 'SCI$'
$strProvider = "Provider=Microsoft.ACE.OLEDB.12.0"
$strDataSource = "Data Source = $strFileName"
$strExtend = "Extended Properties='Excel 8.0;HDR=Yes;IMEX=1';"
$strQuery = "Select * from [$strSheetName]"

$objConn = New-Object 
System.Data.OleDb.OleDbConnection("$strProvider;$strDataSource;$strExtend")
$sqlCommand = New-Object System.Data.OleDb.OleDbCommand($strQuery)
$sqlCommand.Connection = $objConn
 $objConn.open()

$da = New-Object system.Data.OleDb.OleDbDataAdapter($sqlCommand)
$dt = New-Object system.Data.datatable
$da.fill($dt)



$dt | Export-Csv "C:\working\Server Count & Status Check ARC 0219183.csv" - 
NoTypeInformation

$objConn.close()
#endregion

Original Spreadsheet

After trying to gather first column(hostname)

 $array=@{}

$ServerStatus = Get-Content "C:\Users\user\Desktop\Scripts\Linux Server 
Compare\Server Count & Status Check ARC 0219183.csv" | select -skip 1 |  
ConvertFrom-Csv  -Header "HostName","Business Application","Technical 
Function","Location","Environment","Day Number","Slot 
Number","SlotTime","Description","DNS Name","Key Server","Host Name","Public 
IP","Server Type","Build Date","Builder","Product Name","Serial 
No","Enclosure","Bay","CPU Model","CPU Socket","CPU Cores","Logical 
CPU","Memory","Up Time","Kernel Version","OS Type","Version","Patch 
level","Multipath Disks?","Multipath Checker?","Customized Fsck?","Splx 
Installed?","Splx Running?","Suse Manager Status","RHN Running ?","RHN 
Autostart?","Installed Kernel","Available Kernel in 
/boot","Hardened","Syslog 
Config correct ?","Syslog Running ?","Patrol Running?","Splunk 
Installed?","Splunk Running?","Puppet Installed?","Puppet 
Running?","Centrify 
Version","VM Tools Installed?","VM Tools Running?","Comments"

$ServerStatusObj = new-object Object
$ServerStatusObj | add-member -membertype NoteProperty  "Name" -value ""  
$ServerStatusObj | add-member -membertype NoteProperty  "Origin" -value ""  

$array=@{name = $ServerStatus.HostName}


  foreach ($name in $array.name){

         if($name -ne $null){

          $Value = $name


            if($name -ne $null){

            $ServerStatusObj.name =$Value
            $ServerStatusObj.Origin = " Linux Servers"
                                      } #End of IF
                        else {Write-Output "null"}

           Write-Output $ServerStatusObj | export-csv 
"C:\Users\user\Desktop\Scripts\Linux Server Compare\ServerStatusParse.csv"  -notypeinformation -Append        
                            }

0 个答案:

没有答案