Getting an error when running the function Get-DirHash. Please help.

Hi All,

I am getting the below error when running the script attached. Can you help me fix this?

Error -

Get-DirHash : Cannot validate argument on parameter ‘Path’. Cannot bind argument to parameter ‘Path’ because it is an empty string.
At line:63 char:20

  •       if((Get-DirHash $CurrItem[$i]).hash -ne (Get-DirHash $Pre ...
    
  •                       ~~~~~~~~~~~~~
    
  • CategoryInfo : InvalidData: (:slight_smile: [Get-DirHash], ParameterBindingValidationException
  • FullyQualifiedErrorId : ParameterArgumentValidationError,Get-DirHash

 

Code -

$DebugPreference = "Continue"

$NewPath = “D:\DRMData\DRM-GL-EXPORTS\Exports\NewFiles”
$CurrPath = “D:\DRMData\DRM-GL-EXPORTS\Exports\Current”
$PrevPath = “D:\DRMData\DRM-GL-EXPORTS\Exports\Previous”

#Part 2: Compare files in current and previous folders and move the new files to NewFiles folder

Function Get-DirHash {
[Cmdletbinding()]
Param(
[Parameter(Mandatory=$true)]
[ValidateScript({
if(Test-Path -Path $)
{
return $true
}
else
{
throw "$($
) is not a valid path."
}
})]
[string]$Path
)
$temp=[System.IO.Path]::GetTempFileName()
gci -File -Recurse $Path | Get-FileHash | select -ExpandProperty Hash | Out-File $temp -NoNewline
$hash=Get-FileHash $temp
Remove-Item $temp
$hash.Path=$Path
return $hash
}

$CurrItem = get-childitem “D:\DRMData\DRM-GL-EXPORTS\Exports\Current*Current”
$PrevItem = get-childitem “D:\DRMData\DRM-GL-EXPORTS\Exports\Previous*Current”
$BaseItem = get-childitem “D:\DRMData\DRM-GL-EXPORTS\Exports\Current*Base”
$AuditItem = get-childitem “D:\DRMData\DRM-GL-EXPORTS\Exports\Current*Trail”

$c = $CurrItem.count
$p = $PrevItem.count
$WeekDay = (get-date).DayOfWeek

for ($i= 0; $i -le $c; $i++)
{
for ($k= 0; $k -le $p; $k++)
{
$Currname = $CurrItem[$i].name -replace"[0-9]"
$Prevname = $PrevItem[$k].name -replace"[0-9]"

	 <span class="k">if</span><span class="p">(</span><span class="nv">$Currname</span> <span class="o">-eq</span> <span class="nv">$Prevname</span><span class="p">)</span>
	<span class="p">{</span>
		<span class="c">#write-host $CurrFolder[$i]</span>
		<span class="k">if</span><span class="p">((</span><span class="nb">Get-DirHash</span> <span class="nv">$CurrItem</span><span class="p">[</span><span class="nv">$i</span><span class="p">]).</span><span class="n">hash</span> <span class="o">-ne</span> <span class="p">(</span><span class="nb">Get-DirHash</span> <span class="nv">$PrevItem</span><span class="p">[</span><span class="nv">$i</span><span class="p">]).</span><span class="n">hash</span><span class="p">)</span>
		<span class="p">{</span>
		<span class="nb">Copy-Item</span> <span class="n">-Recurse</span> <span class="n">-Path</span> <span class="nv">$CurrItem</span><span class="p">[</span><span class="nv">$i</span><span class="p">]</span> <span class="n">-Destination</span> <span class="s2">"$NewPath"</span> <span class="n">-force</span>
		<span class="nb">Copy-Item</span> <span class="n">-Recurse</span> <span class="n">-Path</span> <span class="nv">$BaseItem</span><span class="p">[</span><span class="nv">$i</span><span class="p">]</span> <span class="n">-Destination</span> <span class="s2">"$NewPath"</span> <span class="n">-force</span>
		<span class="c">If($WeekDay -eq "Friday")</span>
		<span class="c">{</span>
		<span class="nb">Copy-Item</span> <span class="n">-Recurse</span> <span class="n">-Path</span> <span class="nv">$AuditItem</span><span class="p">[</span><span class="nv">$i</span><span class="p">]</span> <span class="n">-Destination</span> <span class="s2">"$NewPath"</span> <span class="n">-force</span>
		<span class="c">}</span>
		<span class="p">}</span>
	<span class="p">}</span>
<span class="p">}</span>
<span class="p">}</span></pre>

Since the error says as the passed value is an empty string, uncomment(L51) that write-host and see what’s in that variable.

It worked now, have to change for loop as below as Array count is more than the total number of directories.

for ($i= 0; $i -lt $c; $i++)