Home > Enterprise >  Check if a folder exists or not in the NAS from a CSV of users
Check if a folder exists or not in the NAS from a CSV of users

Time:02-05

I have to make a script that checks if a file exists in our NAS from a CSV file which contains a list a users. I have made this small script but the result is kind of broken, I don't understand why the typo is "{user=USER10}" and not just "USER10"

This is the result from the script:

This is my CSV of users

The script:

$sInputFile = "D:\Script\ADD2000.csv"
$sColumnName = "User"
$tblUsers = Import-Csv $sInputFile
$FolderPath = "\\somepath.tech.intra.workplace.fr\users$\ADD"
$Retrieve = Test-Path $Path

foreach($user in $tblUsers){
    $Path = $FolderPath   "\"   $($user)   ".ADD2000"
    if (!(Test-Path $Path)) {
        write-host "$path does not exist"

} Else {write-host "it exist"}
}

CodePudding user response:

Looking at the image of your input CSV, I can see it has a header user. When importing that csv, all items in that column are objects with one property called user.
To get the value of that, you need to expand it like $user.user.

In your case, since you have only one column in the file, it would even be simpler if you expand the values straight away when importing, so you can iterate over a string array of usernames.

$sInputFile  = 'X:\Folder\AllUsers.csv'                         # full path to the input csv file
$tblUsers    = (Import-Csv $sInputFile).user                    # just get the values of all items for column 'user' as striong array
$FolderPath  = '\\somepath.tech.intra.workplace.fr\users$\ADD'

foreach($userName in $tblUsers) {
    $path = Join-Path -Path $FolderPath -ChildPath "$username.ADD2000"
    if (Test-Path $Path -PathType Container) { 
        Write-Host "$path exists"
    }
    else {
        Write-Warning "$path could not be found"
    }
}

CodePudding user response:

You are importing a CSV with a column "User".

You need to return the value from that column e.g. $($user.User) not just $($user).

$user contains the entire row $user.User contains just the value from that column.

  •  Tags:  
  • Related