Get-Child item is moving System32 Files when not called

Viewed 37

I have a script running quite often throughout a working day. As of lately it moved what appears to be all of my System32 file. I know the System32 files are running through the script as they are being organized and mapped 'correctly'.

However, I am never calling the system32 directory nor am I calling anything on the C drive. Why would the script turn to the directory that isnt being called or even a different drive path for that matter?

Since then has happen I have added a -Include .pdf so that it will only move. This change is bolded and italicized below . This should work correctly even if system32 is somehow directed again, correct?

##Hashtables

 

$srcRoot = (Get-ChildItem -Path 'D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Unsigned\*\*\Signed - Return to Court Admin' -Directory).FullName

$dstRoot = "D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Signed"

 

# map is defined

$map = @{

    '02' = '02 Anoka Court Admin Signed'

    '13' = 'PICK Court Admin Signed'

    '30' = 'PICK Court Admin Signed'

    '33' = 'PICK Court Admin Signed'

    '58' = 'PICK Court Admin Signed'

    '71' = '71 Sherburne Court Admin Signed'

    '82' = '82 Washington Court Admin Signed'

    '86' = '86 Wright Court Admin Signed'

    'CS' = 'Centralized Support Unit Signed'

}

 

foreach ($file in Get-ChildItem -Path $srcRoot -File ***-Include *pdf**** -Force -Recurse) {

    $key = $file.BaseName.Substring(0,2)

    if ($map.ContainsKey($key)) {

        $fileName = $file.Name

        $dstDir = Join-Path -Path $dstRoot -ChildPath $map[$key]

        if (-not (Test-Path -Path $dstDir)) {

            # Create destination directory below if needed for duplicates

        }

        if (Test-Path -Path (Join-Path -Path $dstDir -ChildPath $fileName)) {

            # Error message if file already exists at destination

            Write-Error -Message "File $fileName already exists at $dstDir"

            # go next here

            continue

        }

        # OK to move file

        Move-Item -Path $file.FullName -Destination $dstDir

        # go next here

        continue

    }

 

    # this is else for -not Contains()

    $dstDir = Join-Path -Path $dstRoot -ChildPath 'District Wide Signed - Error Naming Convention'

    # should test if `Errors` folder exists, but should exist

    if (-not (Test-Path -Path $dstDir)) {

        # Create destination directory below if needed for Errors

    }

    if (Test-Path -Path (Join-Path -Path $dstDir -ChildPath $fileName)) {

        # throw error here and if needed

        # go next after error

        continue

    }

    Move-Item -Path $file.FullName -Destination $dstDir

}
1 Answers

Abraham Zinala gave the crucial pointer in a comment:

  • Surprisingly, when you pass $null or '' (the empty string) to the (potentially positionally implied) -Path parameter of the Get-ChildItem cmdlet, it quietly targets the current location (directory) - see GitHub issue #17793

  • By contrast, the -LiteralPath parameter reports an error if you try to pass $null or ''

You can infer the following from that with respect to your problem:

  • $srcRoot is $null

    • This in turn implies that your wildcard pattern D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Unsigned\*\*\Signed - Return to Court Admin matches no existing directory.
  • The current location (directory) when your script runs happens to be C:\Windows\System32.

The immediate fix is to test $srcRoot immediately after assignment and abort the script if it is $null, i.e. if the source directory cannot be found:

$srcRootPattern = 'D:\Groups\Anoka\10thDistrict\eSignature and Workflow\Unsigned\*\*\Signed - Return to Court Admin'

$srcRoot = (Get-ChildItem -Path $srcRootPattern -Directory).FullName

if (-not $srcRoot) { throw "No directory matching '$srcRootPattern' found." }

As for using -Include *.pdf: it limits Get-ChildItem's output to files with a .pdf extension, so in that sense it would lessen your problem in that the wrongly targeted C:\Windows\System32 directory subtree probably contains no such files, which means that no move operations will be attempted.

Of course, it doesn't fix your problem, which requires ensuring that the correct source directory is identified and targeted.

Related