1

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Good point, but the need to place a command on the same line (with exceptions, as well as the ability to use explicit line continuation) does not apply to the language as a whole, but only its argument-mode parsing - see stackoverflow.com/a/49244654/45375
Thank you for your help, the bracket was indeed the problem. I have a new problem (of course) with the output. Now as it runs through the CSV it is appending the $username entries into one line, so the first created directory is "user1" the second is "user1 user2" and so on. I'm sorry I'm such a noob. ;)

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.