Execute file based on Conditional

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 9 months ago.

  • Author
    Posts
  • #5592

    by ventu at 2013-02-25 21:06:50

    Good evening, all – this thread is phase 2 of Data Verification: viewtopic.php?f=2&t=1169. The backup solution that we are using the backup and restore our client's data is Zenith Arca. It creating 15 minute incremental backups and then collapses them into a daily, which then gets transferred offsite.

    What I'm stuck with this time, is actually executing the .cmd file. This is the command to execute the data verification:
    verifychain.cmd D:\backup\server\C_VOL-b001.spk D:\backup\server\C_VOL-b001-i1241.spi
    SPK File = Encryption file
    SPI file = Latest created 15 minute incremental file

    The following is my code: (Testing purposes)
    #Picks up how many servers are listed in the Backup directory
    $filenames = GCI -path c:\Backup | % {$_.BaseName}

    #Supposed to gather SPK and SPI files used in execution
    $spks = gci -path c:\backup\$filename\*_VOL*.spk | Select-Object -ExpandProperty Name
    $spis = gci -Path C]_VOL-b00*.spi | Select-Object -ExpandProperty name -Last 10

    #Where the Magic happens - We're using foreach to run verification script on each drive on each server
    #The Drive (C_VOL) and the Base image (b001) both need to match. For both the SPK and SPI files
    foreach ($filename in $filenames){
    If ($spks -like 'C_VOL-b001.spk' -and $spis -like 'C_VOL-b001*.spi') {Write-host 'C'}
    elseif ($spks -like 'C_VOL-b002.spk' -and $spis -like 'C_VOL-b002*.spi') {Write-host 'D'}
    Else {Write-host "Whatever"}
    }

    Directory Layout:

    C:\backup\abc1234-server1\
    |
    C_VOL-b001.spk
    C_VOL-b001-i42443.spi
    E_VOL-b001.spk
    E_VOL-b001-42183.spi

    C:\backup\abc2345-server2\
    |
    C_VOL-b002.spk
    C_VOL-b002-i42443.spi
    E_VOL-b002.spk
    E_VOL-b002-42183.spi

    The Execution:
    This is all the code executes. it seems like it's only looking at the last directory in the foreach conditional..
    D
    D

    If i attempt to just run $spis
    C_VOL-b002-i42443.spi
    E_VOL-b002-i42183.spi

    What's not working:
    [list]
    I believe the Foreach command isn't hitting each folder and executing the If conditionals
    I don't believe I have my wildcards right in the variable
    [/list]
    My Thoughts :
    [list]
    First thing..am I really over thinking this problem(Which at times happens)? Could i use some type of an array to assist with my code?
    If not, I'm picturing a long list of elseifs.
    [/list]

    I know this is lengthy request, and if you'd like more information I'd be glad to provide it. Thank you again!

    by DonJ at 2013-02-26 09:48:03

    Well, you've written this a bit wrong I think. If I'm following what you're trying to do.


    #Picks up how many servers are listed in the Backup directory
    $filenames = GCI -path c:\Backup | % {$_.BaseName}

    #Supposed to gather SPK and SPI files used in execution
    $spks = gci -path c:\backup\$filename\*_VOL*.spk | Select-Object -ExpandProperty Name
    $spis = gci -Path C:\backup\$filename\[C-H]_VOL-b00*.spi | Select-Object -ExpandProperty name -Last 10

    #Where the Magic happens - We're using foreach to run verification script on each drive on each server
    #The Drive (C_VOL) and the Base image (b001) both need to match. For both the SPK and SPI files
    foreach ($filename in $filenames){
    If ($spks -like 'C_VOL-b001.spk' -and $spis -like 'C_VOL-b001*.spi') {Write-host 'C'}
    elseif ($spks -like 'C_VOL-b002.spk' -and $spis -like 'C_VOL-b002*.spi') {Write-host 'D'}
    Else {Write-host "Whatever"}
    }

    $spks and $spis can have more than one file in them, yes? Yet you're not enumerating those. You're enumerating what's in $filenames, but you're never actually using $filename. Anyway... I guess I'm not clear on what you're doing. As written, if $spks or $spis contain more than one object, I'd expect the -like comparison to fail. It doesn't compare against collections. And I don't understand what you mean to do with $filename.

    I see where you're using $filename... but it's OUTSIDE the ForEach loop where $filename gets populated. Currently, where you're using $filename, it has no meaning.

    I'd suggest adding some code so you can SEE what values are being used, as they're being used. Debugging 101. E.g, instead of:


    $spks = gci -path c:\backup\$filename\*_VOL*.spk | Select-Object -ExpandProperty Name

    Do this:


    $path = c:\backup\$filename\*_VOL*.spk
    Write-Host "Path is $path"
    $spks = gci -path $path | Select-Object -ExpandProperty Name

    I think that'll help you track down your problem. 99% of logic errors happen because a variable contains something other than what you thought it contained. So you have to see what's actually going on inside your script.

    by ventu at 2013-02-26 15:33:16

    Thank you Don for the reply/assistance.

    [quote]$spks and $spis can have more than one file in them, yes?[/quote]

    You are correct, there can be more than one file in them.

    [quote]And I don't understand what you mean to do with $filename.

    I see where you're using $filename... but it's OUTSIDE the ForEach loop where $filename gets populated. Currently, where you're using $filename, it has no meaning.[/quote]
    I'm using $filename to pin point the parent folder of the spk and spi files. For example, D:\backup\server1, or D:\backup\server2. Inside these directories is where the backups are located.

    I'll perform the changes you have suggested and go form there. Thanks again!

    by DonJ at 2013-02-26 15:38:53

    So, $filename has to be INSIDE the ForEach loop, then. Do you maybe mean...


    $filenames = GCI -path c:\Backup | % {$_.BaseName}

    foreach ($filename in $filenames){
    $spks = gci -path c:\backup\$filename\*_VOL*.spk | Select-Object -ExpandProperty Name
    $spis = gci -Path C:\backup\$filename\[C-H]_VOL-b00*.spi | Select-Object -ExpandProperty name -Last 10
    If ($spks -like 'C_VOL-b001.spk' -and $spis -like 'C_VOL-b001*.spi') {Write-host 'C'}
    elseif ($spks -like 'C_VOL-b002.spk' -and $spis -like 'C_VOL-b002*.spi') {Write-host 'D'}
    Else {Write-host "Whatever"}
    }

    But that still won't work if $spks or $spis can contain more than one file. The -like operator can't compare to multiple items in a collection, it can only compare single strings.

You must be logged in to reply to this topic.