Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 提取特定数据_Powershell - Fatal编程技术网

Powershell 提取特定数据

Powershell 提取特定数据,powershell,Powershell,请帮忙。我正在尝试从以下.xml文件中提取多个文件名。然后我需要将文件列表从一个文件夹复制到另一个文件夹。下面是我发布的XML的一部分: 输出 PowerShell\hpsum_inventory.xml:8: <filename>CP021404.scexe</filename> PowerShell\hpsum_inventory.xml:18: <filename>CP021614.scexe</filename>

请帮忙。我正在尝试从以下.xml文件中提取多个文件名。然后我需要将文件列表从一个文件夹复制到另一个文件夹。下面是我发布的XML的一部分:

输出

PowerShell\hpsum_inventory.xml:8:        <filename>CP021404.scexe</filename>
PowerShell\hpsum_inventory.xml:18:        <filename>CP021614.scexe</filename>

问题是您使用的是正则表达式匹配,正则表达式中的句点字符与除换行符/换行符以外的任何字符都匹配,因此它与后跟“exe”的任何字符都匹配。实际上,您要做的是将文件读取为XML,然后只输出节点

编辑:好的,你需要把它合并到一个字符串中,这很简单。我们将在最后一行添加一个ForEach循环,我使用别名%将文件名插入字符串

(Select-Xml -Path $input_path -XPath //filename).node.InnerText | ?{$_ -match $regex} | %{"copy c:\powershell\$_ x:\firmware\"} | out-file $output_file
Edit2:好的,你想知道如何匹配文件中的文本。我能行!Select string将实现您想要的功能实际上,对于您前面给出的示例来说,它并不是最好的方法。这变得有点有趣,因为您需要熟悉正则表达式匹配模式,但除此之外,它相当简单。您想再次使用-Pattern匹配,但让我建议一种更好的模式:


文件名>*?谢谢。。你真聪明。。。你能帮我在每行前后加上文字吗。它应该类似于副本c:\powershell\cp021404.scexe x:\firmware\Answer,已针对您的其他请求进行更新。如果这足够回答您的问题,请将其标记为已接受。您的语法飞得像CHAMP。但是,我无法让select string命令只显示没有行的文件名。我也删除了句号。我有一个类似的文本文件,它确实有节点。它看起来像*************服务器刀片1信息:类型:服务器刀片产品名称:ProLiant BL680c G5零件号:443527-B21序列号:USE007XLPS服务器刀片2信息:类型:服务器刀片产品名称:ProLiant BL680c G5零件号:443527-B21序列号:USE006XD8F我需要从中提取序列号。由于是纯文本,上述语法可能不起作用。我正在寻找另一种从文本文件中提取特定信息的方法。我还没有阅读下面答案中的所有内容,但似乎下次你应该更具体,写一个更容易理解的问题:欢迎来到SO。
PowerShell\hpsum_inventory.xml:8:        <filename>CP021404.scexe</filename>
PowerShell\hpsum_inventory.xml:18:        <filename>CP021614.scexe</filename>
$input_path = ‘C:\PowerShell\hpsum_inventory.xml’
$output_file = ‘C:\powershell\hpsum_inventory-o.xml’
$regex = "exe$"
(Select-Xml -Path $input_path -XPath //filename).node.InnerText | ?{$_ -match $regex} | out-file $output_file
(Select-Xml -Path $input_path -XPath //filename).node.InnerText | ?{$_ -match $regex} | %{"copy c:\powershell\$_ x:\firmware\"} | out-file $output_file
$input_path = 'C:\PowerShell\hpsum_inventory.xml'
$output_file = 'C:\powershell\hpsum_inventory-o.xml'
$regex = "filename>(.*?)<"
select-string -Path $input_path -Pattern "filename>(.*?)<"|%{$_.matches.groups[1].value}
select-string -Path $input_path -Pattern "filename>(.*?)<"|%{"copy c:\powershell\$($_.matches.groups[1].value) x:\firmware"}|Out-File $output_file
$input_path = 'C:\PowerShell\hpsum_inventory.xml'
$output_file = 'C:\powershell\hpsum_inventory-o.xml'
$regex = "filename>(.*?)<"
$Filenames = select-string -Path $input_path -Pattern "filename>(.*?)<"|%{$_.matches.groups[1].value}
$Filenames|%{"copy c:\powershell\$_ x:\firmware"}|Out-File $output_file
$Filenames|?{$_ -notin (GCI X:\firmware -file|select -expand name)}|%{"copy c:\powershell\$_ x:\firmware"}|Out-File $output_file