Powershell 基于servicname创建excel sheetname
下面是我的4个csv文件,我正在使用Export-Excel“cmdlet合并这些文件Powershell 基于servicname创建excel sheetname,powershell,Powershell,下面是我的4个csv文件,我正在使用Export-Excel“cmdlet合并这些文件 C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__eKommerceExtensions__eKommerceExtensions.csv C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_Take
C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__eKommerceExtensions__eKommerceExtensions.csv
C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__T1T2_Reweighting__T1T2_ReweightingExtensions.csv
C:\Script\sum_prod-ild-ne-as2-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__eKommerceExtensions__eKommerceExtensions.csv
C:\Script\sum_prod-ild-ne-as2-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__T1T2_Reweighting__T1T2_ReweightingExtensions.csv
如果您注意到这里,同一台服务器有两个文件(prod-ild-ne-as1,prod-ild-ne-as2),因为文件名的下一部分是servicename(例如Proc_mem-check_extender-u ldmodels_udemo_utakehome_HH_p_ueCommerceextensions_ueCommerceextensions)
工作表名称应基于servicename,以便一个服务的所有服务器数据都位于一个工作表中,依此类推。因此,在我编写代码之前,当服务名称很小时,该代码工作正常(例如,check_cpu,check_eth0)
但在这种情况下,由于servicename很长,excel不允许创建带有service name的工作表,因为限制为31个字符
在这种情况下,请告诉我如何使用servicename创建工作表,并将其限制在一定范围内。Excel工作表名称不仅受其长度限制,而且还有一些字符不能使用
- 该名称在单个工作簿中必须是唯一的
- 工作表名称不能超过31个字符
- 您可以使用所有字母数字字符,但不能使用以下特殊字符:
\/*?:[]
- 可以在名称中使用空格、下划线(_)和句点(.)作为单词分隔符
$csvFiles = 'C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__eKommerceExtensions__eKommerceExtensions.csv',
'C:\Script\sum_prod-ild-ne-as1-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__T1T2_Reweighting__T1T2_ReweightingExtensions.csv',
'C:\Script\sum_prod-ild-ne-as2-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__eKommerceExtensions__eKommerceExtensions.csv',
'C:\Script\sum_prod-ild-ne-as2-Proc_mem-check_extender-__ldmodels__Demo_TakeHome_HH_P__T1T2_Reweighting__T1T2_ReweightingExtensions.csv'
$today = (Get-Date).ToUniversalTime()
foreach ($csvPath in $csvFiles) {
# combine the distinctice part of the server name (a1 or a2) with the distinctive
# part of the service name (i.e. the stuff between the last underscore and the .csv extension)
# take out all invalid characters and remove doubled underscores
$sheetName = $csvPath -replace '^.+-(as[12])-.+_(\w+)\.csv', '$1_$2' -replace '[\\/*?:[\]]+', '_'
# make sure the sheetname does not exceed the 31 character limit
if ($sheetName.Length -gt 31) { $sheetName = $sheetName.Substring(0, 31) }
$target = Join-Path -Path $PSScriptRoot -ChildPath ('Combind_Data_{0:yyyyMMdd-HHmm}.xlsx' -f $today)
Import-Csv -Path $csvPath | Export-Excel -Path $target -WorksheetName $sheetName -Append
}
使用上述文件名示例,图纸将命名为:
as1_eKommerceExtensions
as1_ReweightingExtensions
as2_eKommerceExtensions
as2_ReweightingExtensions
as1_电子商务扩展
as1_重新称重扩展
as2_电子商务扩展
as2_重新称重扩展
正则表达式详细信息:
^ Assert position at the beginning of the string
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the character “-” literally
( Match the regular expression below and capture its match into backreference number 1
as Match the characters “as” literally
[12] Match a single character present in the list “12”
)
- Match the character “-” literally
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
_ Match the character “_” literally
( Match the regular expression below and capture its match into backreference number 2
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\. Match the character “.” literally
csv Match the characters “csv” literally
^断言字符串开头的位置
.匹配不是换行符的任何单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
-按字面意思匹配字符“-”
(匹配下面的正则表达式,并将其匹配捕获到反向引用编号1中
按字面意思匹配字符“as”
[12] 匹配列表“12”中的单个字符
)
-按字面意思匹配字符“-”
.匹配不是换行符的任何单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
_按字面意思匹配字符“u”
(匹配下面的正则表达式,并将其匹配捕获到反向引用编号2中
\w匹配一个“单词字符”(字母、数字等)的单个字符
+在一次和无限次之间,尽可能多次,根据需要回馈(贪婪)
)
\.按字面意思匹配字符“.”
csv按字面意思匹配字符“csv”
^ Assert position at the beginning of the string
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
- Match the character “-” literally
( Match the regular expression below and capture its match into backreference number 1
as Match the characters “as” literally
[12] Match a single character present in the list “12”
)
- Match the character “-” literally
. Match any single character that is not a line break character
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
_ Match the character “_” literally
( Match the regular expression below and capture its match into backreference number 2
\w Match a single character that is a “word character” (letters, digits, etc.)
+ Between one and unlimited times, as many times as possible, giving back as needed (greedy)
)
\. Match the character “.” literally
csv Match the characters “csv” literally