1

I have a CSV with 98 system names, each system has a file I need. They're all in the same folder on each machine, but the file name is unique.

I'd like to use copy-item to copy the file to a location on my local computer.

$AutoCADs = Import-csv "C:\DTS\BMC\AutoCAD - ANY VERSION_Members.csv"
$AutoPaths = "\\" + $AutoCADs + "\C$\DTS\BMC\Autodesk Audit\Information\*"
foreach-object{Copy-Item $AutoPaths -Destination "C:\DTS\BMC\Audit Results" -Force -Recurse}

When I run this, I get an error for each of the 98 machines. Each error looks like this:

Copy-Item : Cannot find path 'C:\Users\\@{D113978=Y118834}' because it does not exist. At \\CopyAuditFiles.ps1:3 char:16 + ... each-object{Copy-Item $AutoPaths -Destination "C:\DTS\BMC\Audit Resul ...

The @{D113978=Y118834} is confusing to me because each error has a similar string. D113978 is the first system name in the CSV.

The other system name (Y118834 in this example) changes for every error, with the first error having the second item in the csv, 2nd error having the 3rd item in the CSV etc.

So the script appears to be seeing each system name, but it's not doing what I want with the system name at all.

Can someone point out my flaw?

2 Answers 2

3

You have a bad csv. You should add a headername to each columns. Powershell takes the first row of each column as the headername of that column and that is why you get that confusing error. So add another row on top and use a headername like "Computername".

Also $AutoCADs is essentially a table object. And you are passing the entire table to create the dynamic network path.

So change your code to this:

$AutoCADs = Import-csv "C:\DTS\BMC\AutoCAD - ANY VERSION_Members.csv"

Foreach ($Row in $AutoCADs)
{
    $AutoPaths = "\\" + $($Row.Computername) + "\C$\DTS\BMC\Autodesk Audit\Information\*"
    Copy-Item $AutoPaths -Destination "C:\DTS\BMC\Audit Results" -Force -Recurse
}
Sign up to request clarification or add additional context in comments.

2 Comments

No need to edit the file. Just use -Header to pass whatever headers are needed.
After posting I'd been looking at some other posts and had added a header to the csv, but ($Row in $AutoCADs) was the piece I didn't know I needed. Thanks for the help.
3

Import-csv uses the first line in the file for the headers, so it is using D113978 as the header name not as an item.

I'm assuming your csv contains just the computer names:

D113978
Y118834
D978113
Y842118

If so it's not really a csv (as it has no headers), it is a text based list.

In this case I'd just use Get-Content instead, it will just use each line in the file as an item.

Then moving $AutoPaths = [..] inside the foreach so it is updated with each loop:

$computers = Get-Content "C:\DTS\BMC\AutoCAD - ANY VERSION_Members.csv"

foreach ($system in $computers){
    $AutoPaths = "\\$system\C$\DTS\BMC\Autodesk Audit\Information\*"
    Copy-Item $AutoPaths -Destination "C:\DTS\BMC\Audit Results" -Force -Recurse
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.