Skip to main content
deleted 33 characters in body
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

This one grabs the IDNumberIDNumber from the beginning of the filename  (1234-randomfile.csv), then adds the files location to a variable  ($Validate$Validate), then based on the regex, adds files to certain variables  ($Scriptdone$Scriptdone, $Updatedone$Updatedone, $Failed$Failed) and starts the checks to see if they have them.

Like I said, it works, but it is slow. If you have any input on a way to quicken this up, or maybe clean up my code  (I am still learning), it will all be greatly appreciated!

Anyways, Thanks for all the assistance!

This one grabs the IDNumber from the beginning of the filename(1234-randomfile.csv), then adds the files location to a variable($Validate), then based on the regex, adds files to certain variables($Scriptdone, $Updatedone, $Failed) and starts the checks to see if they have them.

Like I said, it works, but it is slow. If you have any input on a way to quicken this up, or maybe clean up my code(I am still learning), it will all be greatly appreciated!

Anyways, Thanks for all the assistance!

This one grabs the IDNumber from the beginning of the filename  (1234-randomfile.csv), then adds the files location to a variable  ($Validate), then based on the regex, adds files to certain variables  ($Scriptdone, $Updatedone, $Failed) and starts the checks to see if they have them.

Like I said, it works, but it is slow. If you have any input on a way to quicken this up, or maybe clean up my code  (I am still learning), it will all be greatly appreciated!

Rollback to Revision 3
Source Link
Mast
  • 13.8k
  • 12
  • 57
  • 127

EDIT: Okay, I have made a few changes and it is going quicker, but still very sluggish.

function Get-MR4RES {
[CmdletBinding()]
param (
    [Parameter(Position = 0,
        Mandatory = $True)]
    [ValidateNotNullorEmpty()]
    [ValidateScript( {Test-Path -Path $_ -PathType 'Any'})]
    [String]
    $Files,
    [Parameter(Position = 1,
        Mandatory = $false)]
    [String]
    $CSVPath) # End Param
begin {
    $Scriptcompletedsuccess = '.+Script\scompleted\ssuccessfully.+' # 3:44:15 End function called, Script completed successfully at  3:44:15 on Tue 07/03/2018
    $Updatecomplete = '\w+\s+\:\s\[\d+\:\d+\:\d+\]\s+\w+\scomplete' # STATUS  : [03:43:07]   Update complete
    $FailedValidaton = '.+check\sfail.+'
    $Fail1 = 'Validation Failed'
    $Fail2 = 'Update Failed'
    $Good1 = 'Script completed'
    $Good2 = 'Update completed'
    $array = @('IDNumber, Results')
    $counter = 0
    $FileList = (Get-ChildItem -Path $Files -File -Filter "*.log").FullName
    $Done = ''
} # End begin
process {
    foreach ($File in $fileList) {
        if ((Get-Item $Files) -is [System.IO.DirectoryInfo]) {
            $counter++
            Write-Progress -Activity 'Analyzing Files' -CurrentOperation $File -PercentComplete (($counter / $FileList.count) * 100)
        }
        $DriveIn = [System.IO.Path]::GetFileName("$File").split('-')[0]
        $Failed = Select-String -Pattern $FailedValidaton -Path $File
        if($Failed){
            $Done = $Fail1
            $array += -join ("$IDNumber",', ',"$Done")
        }
        else {
            $Scriptdone = Select-String -Pattern $Scriptcompletedsuccess -Path $File
            if($Scriptdone){
            $Done = $Good1
            $array += -join ("$IDNumber",', ',"$Done")
            }
            else {
                $Updatedone = Select-String -Pattern $Updatecomplete -Path $File
                if($Updatedone){
                $Done = $Good2
                $array += -join ("$IDNumber",', ',"$Done")
                }
                else {
                    $Done = $Fail2
                    $array += -join ("$IDNumber",', ',"$Done")
                } # End of #Updatedone
            }# End of #Scriptdone
        }# End of #Failed
    } # End of #foreach
} # End process section
End {
    if ($PSBoundParameters.ContainsKey('CSVPath')) {
        Add-Content -Path $CSVPath -Value $array -Encoding ascii
    }
    else {
        Write-Output $array
    } # End of else
} # End of the End
} # End of function

EDIT: Okay, I have made a few changes and it is going quicker, but still very sluggish.

function Get-MR4RES {
[CmdletBinding()]
param (
    [Parameter(Position = 0,
        Mandatory = $True)]
    [ValidateNotNullorEmpty()]
    [ValidateScript( {Test-Path -Path $_ -PathType 'Any'})]
    [String]
    $Files,
    [Parameter(Position = 1,
        Mandatory = $false)]
    [String]
    $CSVPath) # End Param
begin {
    $Scriptcompletedsuccess = '.+Script\scompleted\ssuccessfully.+' # 3:44:15 End function called, Script completed successfully at  3:44:15 on Tue 07/03/2018
    $Updatecomplete = '\w+\s+\:\s\[\d+\:\d+\:\d+\]\s+\w+\scomplete' # STATUS  : [03:43:07]   Update complete
    $FailedValidaton = '.+check\sfail.+'
    $Fail1 = 'Validation Failed'
    $Fail2 = 'Update Failed'
    $Good1 = 'Script completed'
    $Good2 = 'Update completed'
    $array = @('IDNumber, Results')
    $counter = 0
    $FileList = (Get-ChildItem -Path $Files -File -Filter "*.log").FullName
    $Done = ''
} # End begin
process {
    foreach ($File in $fileList) {
        if ((Get-Item $Files) -is [System.IO.DirectoryInfo]) {
            $counter++
            Write-Progress -Activity 'Analyzing Files' -CurrentOperation $File -PercentComplete (($counter / $FileList.count) * 100)
        }
        $DriveIn = [System.IO.Path]::GetFileName("$File").split('-')[0]
        $Failed = Select-String -Pattern $FailedValidaton -Path $File
        if($Failed){
            $Done = $Fail1
            $array += -join ("$IDNumber",', ',"$Done")
        }
        else {
            $Scriptdone = Select-String -Pattern $Scriptcompletedsuccess -Path $File
            if($Scriptdone){
            $Done = $Good1
            $array += -join ("$IDNumber",', ',"$Done")
            }
            else {
                $Updatedone = Select-String -Pattern $Updatecomplete -Path $File
                if($Updatedone){
                $Done = $Good2
                $array += -join ("$IDNumber",', ',"$Done")
                }
                else {
                    $Done = $Fail2
                    $array += -join ("$IDNumber",', ',"$Done")
                } # End of #Updatedone
            }# End of #Scriptdone
        }# End of #Failed
    } # End of #foreach
} # End process section
End {
    if ($PSBoundParameters.ContainsKey('CSVPath')) {
        Add-Content -Path $CSVPath -Value $array -Encoding ascii
    }
    else {
        Write-Output $array
    } # End of else
} # End of the End
} # End of function
Added the modified content, Changed get-content to select-string, modified if-else statement structure.
Source Link

EDIT: Okay, I have made a few changes and it is going quicker, but still very sluggish.

function Get-MR4RES {
[CmdletBinding()]
param (
    [Parameter(Position = 0,
        Mandatory = $True)]
    [ValidateNotNullorEmpty()]
    [ValidateScript( {Test-Path -Path $_ -PathType 'Any'})]
    [String]
    $Files,
    [Parameter(Position = 1,
        Mandatory = $false)]
    [String]
    $CSVPath) # End Param
begin {
    $Scriptcompletedsuccess = '.+Script\scompleted\ssuccessfully.+' # 3:44:15 End function called, Script completed successfully at  3:44:15 on Tue 07/03/2018
    $Updatecomplete = '\w+\s+\:\s\[\d+\:\d+\:\d+\]\s+\w+\scomplete' # STATUS  : [03:43:07]   Update complete
    $FailedValidaton = '.+check\sfail.+'
    $Fail1 = 'Validation Failed'
    $Fail2 = 'Update Failed'
    $Good1 = 'Script completed'
    $Good2 = 'Update completed'
    $array = @('IDNumber, Results')
    $counter = 0
    $FileList = (Get-ChildItem -Path $Files -File -Filter "*.log").FullName
    $Done = ''
} # End begin
process {
    foreach ($File in $fileList) {
        if ((Get-Item $Files) -is [System.IO.DirectoryInfo]) {
            $counter++
            Write-Progress -Activity 'Analyzing Files' -CurrentOperation $File -PercentComplete (($counter / $FileList.count) * 100)
        }
        $DriveIn = [System.IO.Path]::GetFileName("$File").split('-')[0]
        $Failed = Select-String -Pattern $FailedValidaton -Path $File
        if($Failed){
            $Done = $Fail1
            $array += -join ("$IDNumber",', ',"$Done")
        }
        else {
            $Scriptdone = Select-String -Pattern $Scriptcompletedsuccess -Path $File
            if($Scriptdone){
            $Done = $Good1
            $array += -join ("$IDNumber",', ',"$Done")
            }
            else {
                $Updatedone = Select-String -Pattern $Updatecomplete -Path $File
                if($Updatedone){
                $Done = $Good2
                $array += -join ("$IDNumber",', ',"$Done")
                }
                else {
                    $Done = $Fail2
                    $array += -join ("$IDNumber",', ',"$Done")
                } # End of #Updatedone
            }# End of #Scriptdone
        }# End of #Failed
    } # End of #foreach
} # End process section
End {
    if ($PSBoundParameters.ContainsKey('CSVPath')) {
        Add-Content -Path $CSVPath -Value $array -Encoding ascii
    }
    else {
        Write-Output $array
    } # End of else
} # End of the End
} # End of function

