MarkyMark
MarkyMark

Reputation: 13

Powershell Script reading file into array

I have a script I'm working on. I want it to read in a column named ComputerName and one named UserName.

My CSV file looks like this:

ComputerName | Username
computer01   | user1
computer02   | user2

The Pipes are representing cells in excel.

Here's my script:

$computerName = @()
$userName = @()

Import-Csv C:\test\script\Computername_username_test.csv -Delimiter "|" |`
    ForEach-Object
        {
            $computerName += $_.ComputerName
            $userName += $_.UserName
        }

$destination = New-Item -ItemType Directory -Path C:\test\$userName\dictionary_Files\ -force
$fileList = Get-WmiObject -Class CIM_DataFile -Filter "Drive='C:' And Extension='dic'" -Computername $computerName

foreach ($file in $fileList)
{
    $drive, $path = $file.Name.Split('\',2)
    $drive = $drive -replace ':','$'
    $remoteFile = "\\$computerName\$drive\$path"
    Write-Verbose "Copy $remoteFile to $destination"
    Copy-Item $remoteFile -Destination $destination -Confirm
}

My goal is to search the C drive of the remote computer for all files with the .dic extension and copy them to a location inside a folder that is named the same as their username from the excel sheet.

When I run this I'm getting the following:

PS C:\Test\Script> C:\Test\Script\DicFiles03_importCSV.ps1
cmdlet ForEach-Object at command pipeline position 2
Supply values for the following parameters:
Process[0]: 

            $computerName += $_.ComputerName
            $userName += $_.UserName

Get-WmiObject : Cannot validate argument on parameter 'ComputerName'. The argument is null, empty, or an element of the argument 
collection contains a null value. Supply a collection that does not contain any null values and then try the command again.
At C:\Test\Script\DicFiles03_importCSV.ps1:13 char:102
+ ...  -Filter "Drive='C:' And Extension='dic'" -Computername $computerName
+                                                             ~~~~~~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Get-WmiObject], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetWmiObjectCommand

Thank you for your help.

Upvotes: 1

Views: 909

Answers (1)

rerun
rerun

Reputation: 25505

I'm think its because you have your { after the foreach-object on the next line powershell is a scripting language so its particular about line endings.

Upvotes: 1

Related Questions