简体   繁体   English

Powershell 从 Excel 列导入的日期和时间以错误的格式导入

[英]Powershell imported date and time from Excel column are imported with wrong format

I´m importing a Excel document to a powershell script using Import-Excel module.我正在使用 Import-Excel 模块将 Excel 文档导入到 powershell 脚本中。 The import works great but the column with date gets formatted really weird.导入效果很好,但带有日期的列的格式非常奇怪。 The date and time in the Excel sheet are formatted like this: yyyy-mm-dd hh:mm (2020-09-01 04:03) but the data that is imported looks like this: 43965,1672916667. Excel 工作表中的日期和时间格式如下:yyyy-mm-dd hh:mm (2020-09-01 04:03) 但导入的数据如下所示:43965,1672916667。

I've tried to add [DateTime] to the variable like this: "Senast ansluten" = [DateTime]$ExcelLok.'Senast ansluten' but then I just get error Cannot convert value "44075.3451851852" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."我尝试将 [DateTime] 添加到这样的变量中: "Senast ansluten" = [DateTime]$ExcelLok.'Senast ansluten'但后来我得到错误Cannot convert value "44075.3451851852" to type "System.DateTime". Error: "String was not recognized as a valid DateTime." Cannot convert value "44075.3451851852" to type "System.DateTime". Error: "String was not recognized as a valid DateTime."

How can I specify the format so that it gets read correctly?如何指定格式以便正确读取?

$ImportExcel = Import-Excel -Path 'C:\Temp\Powershell scripts\Test\PingFastaIP\Fasta IP-nummer.xlsm' -WorksheetName ADM_UTB

ForEach ($ExcelLok in $ImportExcel){
[PSCustomObject]@{
        "IP address" = $ExcelLok.IP
        "Lokation" = $ExcelLok.Lok
        "Ping status" = $ExcelLok.'Ping status'
        "Senast ansluten" = [DateTime]$ExcelLok.'Senast ansluten'
} | Format-Table -Property `
        @{Name='Lokation';Expression={ $ExcelLok.Lok };align='left';width=15},
        @{Name='IP address';Expression={ $ExcelLok.IP };align='left';width=15},
        @{Name='Ping status';Expression={ $ExcelLok.'Ping status' };align='left';width=20},
        @{Name='Senast ansluten';Expression={ $ExcelLok.'Senast ansluten' }} 
        
}

The solution became this, thanks to mclayton:多亏了 mclayton,解决方案变成了这样:

$ImportExcel = Import-Excel -Path 'C:\Temp\Powershell scripts\Test\PingFastaIP\Fasta IP-nummer.xlsm' -WorksheetName ADM_UTB

ForEach ($ExcelLok in $ImportExcel){
[PSCustomObject]@{
        "IP address" = $ExcelLok.IP
        "Lokation" = $ExcelLok.Lok
        "Ping status" = $ExcelLok.'Ping status'
        "Senast ansluten" = [DateTime]::FromOADate($ExcelLok.'Senast ansluten')
} | Format-Table -Property `
        @{Name='Lokation';Expression={ $ExcelLok.Lok };align='left';width=15},
        @{Name='IP address';Expression={ $ExcelLok.IP };align='left';width=15},
        @{Name='Ping status';Expression={ $ExcelLok.'Ping status' };align='left';width=20},
        @{Name='Senast ansluten';Expression={ [DateTime]::FromOADate($ExcelLok.'Senast ansluten') }} 
        
}

试试这个: $ImportExcel = Import-Excel -Path 'C:\\Temp\\Powershell scripts\\Test\\PingFastaIP\\Fasta IP-nummer.xlsm' -WorksheetName ADM_UTB -AsDate 'DateColumn'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM