I have a problem:
I need the user to input the Path (of files) that will be used later but I get this error:
My problem is that the folder has files in it but I just need the Path of the entered folder:
Input should be:
C:\Users\USER1\Desktop\test\files
And I'd need that Path to be put in the variable $Source_UIP
My Powershell error is this:
Set-Location : Cannot convert 'System.Object[]' to the type 'System.String' required by parameter >'Path'. Specified method is not supported.
At Z:\PS_Hash_Compare.ps1:14 char:4
cd $Source_UIP
~~~~~~~~~~~
CategoryInfo : InvalidArgument: (:) [Set-Location], ParameterBindingException
FullyQualifiedErrorId : CannotConvertArgument,Microsoft.PowerShell.Commands.SetLocationCommand
This is my code:
$Source_UIP = Get-ChildItem (Read-Host -Prompt 'Get path of the source files')
cd $Source_UIP
$Hash_Src = Get-Filehash -Algorithm SHA256 (Get-ChildItem -Recurse | Get-Item | where {!$_.PsIsContainer}
and this would be the final use:
$Compare = Compare-Object -ReferenceObject $Hash_Src -DifferenceObject $Hash_Extr -Property hash -IncludeEqual -PassThru | Format-List
any Ideas?
EDIT: many thanks to @Santiago Squarzon for the help! This is my working script. It works also without the Validation step. This is usefull if I enter a "wrong" Path with e.g. a *.txt (= File) instead of a path
$Date_Time = Get-Date -Format "yyyyMMdd_HH_mm"
$ErrorActionPreference = 'Stop'
[ValidateScript({
if(-not (Test-Path -Path $_ -PathType Container))
{
throw 'Invalid Path'
}
$true
})]
$Source_UIP = Read-Host -Prompt 'Get path of the source files'
[ValidateScript({
if(-not (Test-Path -Path $_ -PathType Container))
{
throw 'Invalid Path'
}
$true
})]
$Extracetd_UIP = Read-Host -Prompt 'Get path of the Destination files'
#Compare
$Hash_Src = Get-ChildItem $Source_UIP -Recurse -File | Get-FileHash -Algorithm SHA256
$Hash_Extr = Get-Filehash -Algorithm SHA256 (Get-ChildItem -Recurse | Get-Item | where {!$_.PsIsContainer})
$Hash_Extr = Get-ChildItem $Extracetd_UIP -Recurse -File | Get-FileHash -Algorithm SHA256
$Compare = Compare-Object -ReferenceObject $Hash_Src -DifferenceObject $Hash_Extr -Property hash -IncludeEqual -PassThru | Format-List
$Compare | Out-File -FilePath "C:\Users\USER1\Desktop\test\Destination\Hash_Result-$Date_Time.txt"
Invoke-Item -Path "C:\Users\USER1\Desktop\test\Destination\Hash_Result-$Date_Time.txt"
CodePudding user response:
The right approach, in my opinion, would be first to check if the user's input is valid, meaning that the path exists and the path is a folder.
For that, you can use many approaches, one I like to use is the ValidateScript validation attribute
.
One thing to note, see that I'm not using Set-Location
but passing the path (user input) directly to Get-ChildItem
once we confirmed that the path is a folder, it's up to you if you wish to do it this way or with cd
.
$ErrorActionPreference = 'Stop'
[ValidateScript({
if(-not (Test-Path -Path $_ -PathType Container))
{
throw 'Invalid Path'
}
$true
})]
$Source_UIP = Read-Host -Prompt 'Get path of the source files'
# where {!$_.PsIsContainer} => Can be replaced with -File
$Hash_Src = Get-ChildItem $Source_UIP -Recurse -File | Get-FileHash -Algorithm SHA256