简体   繁体   中英

Powershell: get-childitem folder from csv

I'm trying to get the results of a Get-ChildItem to filter a list of these folders from a CSV

I have a number of homefolders that are named the same as the users SamAccountName (as is usual)

all the home folders are stored on my share: \\DomainName\\Users

My CSV contains the users which are names the same as the home folders Example:

SamAccountName
123456
123457
123458

The part of my script that I want to work goes like this:

$SAM = import-csv "C:\Scripts\HomeDrive.csv"
$HomeDriveSharePath = "\\Domain\users"
$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.Name -Match $SAM.samaccountname -AND $_.PsIsContainer -eq $true}

With my CSV this returns no results (although the folder for the example users in the CSV definitely exists)

If I run this, it returns all of the homefolders (as it should)

Get-ChildItem $HomeDriveSharePath

If I run this:

$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.Name -Match "123456" -AND $_.PsIsContainer -eq $true}

then I do get the corresponding folder displayed (proving the line kind of works and the folder does exist)

This needs to be run for hundreds of users with regards to a migration, Otherwise I'd just sit here doing it manually :-(

First time posting, apologies if this isn't formatted or explained quite right... a thorough search of this site and Google hasn't brought back the exact scenario I seem to have.

I'm quite stumped... your help appreciated,

Thanks

$SAM = import-csv "C:\Scripts\HomeDrive.csv"
$HomeDriveSharePath = "\\Domain\users"
$HomeDirectories = Get-ChildItem $HomeDriveSharePath | Where {$_.PsIsContainer}

$HomeDirectories = $(foreach ($name in $sam) {
    $HomeDirectories | ? {$_.name -match $name.samaccountname.trim()}
})

or

$HomeDirectories = $sam | % { dir $HomeDriveSharePath -Filter $_.samaccountname.trim() -Directory -Name }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM