2014-09-23 8 views
0

我再次虛心地處理堆棧溢出問題。我的視頻遊戲太空工程師的服務器現在已經有幾個月的時間了,我遇到了一個常見問題。這就是:我們在遊戲中有很多對象,因爲用戶因爲自己的原因放棄了服務器。不幸的是,遊戲處於alpha狀態,專用服務器控制有限。Powershell - 如何從字符串解析日期,然後將其與過去30天進行比較

我需要一個腳本從字符串中提取一個日期,然後比較一下,看看玩家最後一次登錄的時間是否大於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年9月20日」的示例結果,並將其與一個值進行比較以確定它是否比當前日期早30天。然後我可以決定如何處理對象。

回答

0

試試這個,如果工作:

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

雖然沒有答案,這導致我在正確的道路上解決我自己的問題。感謝您提及正則表達式! – Tostito80 2014-09-24 03:14:15

1

榮譽給瓦利德開始我在正確的道路上。我創建了下面的代碼來回答我的問題!

$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" 
        } 
       } 
      } 
     } 

    } 
    } 
相關問題