草庐IT

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

codeneng 2023-03-28 原文

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
**********************

  • 有关不起作用的代码的帮助:显示不起作用的代码及其引发的错误。编辑您的问题以提供此信息。不要在评论中发布它。
  • 您尝试过什么,您尝试过的如何失败了?理想情况下,您应该提供您尝试过的最小可重现示例,并包括有关它如何失败的具体信息,以及错误消息和/或错误输出。 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

有关关于 csv:将 PowerShell 输出发送到子目录的更多相关文章

  1. ruby - 用逗号、双引号和编码解析 csv - 2

    我正在使用ruby​​1.9解析以下带有MacRoman字符的csv文件#encoding:ISO-8859-1#csv_parse.csvName,main-dialogue"Marceu","Giveittohimóhe,hiswife."我做了以下解析。require'csv'input_string=File.read("../csv_parse.rb").force_encoding("ISO-8859-1").encode("UTF-8")#=>"Name,main-dialogue\r\n\"Marceu\",\"Giveittohim\x97he,hiswife.\"\

  2. ruby - 我可以使用 Ruby 从 CSV 中删除列吗? - 2

    查看Ruby的CSV库的文档,我非常确定这是可能且简单的。我只需要使用Ruby删除CSV文件的前三列,但我没有成功运行它。 最佳答案 csv_table=CSV.read(file_path_in,:headers=>true)csv_table.delete("header_name")csv_table.to_csv#=>ThenewCSVinstringformat检查CSV::Table文档:http://ruby-doc.org/stdlib-1.9.2/libdoc/csv/rdoc/CSV/Table.html

  3. ruby-on-rails - active_admin 目录中的常量警告重新声明 - 2

    我正在使用active_admin,我在Rails3应用程序的应用程序中有一个目录管理,其中包含模型和页面的声明。时不时地我也有一个类,当那个类有一个常量时,就像这样:classFooBAR="bar"end然后,我在每个必须在我的Rails应用程序中重新加载一些代码的请求中收到此警告:/Users/pupeno/helloworld/app/admin/billing.rb:12:warning:alreadyinitializedconstantBAR知道发生了什么以及如何避免这些警告吗? 最佳答案 在纯Ruby中:classA

  4. ruby-on-rails - 如果 Object::try 被发送到一个 nil 对象,为什么它会起作用? - 2

    如果您尝试在Ruby中的nil对象上调用方法,则会出现NoMethodError异常并显示消息:"undefinedmethod‘...’fornil:NilClass"然而,有一个tryRails中的方法,如果它被发送到一个nil对象,它只返回nil:require'rubygems'require'active_support/all'nil.try(:nonexisting_method)#noNoMethodErrorexceptionanymore那么try如何在内部工作以防止该异常? 最佳答案 像Ruby中的所有其他对象

  5. ruby CSV : How can I read a tab-delimited file? - 2

    CSV.open(name,"r").eachdo|row|putsrowend我得到以下错误:CSV::MalformedCSVErrorUnquotedfieldsdonotallow\ror\n文件名是一个.txt制表符分隔文件。我是专门做的。我有一个.csv文件,我转到excel,并将文件保存为.txt制表符分隔的文件。所以它是制表符分隔的。CSV.open不应该能够读取制表符分隔的文件吗? 最佳答案 尝试像这样指定字段分隔符:CSV.open("name","r",{:col_sep=>"\t"}).eachdo|row|

  6. ruby-on-rails - 如何在 Gem 中获取 Rails 应用程序的根目录 - 2

    是否可以在应用程序中包含的gem代码中知道应用程序的Rails文件系统根目录?这是gem来源的示例:moduleMyGemdefself.included(base)putsRails.root#returnnilendendActionController::Base.send:include,MyGem谢谢,抱歉我的英语不好 最佳答案 我发现解决类似问题的解决方案是使用railtie初始化程序包含我的模块。所以,在你的/lib/mygem/railtie.rbmoduleMyGemclassRailtie使用此代码,您的模块将在

  7. ruby - 如何使用 Ruby 将 CSV 文件读入 HTML 表格? - 2

    我正在尝试将一个简单的CSV文件读入HTML表格以在浏览器中显示,但我遇到了麻烦。这就是我正在尝试的:Controller:defshow@csv=CSV.open("file.csv",:headers=>true)end查看:输出:NameStartDateEndDateQuantityPostalCode基本上我只获取标题,而不会读取和呈现CSV正文。 最佳答案 这最终成为最终解决方案:Controller:defshow#OpenaCSVfile,andthenreaditintoaCSV::Tableobjectforda

  8. ruby-on-rails - 没有这样的文件或目录 - 用 Mini Magick 识别 - 2

    在我让另一个人重做我的前端UI之前,我的Rails应用程序运行平稳。我已经尝试解决此错误3天了。这是错误:Nosuchfileordirectory-identifyExtractedsource(aroundline#59):575859606162@post=Post.find(params[:id])authorize@postif@post.update_attributes(post_params)flash[:notice]="Postwasupdated."redirect_to[@topic,@post]else{"utf8"=>"✓","_method"=>"patc

  9. ruby - 未定义的方法 auto_upgrade!将 Sinatra/DataMapper 应用程序推送到 Heroku 时 - 2

    有谁知道在Heroku的Bamboo堆栈上启动并运行使用DataMapper的Sinatra应用程序所需的魔法咒语?Bamboo堆栈不包含任何预安装的系统gem,无论我尝试使用何种gem组合,我都会不断收到此错误:undefinedmethod`auto_upgrade!'forDataMapper:Module(NoMethodError)这是我的.gems文件中的内容:sinatrapgdatamapperdo_postgresdm-postgres-adapter这些是我将应用程序推送到Heroku时安装的依赖项:----->Herokureceivingpush----->Si

  10. ruby-on-rails - 使用 RSpec 测试 CSV.generate - 2

    我在Rails3.1项目中有以下助手-我只是想知道是否有办法测试CSV.generate调用。我很想说我知道如何去做,但事实是我什至不知道从哪里开始。任何想法表示赞赏。require'csv'moduleAdmin::PurchasesHelperdefcsv_purchase_listcolumns=['course','amount','first_name','last_name','contact_phone','contact_mobile','created_at']CSV.generate(:col_sep=>";",:row_sep=>"\r\n",:headers=>

随机推荐