我再次谦虚地接近堆栈溢出的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}
=
我需要抓住日期" 2014-09-20"从示例结果的第6行开始,并将其与一个值进行比较,以确定它是否比当前日期早30天。然后我可以决定如何处理对象。
答案 0 :(得分: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"
}
}
}
}
}
}
答案 1 :(得分:0)
如果工作,请尝试此操作:
$log = [datetime][regex]::match($data,'(?<=:[0-9]+:)[0-9]{4}(?:-[0-9]{2}){2}').value
(date) - $log