I again humbly approach stack overflow for a powershell question. My server in the video game space engineers is now several months old and i am running into a common problem. Which is thus: we have many objects in game that the users have abandoned the server for their own reasons. unfortunately the game is in an alpha state and dedicated servers have limited control.
I need a script that extracts a date from a string and then compares that to see if the last time the player logs in is greater than 30 days old. so then i can delete the XML node of their game object.
here is what i have thus far:
$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)"
}
}
}
This yields similar to the following example:
[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}
=
I need to grab the date "2014-09-20" from line 6 of the example result and compare that to a value to determine if it is 30 days older than the current date. Then i can decide what to do with the object.
kudos to walid for starting me on the right path. I created the code below to answer my question!
$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"
}
}
}
}
}
}
Try this if work:
$log = [datetime][regex]::match($data,'(?<=:[0-9]+:)[0-9]{4}(?:-[0-9]{2}){2}').value
(date) - $log
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.