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:
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?
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 .....
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?
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???
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
OK I TEST NOW
ASKER
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
You'll get that error if you write anything else before param (except comments and attributes).
For example, this will throw exactly that error:
For example, this will throw exactly that error:
Get-Process -Id $PID
param ( )
The param block has to appear before the body of your script.
ASKER
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:
You can read more about Requires with Get-Help:
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
)
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