0

This is my first of a few scripts that will use a hashtable. the following scripts will work with info from each previous hashtable. My conundrum is that when I run the script using a command line that points to it, the hashtable variables do not get updated, yet when I run the script as a whole, copy and pate it all into PS, the hashtable variable is exactly as it should be. I then add/remove a file or two from the directory, paste the full script again and the results are perfect again. I do run PS as an Administrator to process these scripts.

I've learned to make sure to run these all from the same PS tab. I've also concluded it really doesn't matter what dir shows in the PS tab as I'm pointing to the location of the script and pointing to the working dir all in the same line.

The filePath variable points to the folder where with the files I'm working with. The command line looks like this: C:\Users\Me\Powershell\DocumentCount.ps1 -filePath $filePath

Although the script is working perfectly, I'm showing it just so you see what I'm working with.

    #Clear Variables at start:
    if (Test-Path variable:Documents) {clear-variable -Name Documents}
    if (Test-Path variable:files) {Clear-Variable -Name files}

    # Get all files in the specified path
    $files = Get-ChildItem -Path $filePath | Where-Object { $_.PSIsContainer -eq $false }

    # Initialize a hashtable to store values and counts for each section[1]
    $Documents = @{}

    # Loop through each file
    foreach ($file in $files) {
        # Split the file name based on underscores
        $sect = $file.BaseName -split '_'
        $DocName = (($sect[0],$sect[1]) -join '_')

        # Increment the count in the hashtable
        if ($Documents.ContainsKey($DocName)) {
            $Documents[$DocName]++
        } else {
            $Documents[$DocName] = 1
        }

    }

    # Display the values and counts for each section[1]
    foreach ($sectionKey in $Documents.Keys) {
        Write-Host "$sectionKey Count: $($Documents[$sectionKey])"
    }

Again, I simply can not figure out why this script is not processing correctly when run as a command line.

Thank you.

3
  • Not yet sure of your problem, but a couple of notes on your code. The first two lines don't really seem to serve a purpose, writing over a variable's content is sufficient. You shouldn't need the "Where-Object", instead, take a look at the -File parameter of Get-Children. Commented Jan 22, 2024 at 11:59
  • I believe gpburdell's answer is correct, you need to define filePath as a parameter. But here is a tip while writing scripts in a language you are not yet familiar with, take each step one at a time and test as you go. In your case, the first step is getting the $filePath, so create a script with only one line: Write-Host $filePath. Open a fresh copy of PowerShell and run the script to see what happens. Better yet, use VSCode and follow these instructions to insure each execution is on a fresh copy of PowerShell. Commented Jan 22, 2024 at 12:20
  • Good morning all. Thank you for your comments/answers. I need to correct myself on this. "running from command" actually meant "running from PS command". I've built the scripts as individual files to run in sequence. I've learned that I need to assign the working variables in the current directory that shows in PS. The only way I can do this is to copy/paste the individual lines. In the script. I've tried using get-location, no go. A script to set/show $Dir as get-location works, but when I type in $Dir, it doesn't exist in the working dir. This is the cause of my troubles. Always, Big Thanks Commented Jan 24, 2024 at 15:10

1 Answer 1

0

One issue may be that you're using a variable named $filePath in your script but also trying to pass in a variable named $filePath. When you execute a script, it will inherit all variables from the parent shell. Also, your script needs to either define the parameters that are passed in or you need to use the args[] array. I generally don't fool with named parameters, I just use args because it's easier.

So, your command line would look like: C:\Users\Me\Powershell\DocumentCount.ps1 $filePath

You reference the first argument in your script as $args[0] so that would look like: $files = Get-ChildItem -Path $args[0] | ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.