Link to home
Start Free TrialLog in
Avatar of cawasaki
cawasaki

asked on

change script to get csv file on the prompt

hello,

actually for all my script, on the code i import a csv file like that:

$csvFile = "D:\test.csv"
$create = Import-CSV $csvFile

or
import-csv "test.csv" | foreach .....

Open in new window


i need now to execute directly the script from powershell console and give the csv file name like this:

script.ps1 xxx.csv

can you help plz?
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Declare a parameter, like this.
param (
    [Parameter(Mandatory = $true)]
    [String]$csvFile
)

$create = Import-CSV $csvFile
# or
import-csv "test.csv" | foreach .....

Open in new window

Avatar of cawasaki
cawasaki

ASKER

hello Chris,

the need is the csv file name can change, so the exact need is

script.ps1 xxx.csv    the name of csv file can change

so your code:
or
import-csv "test.csv" | foreach .....  is not good???
ASKER CERTIFIED SOLUTION
Avatar of Chris Dent
Chris Dent
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
OK I TEST NOW
error:

PS D:\script\.\script.ps1 test.csv
param : The term 'param' is not recognized as the name of a cmdlet, function, script file, or operable program. Check
the spelling of the name, or if a path was included, verify that the path is correct and try again.
At D:\script\script.ps1:3 char:1
+ param (
+ ~~~~~
    + CategoryInfo          : ObjectNotFound: (param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

Import-Csv : Cannot validate argument on parameter 'Path'. The argument is null or empty. Provide an argument that is
not null or empty, and then try the command again.
At D:\script\script.ps1:9 char:12
+ Import-Csv $csvFile | %{
+            ~~~~~~~~
    + CategoryInfo          : InvalidData: (:) [Import-Csv], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.ImportCsvCommand

Open in new window

You'll get that error if you write anything else before param (except comments and attributes).

For example, this will throw exactly that error:
Get-Process -Id $PID

param ( )

Open in new window

The param block has to appear before the body of your script.
yes its work i have the import-module before :)
You might consider using Requires instead of Import-Module. This has a few advantages:

1. You get to put the module you need right at the top of the script.
2. You don't have to worry about handling errors if the module you need isn't there.

The Requires statement must be the first line of your script (before param). Like this:
#Requires -Module SomeModule

param (
    [String]$CsvFile
)

Open in new window

When writing a requires statement, ensure you do not add a space between # and "Requires". As this is, technically, a comment it does not violate my earlier note about comments and attributes only before param.

You can read more about Requires with Get-Help:
Get-Help about_requires

Open in new window