Powershell-如何从字符串中解析日期,然后将其与过去30天进行比较



我再次谦虚地处理powershell问题的堆栈溢出。我在电子游戏太空工程师中的服务器已经使用了几个月,我遇到了一个常见的问题。因此:我们在游戏中有许多对象,用户出于自己的原因放弃了服务器。不幸的是,游戏处于alpha状态,专用服务器的控制能力有限。

我需要一个从字符串中提取日期的脚本,然后对其进行比较,看看玩家上次登录的时间是否超过30天。这样我就可以删除他们游戏对象的XML节点。

到目前为止,我拥有的是:

$filePath = 'F:DedicatedServerDataDirVPS Dedicated 1SavesVPS RC 1SANDBOX_0_0_0_.sbs'
$filePath2 = 'F:DedicatedServerDataDirVPS Dedicated 1SavesVPS RC 1SANDBOX.sbc'
$CurrentDateTime = Get-Date -Format "MM-dd-yyyy_HH-mm"
$playerslog = "W:Google DriveAdmin LogsAuditsActive Players"
$playerfilename = "Players_log_" +$CurrentDateTime+ ".log"
$playerspath = $playerslog + $playerfilename
[xml]$myXML = Get-Content $filePath
$ns = New-Object System.Xml.XmlNamespaceManager($myXML.NameTable)
$ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
[xml]$myXML2 = Get-Content $filePath2
$ns2 = New-Object System.Xml.XmlNamespaceManager($myXML2.NameTable)
$ns2.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
New-Item -path $playerspath -type file
Add-Content -path $playerspath -Value "[$([DateTime]::Now)] FoH Space Engineers Dedicated     Players Audit Log  ==================="
#find cubegrid cockpits by owner
Add-Content -Path $playerspath -Value "="
Add-Content -Path $playerspath -Value "Ships ========="
$nodePIDs = $myXML2.SelectNodes("//AllPlayers/PlayerItem"  , $ns2) 
$nodeOwns = $myXML.SelectNodes("//SectorObjects/MyObjectBuilder_EntityBase[IsStatic='false' and (@xsi:type='MyObjectBuilder_CubeGrid')]/CubeBlocks/MyObjectBuilder_CubeBlock[(@xsi:type='MyObjectBuilder_Cockpit')]"  , $ns)
ForEach($node in $nodePIDs){
    $nodename = $node.Name
    $findlogin = dir 'F:DedicatedServerDataDirVPS Dedicated 1' -Include *.log -Recurse | Select-String -Pattern "OnConnectedPlayer $nodename" 
    Add-Content -Path $playerspath -Value "="
    #Write-Host -ForegroundColor Green $node.PlayerId " "$node.Name "is Dead? :" $node.IsDead "!" 
    Add-Content -Path $playerspath -Value "[$($node.PlayerId)] [$($node.Name)] is Dead? : [$($node.IsDead)] !"
    #Write-Host -ForegroundColor Green "Last login:" $findlogin[-1]
    Add-Content -Path $playerspath -Value "Last login: [$($findlogin[-1])]" -EA SilentlyContinue
    #Write-Host -ForegroundColor Green "Has ownership of"
    Add-Content -Path $playerspath -Value "****Ships owned****"
    ForEach($node2 in $nodeOwns){
        if ($node.PlayerId -eq $node2.Owner){
        #$node2.ParentNode.ParentNode.DisplayName
        Add-Content -Path $playerspath -Value "$($node2.ParentNode.ParentNode.DisplayName) Coordinates: $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select X) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Y) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Z)"
        }
    }
    }
#find cubegrid stations by owner
Add-Content -Path $playerspath -Value "="
Add-Content -Path $playerspath -Value "Stations ========="
$nodePIDs = $myXML2.SelectNodes("//AllPlayers/PlayerItem"  , $ns2) 
$nodeOwns = $myXML.SelectNodes("//SectorObjects/MyObjectBuilder_EntityBase[IsStatic='true' and (@xsi:type='MyObjectBuilder_CubeGrid')]/CubeBlocks/MyObjectBuilder_CubeBlock[(@xsi:type='MyObjectBuilder_Refinery')]"  , $ns)
ForEach($node in $nodePIDs){
    $nodename = $node.Name
    $findlogin = dir 'F:DedicatedServerDataDirVPS Dedicated 1' -Include *.log -Recurse | Select-String -Pattern "OnConnectedPlayer $nodename" 
    Add-Content -Path $playerspath -Value "="
    #Write-Host -ForegroundColor Green $node.PlayerId " "$node.Name "is Dead? :" $node.IsDead "!" 
    Add-Content -Path $playerspath -Value "[$($node.PlayerId)] [$($node.Name)] is Dead? : [$($node.IsDead)] !"
    #Write-Host -ForegroundColor Green "Last login:" $findlogin[-1]
    Add-Content -Path $playerspath -Value "Last login: [$($findlogin[-1])]" -EA SilentlyContinue
    #Write-Host -ForegroundColor Green "Has ownership of"
    Add-Content -Path $playerspath -Value "****Stations owned****"
    ForEach($node2 in $nodeOwns){
        if ($node.PlayerId -eq $node2.Owner){
        #$node2.ParentNode.ParentNode.DisplayName
        Add-Content -Path $playerspath -Value "$($node2.ParentNode.ParentNode.DisplayName) Coordinates: $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select X) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Y) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Z)"
        }
    }
    }

这与以下示例类似:

