Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Below is my Powershell script to delete certain backup files on my servers and then display their contents. There is a lot of repetition. Is there a more efficient way of performing this task?

Remove-Item \\ietg\z$\Backups\daily\ETG*.bkf -Force -Confirm 
Remove-Item \\icnt\Z$\Backups\Daily\cnt*.bkf -Force -Confirm
Remove-Item \\igre\Z$\Backups\Daily\gre*.bkf -Force -Confirm
Remove-Item \\ihvd\Z$\Backups\Daily\hvd*.bkf -Force -Confirm
Remove-Item \\iklr\Z$\Backups\Daily\klr*.bkf -Force -Confirm

Get-ChildItem \\ietg\z$\Backups\daily\
Get-ChildItem \\icnt\Z$\Backups\Daily\
Get-ChildItem \\igre\Z$\Backups\Daily\
Get-ChildItem \\ivd\Z$\Backups\Daily\
Get-ChildItem \\iklr\Z$\Backups\Daily\
share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Another way to keep the data in the same file is to build the hash from two arrays...

$servers={"ietg", "icnt", "igre", "ihvd", "iklr"}
$filenames={"ETG", "cnt", "gre", "hvd", "klr"}
$Targets = @{}
if ($servers.Length -ne $filenames.Length) {
    Write-Error -Message "Array lengths do not match" `
                -Category InvalidData `
                -TargetObject $values
} else {
    for ($i = 0; $i -lt $keys.Length; $i++) {
        $Targets[$keys[$i]] = $values[$i]
    }
}
# the rest is from @sysadmin1138's post...
Foreach ($Targ in $Targets) {
    $Child=""\\"+$Targ.Server+"\Z$\Backups\daily\"+$Targ.filename
    $Server=$Child+"*.bkf"
    Remove-Item $Server -Force -Confirm
    Get-ChildItem $Child
}

Or, better yet, if the filenames are always part of the servername, you can build the data from one array, like this:

$servers={"ietg", "icnt", "igre", "ihvd", "iklr"}
$Targets = @{}
$servers | %{ $Targets.Add( $_, $_.Substring(1,3) ) }
# continue same as above starting from "Foreach ($Targ..."
share|improve this answer
add comment

One way is to have the script read in another file for the value pairs. It'll simplify the script, and make maintenance a lot easier.

Input file:

Server,filename
ietg,ETG
icnt,cnt
igre,gre
ihvd,hvd
iklr,klr

Then something like this (off the top of my head, do not run. Here be errors)

$Targets=Import-CSV -File "Input.csv"
Foreach ($Targ in $Targets) {
    $Child=""\\"+$Targ.Server+"\Z$\Backups\daily\"+$Targ.filename
    $Server=$Child+"*.bkf"
    Remove-Item $Server -Force -Confirm
    Get-ChildItem $Child
}

The string bulding can almost definitely be done easier. The key thing here is the CSV import and the loop.

If you'd rather do it all in a single file, you can build $Targets manually pretty simply:

$Targets[0]= @{"Server" = "ietg"; "filename" = "ETG"}
$Targets[1]= @{"Server" = "icnt"; "filename" = "cnt"}
$Targets[2]= @{"Server" = "igre"; "filename" = "gre"}
## and so on
share|improve this answer
    
I like this (+1), but it'd be nice to define the data in the same file as the script. –  Joel Coel Jul 13 '11 at 14:36
    
@JoelCool That's done easily enough. Will amend. –  sysadmin1138 Jul 13 '11 at 14:43
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.