本帖最后由 flashercs 于 2022-8-1 15:46 编辑
回复 11# linlianboy - <#*,:&cls
- @echo off
- cd /d "%~dp0"
- powershell -C "Set-Location -LiteralPath ([Environment]::CurrentDirectory);. ([ScriptBlock]::Create((Get-Content -LiteralPath \"%~f0\" -ReadCount 0 | Out-String)))"
-
- exit /b
- #>
- # 超时90分钟
- $span0 = New-TimeSpan -Minutes 90
- # ini file
- $inifile = ".\1.ini"
-
- function Get-DataHash {
- [CmdletBinding(DefaultParameterSetName = "Path")]
- [OutputType('Microsoft.Powershell.Utility.FileHash')]
- param(
- [Parameter(Mandatory = $true, ParameterSetName = "Path", ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0)]
- [System.String[]]
- $Path,
- [Parameter(Mandatory = $true, ParameterSetName = "LiteralPath", ValueFromPipelineByPropertyName = $true)]
- [ValidateNotNull()]
- [Alias("PSPath")]
- [System.String[]]
- $LiteralPath,
- [Parameter(Mandatory = $true, ParameterSetName = "Stream", ValueFromPipeline = $true, Position = 0)]
- [System.IO.Stream]
- $InputStream,
- [Parameter(Mandatory = $true, ParameterSetName = "Bytes", Position = 0)]
- [byte[]]
- $Buffer,
- [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
- [int]
- $Offset = -1,
- [Parameter(Mandatory = $false, ParameterSetName = "Bytes")]
- [int]
- $Count = -1,
- [Parameter(Mandatory = $false)]
- [ValidateSet("SHA1", "SHA256", "SHA384", "SHA512", "MD5", "RIPEMD160")]
- [System.String]
- $Algorithm = "SHA256",
- # Consider all input data as a single data.
- [switch]$AllTogether,
- # return Hash as byte[]
- [ValidateSet("Hex", "Base64", "Bytes")]
- [string]$HashMode = "Hex"
- )
- begin {
- # Construct the strongly-typed crypto object
- [System.Security.Cryptography.HashAlgorithm]$hashAlg = [System.Security.Cryptography.HashAlgorithm]::Create($Algorithm)
- $Algorithm = $Algorithm.ToUpperInvariant()
- if ($AllTogether) {
- $cryptoStream = New-Object System.Security.Cryptography.CryptoStream -ArgumentList ([System.IO.Stream]::Null, $hashAlg, 'Write')
- }
- function GetPso {
- param (
- [byte[]]$Hash,
- [string]$RelatedPath
- )
- ($pso = 0 | Select-Object -Property @{
- Name = "Algorithm"
- Expression = { $Algorithm }
- }, @{
- Name = "Hash"
- Expression = {
- switch ($HashMode) {
- "Hex" { [System.BitConverter]::ToString($Hash) -replace "-" }
- "Base64" { [convert]::ToBase64String($Hash) }
- "Bytes" { $Hash }
- }
- }
- }, @{
- Name = "Path"
- Expression = { $RelatedPath }
- }) | Add-Member -TypeName Microsoft.Powershell.Utility.FileHash
- $pso
- # New-Object psobject -Property ([ordered]@{
- # Algorithm = $Algorithm
- # Hash = [System.BitConverter]::ToString($Hash) -replace "-", ""
- # Path = $RelatedPath
- # })
- }
- }
- process {
- switch ($PSCmdlet.ParameterSetName) {
- "Stream" {
- if ($AllTogether) {
- $InputStream.CopyTo($cryptoStream)
- } else {
- GetPso -Hash ($hashAlg.ComputeHash($InputStream))
- }
- continue
- }
- "Bytes" {
- if ($AllTogether) {
- if ($Offset -lt 0 -or $Count -lt 0) {
- $cryptoStream.Write($Buffer, 0, $Buffer.Count)
- } else {
- $cryptoStream.Write($Buffer, $Offset, $Count)
- }
- } else {
- if ($Offset -lt 0 -or $Count -lt 0) {
- $hash = $hashAlg.ComputeHash($Buffer)
- } else {
- $hash = $hashAlg.ComputeHash($Buffer, $Offset, $Count)
- }
- GetPso -Hash $hash
- }
- continue
- }
- "Path" {
- $pathsToProcess = Convert-Path -Path $Path
- }
- "LiteralPath" {
- $pathsToProcess = Convert-Path -LiteralPath $LiteralPath
- }
- { $true } {
- foreach ($filePath in $pathsToProcess) {
- if (Test-Path -LiteralPath $filePath -PathType Container) {
- continue
- }
- try {
- # Read the file specified in $FilePath as a Byte array
- $stream = [System.IO.File]::OpenRead($filePath)
- if ($AllTogether) {
- $stream.CopyTo($cryptoStream)
- } else {
- GetPso -Hash ($hashAlg.ComputeHash($stream)) -RelatedPath $filePath
- }
- } catch {
- # $PSCmdlet.WriteError($_)
- } finally {
- if ($null -ne $stream) {
- $stream.Dispose()
- }
- }
- }
- break
- }
- }
- }
- end {
- if ($AllTogether) {
- $cryptoStream.Clear()
- $cryptoStream.Dispose()
- GetPso -Hash ($hashAlg.Hash)
- }
- $hashAlg.Clear()
- $hashAlg.Dispose()
- }
- }
- # main
- $pso = New-Object psobject
- $pso | Add-Member -MemberType NoteProperty -Name UpdateTime -Value (Get-Date)
- $pso | Add-Member -MemberType NoteProperty -Name CurrentTime -Value (Get-Date)
- $pso | Add-Member -MemberType ScriptProperty -Name TimeElapsed -Value { $this.CurrentTime - $this.UpdateTime }
- $pso | Add-Member -MemberType NoteProperty -Name FileHash -Value $null
- while ($true) {
- $pso.CurrentTime = Get-Date
- $timeOfDay = $pso.CurrentTime.TimeOfDay
- if ($timeOfDay -ge '00:00:00' -and $timeOfDay -lt '23:00:00') {
- $oHash = Get-DataHash -LiteralPath $inifile -Algorithm SHA256
- if ($pso.FileHash -ne $oHash.Hash) {
- $pso.FileHash = $oHash.Hash
- $pso.UpdateTime = $pso.CurrentTime
- }
- $pso
- if ($pso.TimeElapsed -gt $span0) {
- '超时1.5小时'
- $(
- & "C:\ldplayer\Ldconsole.exe" quitall
- choice /t 2 /d y /n
- taskkill /f /im my.exe
- taskkill /f/ im dnmultiplayer.exe
- taskkill /f /im LdVBoxHeadless.exe
- taskkill /f /im LdVBoxSVC.exe
- shutdown.exe /r /f /t 0
- )>$null 2>$null
- break
- } else {
- '未超时'
- }
- }
- Start-Sleep -Seconds 10
- }
复制代码
|