1
votes

I have a script that is generating multiple CSV files with the time stamp and group name as part of the file name. Right now the script outputs the CSV files into the directory the script is run from.

I would like to have the CSV files output to a sub directory. Whenever I include a path in my Export-Csv command, I get an error.

Here is what I have:

#This array lists the names of the AD groups that the script will query to enumerate membership.
$aGroups = @("Group1", "Group2")

#Create a log file to verify that the script did not have any errors.

$strTimestamp = [string](Get-Date -Format "yyyy-MM-dd_hh-mm")
Start-Transcript ADGroupScriptLog-$strTimestamp.txt 
Write "Enumerating group membership. This may take some time..."

#Loop through each group in the array and output to CSV each member in the group.

foreach ($group in $aGroups) {
    Get-QADGroupMember $group -Indirect -Type 'user' -ShowProgress -ProgressThreshold 0 -Sizelimit '0' |
        Select-Object Name, SAMAccountName, givenName, sn, title, manager, Description |
        Export-Csv ($strTimestamp + "_" + $group + ".csv") -NoType
}

Write "Execution Complete"
Get-Date -format s
Stop-Transcript

I have tried:

Export-Csv \\Server\Share\File.csv ($strTimestamp + "_" + $group + ".csv") -NoType
**********************
Windows PowerShell transcript start
Start time: 20180207113151
Username  :  
Machine   :  (Microsoft Windows NT 6.1.7601 Service Pack 1) 
**********************
Transcript started, output file is OneSourceUsers-2018-02-07_11-31.txt Enumerating group membership. This may take some time...

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_OneSrce_BI_Browser.csv" to type "System.Char". Error:
"String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_OneSrce_BI_Admin.csv" to type "System.Char". Error:
"String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_OneSource MSSQL POC.csv" to type "System.Char".
Error: "String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_One Source Reporting Alert.csv" to type "System.Char".
Error: "String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_One Source Loads.csv" to type "System.Char". Error:
"String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_One Sales Team.csv" to type "System.Char". Error:
"String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Export-Csv : Cannot bind parameter 'Delimiter'. Cannot convert value
"2018-02-07_11-31_OneSrce_BI_Contributor.csv" to type "System.Char".
Error: "String must be exactly one character long."
At E:\OneSource\get_OneSourceUsers.ps1:19 char:215
+ ... neSource\Output($strTimestamp + "_" + $group + ".csv") -NoType
+                    ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Export-Csv], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.PowerShell.Commands.ExportCsvCommand

Execution Complete 2018-02-07T11:31:51
**********************
Windows PowerShell transcript end
End time: 20180207113151
**********************
1
For help with code that doesn't work: show the code that doesn't work along with the error it throws. Edit your question to provide this information. Do not post it in comments.Ansgar Wiechers
What have you tried, and how has what you've tried failed? Ideally, you should provide a minimal reproducible example of what you've tried, and include specific information on how it failed, with error messages and/or erroneous output. Stack Overflow is not a code-writing service; the best questions are those which provide useful information so that those who answer can guide you to devising your own correct answer. See How to Ask a Good Question.Jeff Zeitlin
Show us the path that produces the error. Tell us what the error is. Otherwise, you are making us guess.Walter Mitty

1 Answers

1
votes

When you run something like

... | Export-Csv \\server\share($strTimestamp + "_" + $group + ".csv") -NoType

PowerShell sees \\server\share as one argument, and ("something" + ".csv") as another argument (due to the grouping expression). The second positional parameter is passed to the parameter -Delimiter, which expects a single character. Hence the error you observed.

To create a path string from several variables just use a string with nested variables:

... | Export-Csv "\\server\share\${strTimestamp}_${group}.csv" -NoType