Имя пользователя:
Пароль:  
Помощь | Регистрация | Забыли пароль?  

Показать сообщение отдельно

Ветеран


Contributor


Сообщения: 24953
Благодарности: 7249

Профиль | Отправить PM | Цитировать


Цитата v79italya:
и не поймешь - запустился или не запустился скрипт. »
Ну, давайте чуть поменяем:
Скрытый текст
Код: Выделить весь код
$sArchiveFile    = 'C:\Мои проекты\0272\project.iwprj2'
$sSourceFileName = 'project'
$sPath27Za       = "$($env:ProgramFiles)\7-Zip\7za.exe"

if([System.IO.File]::Exists($sArchiveFile)) {
    if([System.IO.File]::Exists($sPath27Za)) {
        $oProcess = New-Object -TypeName 'System.Diagnostics.Process'

        $oProcess.StartInfo.FileName               = $sPath27Za
        $oProcess.StartInfo.Arguments              = "e -y -sccDOS -o""$($env:TEMP)"" ""$sArchiveFile"" ""$sSourceFileName"""
        $oProcess.StartInfo.CreateNoWindow         = $true
        $oProcess.StartInfo.UseShellExecute        = $false
        $oProcess.StartInfo.RedirectStandardOutput = $true

        if($oProcess.Start()) {
            Write-Host "Trying extract source file [$sSourceFileName] from archive [$sArchiveFile] into temp folder [$($env:TEMP)]." -ForegroundColor Green
            $oProcess.WaitForExit()
            Write-Host $($oProcess.StandardOutput.ReadToEnd()) -ForegroundColor Cyan

            if($oProcess.ExitCode -eq 0) {
                $sTempFile = "$($env:TEMP)\$sSourceFileName"
                Write-Host "Trying find and replace value in extracted file [$sTempFile]." -ForegroundColor Green

                try {
                    $oXmlDocument = [System.Xml.XmlDocument](Get-Content -Path $sTempFile)
                    $oXmlDocument.StateOfInfoProject.CaptchaID = '27'
                    $oXmlDocument.Save($sTempFile)
                } catch {
                    Write-Host "Can't find and replace value in extracted file [$sTempFile]." -ForegroundColor Red
                }

                $oProcess.StartInfo.Arguments = "u -y -sccDOS ""$sArchiveFile"" ""$sTempFile"""

                if($oProcess.Start()) {
                    Write-Host "Trying update source file [$sSourceFileName] in archive [$sArchiveFile] from previously extracted file [$sTempFile]." -ForegroundColor Green
                    $oProcess.WaitForExit()
                    Write-Host $($oProcess.StandardOutput.ReadToEnd()) -ForegroundColor Cyan

                    if($oProcess.ExitCode -eq 0) {
                        Write-Host "Delete temporary file [$sTempFile]." -ForegroundColor Green
                        [System.IO.File]::Delete($sTempFile)
                    } else {
                        Write-Host "Can't update file [$sSourceFileName] in archive [$sArchiveFile] from file [$sTempFile] correctly. 7za.exe return non-zero exit code: $($oProcess.ExitCode)." -ForegroundColor Red
                    }
                } else {
                    Write-Host "Can't execute [$($oProcess.StartInfo.FileName)] with arguments [$($oProcess.StartInfo.Arguments)] normally." -ForegroundColor Red
                }
            } else {
                Write-Host "Can't extract source file [$sSourceFileName] from archive [$sArchiveFile] correctly. 7za.exe return non-zero exit code: $($oProcess.ExitCode)." -ForegroundColor Red
            }
        } else {
            Write-Host "Can't execute [$($oProcess.StartInfo.FileName)] with arguments [$($oProcess.StartInfo.Arguments)] normally." -ForegroundColor Red
        }
    } else {
        Write-Host "Can't find executable file [$sPath27Za]." -ForegroundColor Red
    }
} else {
    Write-Host "Can't find archive file [$sArchiveFile]." -ForegroundColor Red
}

Write-Host 'Press any key to continue...'
[void][System.Console]::ReadKey($true)

Отправлено: 09:52, 20-08-2019 | #23