1 year ago

#75552

test-img

ziico

A positional parameter cannot be found in powershell

I have this basic script that import a csv file into sql server database but I'm not sure why I'm getting this error.

Import-Csv : A positional parameter cannot be found that accepts argument '+'.
At C:\scripts\LitHold-OneDrive\Daily0365DatabaseImport.ps1:9 char:12
+ $csvfile = import-csv -delimiter "," -path "C:\scripts\LitHold-OneDri ...
+            ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Import-Csv], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Microsoft.PowerShell.Commands.ImportCsvCommand

If I try hard coding the exact date like this then it's working

$csvfile = import-csv -delimiter "," -path "C:\scripts\LitHold-OneDrive\output\Return\01/20/22-Return.csv"

But I don't want to hard coded the date

$CurDate = Get-Date -Format "yyyy-MM-dd"
$s =  New-Object Microsoft.SqlServer.Management.Smo.Server "Server Name"
$db = $s.Databases.Item("LitHold")
$csvfile = import-csv -delimiter "," -path "C:\scripts\LitHold-OneDrive\output\Return\"  + $CurDate + "-Return.csv"
$csvfile |foreach-object{
    $query = "insert into DailyReport VALUES ('$($_.MIN)','$($_.MID)','$($_.UPN)','$($_.Change)','$($_.Type)','$($_.'Hold Value')','$($_.OneDrive)','$($_.Mailbox)','$($_.Created)','$($_.Modified)','$($_.'Mult GMID')','$($_.Account)','$($_.'Exist OD')')"
    $result = $db.ExecuteWithResults($query)
}


# Show output
$result.Tables[0]


sql-server

powershell

csv

powershell-2.0

0 Answers

Your Answer

Accepted video resources