关于 csv:将 PowerShell 输出发送到子目录

Sending PowerShell output to a sub directory

我有一个脚本正在生成多个 CSV 文件,其中时间戳和组名作为文件名的一部分。现在脚本将 CSV 文件输出到运行脚本的目录中。

我希望将 CSV 文件输出到子目录。每当我在 Export-Csv 命令中包含路径时,都会出现错误。

这是我所拥有的:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#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

我试过了:

1
Export-Csv \\\\Server\\Share\\File.csv ($strTimestamp +"_" + $group +".csv") -NoType
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
**********************
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
... | Export-Csv \\\\server\\share($strTimestamp +"_" + $group +".csv") -NoType

PowerShell 将 \\\\server\\share 视为一个参数,将 ("something" +".csv") 视为另一个参数(由于分组表达式)。第二个位置参数被传递给参数 -Delimiter,它需要一个字符。因此,您观察到的错误。

要从多个变量创建路径字符串,只需使用带有嵌套变量的字符串:

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