In Way Over My Head

Ok, I’ll lay it out the best I can. I’ve been working with PS for about 5 weeks, got a great job giving me a chance to learn. I get handed a project that needs me to copy files that match a csv from a DB into a SP library. A team member points me in the direction I should go…so I go. I’ve pulled and spliced more pieces of code from online, as I’m still in the process of learning piping atm…so forgive me on how this looks. Its a 1 time move so it doesn’t need to be pretty (as I was told).

My issues is that when the code runs its comparing the files, but appending the file name at the front of the file and for all files there after. There are 48K files in the directory and from the db the CSV has exactly 307. What i’m looking to do is match the files from the csv that are in the directory and then copy them over to the library. Along with all metadata for each file. There have been some special request such as the file name would be great if it could have the Claim_Number in the file name at the end.

I believe my issue is between lines 130-160, but to be on the level…I don’t know what some of these functions are doing calling the same variable multiple times within the same script. I thought that wasn’t something that you wanted to do.

Again this might be rough and I might not be explaining it very well…so my apologizes for that. Any tips/direction/duh obvious/ items that could be seen would be greatly appreciated.

The code is here: CopyFiletoSP · GitHub

This is a fairly large script that is doing a lot of logic based changes. The best thing you can do is add verbose logging and make sure you are passing the correct parameters. Right now, you are blind to what your process is actually doing. Which IF is being executed? Did the file qualify for the ELSE? Write-Verbose is your friend!!

Take a look at this mock function and run it:

function Test-It {
    [CmdletBinding()]
    param (
        $MyParameter1,
        $MyParameter2
    )

    if ($MyParameter1) {
        Write-Verbose ("MyParameter1 is not null.  Setting pen color to {0}." -f $MyParameter1)
        $result = "The pen is {0}" -f $MyParameter1
    }
    else {
        Write-Verbose ("MyParameter1 is null")
        If ($MyParameter2) {
            Write-Verbose ("MyParameter2 is not null. Setting pen color to {0}." -f $MyParameter2)
            $result = "The pen is {0}" -f $MyParameter1
        }
        else {
            Write-Verbose ("MyParameter2 is null.  Both parameters are null, setting pen color to yellow.")
            $result = "The pen is yellow"
        }
    }
    $result
}

#hash table defined as a splat
$myParams = @{
    MyParameter1 = "Blue"
    MyParameter2 = "Red"
}

Test-It @myParams

What color is the pen? If I passed two parameters, how is the pen blue? If you have a small function like this one, you can read the logic and probably figure it out. However, your script is doing a lot. Now add the -Verbose switch:

Test-It @myParams -Verbose

Now we get output to help us follow the logic and understand WHY and WHERE we are executing in the function. Think of it like a log and if someone has no idea what you script does and they add the -Verbose switch, they would get a walk-through of the logic in simple logical steps.

Try using splatting to pass the params which is clean and forces you to use named parameters. Instead of this:

Copy-FilestoSP -LocalPath “P:\KC  Cargo  Claims\Claim Scans" -LocalFilter "*.*" -SiteUrl "http://mygateway.miq.com/libraries/CargoClaims" -Library "Claims Document Library" -LibraryStartFolder “” -IncludeSubFolders -FlattenStructure 

Do this:

$copyParams = @{
    LocalPath          = "P:\KC  Cargo  Claims\Claim Scans"
    LocalFilter        = "*.*" 
    SiteUrl            = "http://mygateway.miq.com/libraries/CargoClaims" 
    Library            = "Claims Document Library" 
    LibraryStartFolder =  "" 
    IncludeSubFolders  = $true
    FlattenStructure   = $true
}

#Note that the parameters are passed with a @ and not $
Copy-FilestoSP @copyParams