在下面的代码示例中,如何将存储在[10/Mar/2021:03:29+0000]中的时间转换为MM-dd-yyyyTHH:MM:ssZ格式。我问的原因是,我们需要在下游系统中使用这些数据,其中数据存储为03-10-2021T03:27:29Z。我得到了@712648(Mathias Jensen(的帮助,他建议查看parsedate。然而,我在下面的代码中使用了相同的问题。相同的解析日期在单独运行时执行,没有任何问题
代码
cls
$date = Get-Date -UFormat "%m-%d-%Y %H-%M-%S"
$BasePath = "C:TempAWS_S3S3Logs"
$JsonOutFile = "$Basepath"+"JsonFile - $date.json"
#Combine TextFiles Into A Single Text File
$ConcatenatedFile = "C:TempAWS_S3S3LogsCombinedFile.txt"
#Get-ChildItem $BasePath -File -Filter *.txt | gc | out-file -FilePath $ConcatenatedFile
#Build Columns for JSON file Key:Pair combination
$columns = 'Bucket Owner', 'Bucket', 'Time', 'Remote IP', 'Requester', 'Request ID', 'Operation', 'Key', 'Request-URI', 'HTTP status', 'Error Code', 'Bytes Sent', 'Object Size', 'Total Time', 'Turn-Around Time', 'Referer', 'User-Agen', 'Version Id', 'Host Id', 'Signature Version', 'Cipher Suite', 'Authentication Type', 'Host Header', 'TLS version'
$data = Get-Content -Path $ConcatenatedFile
$parsedLog = $data -replace '[([^]]+)]','"$1"' |ConvertFrom-Csv -Delimiter ' ' -Header $columns
$date_format = "yyyy-MM-ddTHH:mm:ssZ"
$parsedLog | Select Bucket Owner, Bucket,
([datetime]::ParseExact($_.'Time',"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format)),
Remote IP, Requester, Request ID, Operation, Key, Request-URI, HTTP status, Error Code, Bytes Sent, Object Size, Total Time,Turn-Around Time, Referer, User-Agen, Version Id, Host Id, Signature Version, Cipher Suite, Authentication Type, Host Header,TLS version | ConvertTo-JSON -Compress
#$jsonFile #| Out-File -FilePath $JsonOutFile
#Remove The Concatenated File after JSON File is created
#Remove-Item -Path $ConcatenatedFile
CombinedFile.txt的内容
fd89d80d676948bd913040b667965ef6a50a9c80a12f38c504f497953aedc341 SampleS3Bucket [12/Mar/2021:14:11:57 +0000] 117.222.211.65 arn:aws:iam::486031527132:user/jdoe H3KK9B3EERPBZZN1 REST.GET.BUCKET - "GET /SampleS3Bucket?list-type=2&encoding-type=url&max-keys=1000&fetch-owner=true&delimiter=%2F&prefix=Test1%2F HTTP/1.1" 200 - 986 - 126 125 "-" "S3Console/0.4, aws-internal/3 aws-sdk-java/1.11.964 Linux/4.9.230-0.1.ac.224.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.282-b08 java/1.8.0_282 vendor/Oracle_Corporation" - nH4rr0OG7OPS9WYZKyi4F0VcjffvyHpVhZgJGrzB99OtGBv8EqfdbKifFaREa1sFdxLMjU1GjoU= SigV4 ECDHE-RSA-AES128-GCM-SHA256 AuthHeader s3-us-west-2.amazonaws.com TLSv1.2
fd89d80d676948bd913040b667965ef6a50a9c80a12f38c504f497953aedc341 SampleS3Bucket [12/Mar/2021:14:13:32 +0000] 117.222.211.65 arn:aws:iam::486031527132:user/jdoe 7F9VJTFGE993PZ0B BATCH.DELETE.OBJECT Test1/Debt+Dictionary_BASE.xlsx - 204 - - 71222 - - - - - Ya9Yoe23lAFZdY7grWdyD/RggdJq14T2E02enlt4qAlebHfsbeiG2JQ+qZP8CF/iNvCUuTpv7gY= SigV4 ECDHE-RSA-AES128-GCM-SHA256 AuthHeader s3-us-west-2.amazonaws.com TLSv1.2
fd89d80d676948bd913040b667965ef6a50a9c80a12f38c504f497953aedc341 SampleS3Bucket [12/Mar/2021:14:13:32 +0000] 117.222.211.65 arn:aws:iam::486031527132:user/jdoe 7F9VJTFGE993PZ0B REST.POST.MULTI_OBJECT_DELETE - "POST /SampleS3Bucket?delete= HTTP/1.1" 200 - 177 - 63 - "-" "S3Console/0.4, aws-internal/3 aws-sdk-java/1.11.964 Linux/4.9.230-0.1.ac.224.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.282-b08 java/1.8.0_282 vendor/Oracle_Corporation" - Ya9Yoe23lAFZdY7grWdyD/RggdJq14T2E02enlt4qAlebHfsbeiG2JQ+qZP8CF/iNvCUuTpv7gY= SigV4 ECDHE-RSA-AES128-GCM-SHA256 AuthHeader s3-us-west-2.amazonaws.com TLSv1.2
fd89d80d676948bd913040b667965ef6a50a9c80a12f38c504f497953aedc341 SampleS3Bucket [12/Mar/2021:14:17:40 +0000] 117.222.211.65 arn:aws:iam::486031527132:user/jdoe GQ80J3H2EJVH13RS REST.GET.VERSIONING - "GET /SampleS3Bucket?versioning= HTTP/1.1" 200 - 113 - 17 - "-" "S3Console/0.4, aws-internal/3 aws-sdk-java/1.11.964 Linux/4.9.230-0.1.ac.224.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.282-b08 java/1.8.0_282 vendor/Oracle_Corporation" - 96OcC7CN8VmIeJLY96PfWBNivqn6quBIZAYslV1kF2wZbKjYBdkn1N5aE0xbzZxyWUTDav0CT7A= SigV4 ECDHE-RSA-AES128-GCM-SHA256 AuthHeader s3-us-west-2.amazonaws.com TLSv1.2
fd89d80d676948bd913040b667965ef6a50a9c80a12f38c504f497953aedc341 SampleS3Bucket [12/Mar/2021:14:26:57 +0000] 117.222.211.65 arn:aws:iam::486031527132:user/jdoe 7G6TJ2NEKZYDR36M REST.GET.POLICY_STATUS - "GET /?policyStatus HTTP/1.1" 404 NoSuchBucketPolicy 307 - 9 - "-" "S3Console/0.4, aws-internal/3 aws-sdk-java/1.11.964 Linux/4.9.230-0.1.ac.224.84.332.metal1.x86_64 OpenJDK_64-Bit_Server_VM/25.282-b08 java/1.8.0_282 vendor/Oracle_Corporation" - 53JowWaGjViTyT0CCtzmzhoxh4WGKe8UcOkwv7Q/HlGVfStvPeDzIzlaFJsJpUujg2j5apQ9VBc= SigV4 ECDHE-RSA-AES128-GCM-SHA256 AuthHeader SampleS3Bucket.s3.us-west-2.amazonaws.com TLSv1.2
错误
异常调用";ParseExact";用";3〃;自变量:";字符串未被识别为有效的DateTime"在行:18 char:1+$parsedLog|Select([datetime]::ParseExact($_.'Time',"dd/MMM/yyyy:HH…+CategoryInfo:NotSpecified:(:([],MethodInvocationException+FullyQualifiedErrorId:FormatException–
然而,当单独考虑日期时,相同的代码也可以正常工作PARSEDATE代码
$date = "12/Mar/2021:14:11:57 +0000"
$date_format = "yyyy-MM-dd HH:mm"
[datetime]::ParseExact($date,"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format)
输出2021-03-12 14:11
我看到了一些问题。
- 首先,您的时间属性没有使用有效的计算表达式
- 其次,在replace语句中添加了一对额外的双引号
- 第三,有些带有空格的房地产名称没有被引用
我认为你应该更改这些线路
-
$parsedLog = $data -replace '[([^]]+)]','"$1"' |ConvertFrom-Csv -Delimiter ' ' -Header $columns
-
$parsedLog | Select Bucket Owner, Bucket, ([datetime]::ParseExact($_.'Time',"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format)), Remote IP, Requester, Request ID, Operation, Key, Request-URI, HTTP status, Error Code, Bytes Sent, Object Size, Total Time,Turn-Around Time, Referer, User-Agen, Version Id, Host Id, Signature Version, Cipher Suite, Authentication Type, Host Header,TLS version
到这些
$parsedLog = $data -replace '[([^]]+)]','$1' |ConvertFrom-Csv -Delimiter ' ' -Header $columns
$parsedLog | Select 'Bucket Owner', Bucket, @{n='Time';e={[datetime]::ParseExact($_.'Time',"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format)}}, 'Remote IP', Requester, 'Request ID', Operation, Key, Request-URI, 'HTTP status', 'Error Code', 'Bytes Sent', 'Object Size', 'Total Time','Turn-Around Time', Referer, User-Agen, 'Version Id', 'Host Id', 'Signature Version', 'Cipher Suite', 'Authentication Type', 'Host Header','TLS version
这也可以用更容易阅读和维护的方式编写。
$selectProps = 'Bucket Owner',
'Bucket',
@{n='Time';e={[datetime]::ParseExact($_.'Time',"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format)}},
'Remote IP',
'Requester',
'Request ID',
'Operation',
'Key',
'Request-URI',
'HTTP status',
'Error Code',
'Bytes Sent',
'Object Size',
'Total Time',
'Turn-Around Time',
'Referer',
'User-Agent',
'Version Id',
'Host Id',
'Signature Version',
'Cipher Suite',
'Authentication Type',
'Host Header',
'TLS version'
$parsedLog | Select-Object $selectProps
$parsedLog | Select ($_.'Time')
正在返回所有列。
$parsedLog | Select {$_.'Time'}
返回时间列。
您的代码确实不喜欢在使用大括号的地方使用大括号。我建议在检索所有列之前,先自行转换$parsedLog中的时间。
所以先做这样的事情:
foreach ($log in $parsedLog){
$log.time = ([datetime]::ParseExact($log.time,"dd/MMM/yyyy:HH:mm:ss +0000",$null).ToSTring($date_format))
}
现在,您可以像其他列一样选择时间。