关于 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) {
    GetQADGroupMember $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 OneSourceUsers20180207_1131.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 20180207T11:31:51
**********************
Windows PowerShell transcript end
End time: 20180207113151
**********************

  • 有关不起作用的代码的帮助:显示不起作用的代码及其引发的错误。编辑您的问题以提供此信息。不要在评论中发布它。
  • 您尝试过什么,您尝试过的如何失败了?理想情况下,您应该提供您尝试过的最小可重现示例,并包括有关它如何失败的具体信息,以及错误消息和/或错误输出。 Stack Overflow 不是代码编写服务;最好的问题是那些提供有用信息的问题,以便回答的人可以指导您设计自己的正确答案。请参阅如何提出一个好问题。
  • 向我们展示产生错误的路径。告诉我们错误是什么。否则,你让我们猜测。


当你运行类似

的东西时

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

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

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

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

来源:https://www.codenong.com/48666623/

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_9381.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?