[09/21/2014 12:14:47] FoH Space Engineers Dedicated Players Audit Log  ===================
=
Ships =========
=
[144150095890427719] [WWJDROJC] is Dead? : [false] !
Last login: [F:DedicatedServerDataDirVPS Dedicated 1SpaceEngineersDedicated_20140920_223300.log:1417:2014-09-20 23:11:20.165 - Thread:   6 ->  OnConnectedPlayer WWJDROJC attempt]
****Ships owned****
Erador Ind. Small Freighter Coordinates: @{x=-13380.1563} , @{y=-1520.06714} , @{z=-6649.97949}
WWJD'S Torp Boat Coordinates: @{x=-13324.3037} , @{y=-1511.96716} , @{z=-6655.992}
WW's Welder Coordinates: @{x=-13349.0645} , @{y=-1567.62659} , @{z=-6631.092}
Speed's Welder Coordinates: @{x=-13378.6934} , @{y=-1517.177} , @{z=-6666.328}
WW's Grinder Coordinates: @{x=-13383.8643} , @{y=-1523.86548} , @{z=-6662.607}
WWJD's Extreme miner Coordinates: @{x=-13051.2822} , @{y=-487.573151} , @{z=-5810.092}
WW'S Tug Coordinates: @{x=-13416.6328} , @{y=-1521.7019} , @{z=-6634.372}
WW'S Stealth Fighter Coordinates: @{x=-13421.85} , @{y=-1546.42017} , @{z=-6661.154}
=

我需要从示例结果的第6行获取日期"2014-09-20",并将其与一个值进行比较,以确定它是否比当前日期早30天。然后我可以决定如何处理这个对象。

感谢walid让我走上了正确的道路。我创建了下面的代码来回答我的问题!
$regex = "d{4}-d{2}-d{2}"
$dte = Get-Date
$dte = $dte.AddDays(-30)
$dte = $dte.DayofYear
$filePath = 'F:DedicatedServerDataDirVPS Dedicated 1SavesVPS RC 1SANDBOX_0_0_0_.sbs'
$filePath2 = 'F:DedicatedServerDataDirVPS Dedicated 1SavesVPS RC 1SANDBOX.sbc'
$CurrentDateTime = Get-Date -Format "MM-dd-yyyy_HH-mm"
$playerslog = "W:Google DriveAdmin LogsAuditsActive Players"
$playerfilename = "Players_log_" +$CurrentDateTime+ ".log"
$playerspath = $playerslog + $playerfilename
[xml]$myXML = Get-Content $filePath
$ns = New-Object System.Xml.XmlNamespaceManager($myXML.NameTable)
$ns.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")
[xml]$myXML2 = Get-Content $filePath2
$ns2 = New-Object System.Xml.XmlNamespaceManager($myXML2.NameTable)
$ns2.AddNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance")

New-Item -path $playerspath -type file
Add-Content -path $playerspath -Value "[$([DateTime]::Now)] FoH Space Engineers Dedicated Players Audit Log  ==================="
#find cubegrid cockpits by owner
Add-Content -Path $playerspath -Value "="
Add-Content -Path $playerspath -Value "Ships ========="
$nodePIDs = $myXML2.SelectNodes("//AllPlayers/PlayerItem"  , $ns2) 
$nodeOwns = $myXML.SelectNodes("//SectorObjects/MyObjectBuilder_EntityBase[IsStatic='false' and (@xsi:type='MyObjectBuilder_CubeGrid')]/CubeBlocks/MyObjectBuilder_CubeBlock[(@xsi:type='MyObjectBuilder_Cockpit')]"  , $ns)
ForEach($node in $nodePIDs){
    $nodename = $node.Name
    $findlogin = dir 'F:DedicatedServerDataDirVPS Dedicated 1' -Include *.log -Recurse | Select-String -Pattern "OnConnectedPlayer $nodename" 
    Add-Content -Path $playerspath -Value "="
    #Write-Host -ForegroundColor Green $node.PlayerId " "$node.Name "is Dead? :" $node.IsDead "!" 
    Add-Content -Path $playerspath -Value "[$($node.PlayerId)] [$($node.Name)] is Dead? : [$($node.IsDead)] !"
    #Write-Host -ForegroundColor Green "Last login:" $findlogin[-1]
    Add-Content -Path $playerspath -Value "Last login: [$($findlogin[-1])]" -EA SilentlyContinue
    #Write-Host -ForegroundColor Green "Has ownership of"
    Add-Content -Path $playerspath -Value "****Ships owned****"
    ForEach($node2 in $nodeOwns){
        if ($node.PlayerId -eq $node2.Owner){
        #$node2.ParentNode.ParentNode.DisplayName
          Add-Content -Path $playerspath -Value "$($node2.ParentNode.ParentNode.DisplayName) Coordinates: $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select X) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Y) , $($node2.ParentNode.ParentNode.PositionAndOrientation.position | Select Z)"
            $matchInfos = @(Select-String -Pattern $regex -AllMatches -InputObject [$($findlogin[-1])])
            foreach ($minfo in $matchInfos){
                foreach ($match in @($minfo.Matches | Foreach {$_.Groups[0].value})){
                    if ([datetime]::parseexact($match, "yyyy-MM-dd", $null).DayOfYear -lt $dte){
                       Add-Content -Path $playerspath -Value "this grid would have been deleted"
                    }
                }
           }
        }
    }
    }

如果有效,请尝试以下操作:

$log = [datetime][regex]::match($data,'(?<=:[0-9]+:)[0-9]{4}(?:-[0-9]{2}){2}').value
(date) - $log

相关内容

  • 没有找到相关文章

最新更新