EDIT: Okay, I have made a few changes and it is going quicker, but still very sluggish.

function Get-MR4RES {
[CmdletBinding()]
param (
    [Parameter(Position = 0,
        Mandatory = $True)]
    [ValidateNotNullorEmpty()]
    [ValidateScript( {Test-Path -Path $_ -PathType 'Any'})]
    [String]
    $Files,
    [Parameter(Position = 1,
        Mandatory = $false)]
    [String]
    $CSVPath) # End Param
begin {
    $Scriptcompletedsuccess = '.+Script\scompleted\ssuccessfully.+' # 3:44:15 End function called, Script completed successfully at  3:44:15 on Tue 07/03/2018
    $Updatecomplete = '\w+\s+\:\s\[\d+\:\d+\:\d+\]\s+\w+\scomplete' # STATUS  : [03:43:07]   Update complete
    $FailedValidaton = '.+check\sfail.+'
    $Fail1 = 'Validation Failed'
    $Fail2 = 'Update Failed'
    $Good1 = 'Script completed'
    $Good2 = 'Update completed'
    $array = @('IDNumber, Results')
    $counter = 0
    $FileList = (Get-ChildItem -Path $Files -File -Filter "*.log").FullName
    $Done = ''
} # End begin
process {
    foreach ($File in $fileList) {
        if ((Get-Item $Files) -is [System.IO.DirectoryInfo]) {
            $counter++
            Write-Progress -Activity 'Analyzing Files' -CurrentOperation $File -PercentComplete (($counter / $FileList.count) * 100)
        }
        $DriveIn = [System.IO.Path]::GetFileName("$File").split('-')[0]
        $Failed = Select-String -Pattern $FailedValidaton -Path $File
        if($Failed){
            $Done = $Fail1
            $array += -join ("$IDNumber",', ',"$Done")
        }
        else {
            $Scriptdone = Select-String -Pattern $Scriptcompletedsuccess -Path $File
            if($Scriptdone){
            $Done = $Good1
            $array += -join ("$IDNumber",', ',"$Done")
            }
            else {
                $Updatedone = Select-String -Pattern $Updatecomplete -Path $File
                if($Updatedone){
                $Done = $Good2
                $array += -join ("$IDNumber",', ',"$Done")
                }
                else {
                    $Done = $Fail2
                    $array += -join ("$IDNumber",', ',"$Done")
                } # End of #Updatedone
            }# End of #Scriptdone
        }# End of #Failed
    } # End of #foreach
} # End process section
End {
    if ($PSBoundParameters.ContainsKey('CSVPath')) {
        Add-Content -Path $CSVPath -Value $array -Encoding ascii
    }
    else {
        Write-Output $array
    } # End of else
} # End of the End
} # End of function
deleted 3 characters in body; edited tags; edited title
Source Link
200_success
  • 145.6k
  • 22
  • 191
  • 481
Loading
Removed request for change in code to stack overflow.
Source Link
Loading
Source Link
Loading