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”) #Loop through each group in the array and output to CSV each member in the group. foreach ($group in $aGroups) { Write“Execution Complete” |
我试过了:
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 Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Export-Csv : Cannot bind parameter ‘Delimiter’. Cannot convert value Execution Complete 2018–02–07T11:31:51 |
- 有关不起作用的代码的帮助:显示不起作用的代码及其引发的错误。编辑您的问题以提供此信息。不要在评论中发布它。
- 您尝试过什么,您尝试过的如何失败了?理想情况下,您应该提供您尝试过的最小可重现示例,并包括有关它如何失败的具体信息,以及错误消息和/或错误输出。 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/