繁体   English   中英

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

[英]Powershell - how to parse date from string then compare that to last 30 days

我再次谦虚地处理堆栈溢出问题,以解决Powershell问题。 我在视频游戏空间工程师中使用的服务器现在已经使用了几个月,我遇到了一个常见问题。 因此,这是:游戏中有许多对象,用户出于自己的原因而放弃了服务器。 不幸的是,游戏处于Alpha状态,专用服务器的控制力有限。

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

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

$filePath = 'F:\DedicatedServer\DataDir\VPS Dedicated 1\Saves\VPS RC 1\SANDBOX_0_0_0_.sbs'
$filePath2 = 'F:\DedicatedServer\DataDir\VPS Dedicated 1\Saves\VPS RC 1\SANDBOX.sbc'
$CurrentDateTime = Get-Date -Format "MM-dd-yyyy_HH-mm"
$playerslog = "W:\Google Drive\Admin Logs\Audits\Active 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:\DedicatedServer\DataDir\VPS 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:\DedicatedServer\DataDir\VPS 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:\DedicatedServer\DataDir\VPS Dedicated 1\SpaceEngineersDedicated_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天。 然后,我可以决定如何处理该对象。

为在正确的道路上开始我而感到敬畏。 我创建了下面的代码来回答我的问题!

$regex = "\d{4}-\d{2}-\d{2}"
$dte = Get-Date
$dte = $dte.AddDays(-30)
$dte = $dte.DayofYear

$filePath = 'F:\DedicatedServer\DataDir\VPS Dedicated 1\Saves\VPS RC 1\SANDBOX_0_0_0_.sbs'
$filePath2 = 'F:\DedicatedServer\DataDir\VPS Dedicated 1\Saves\VPS RC 1\SANDBOX.sbc'
$CurrentDateTime = Get-Date -Format "MM-dd-yyyy_HH-mm"
$playerslog = "W:\Google Drive\Admin Logs\Audits\Active 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:\DedicatedServer\DataDir\VPS 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

暂无
暂无

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

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