我尝试使用 powershell 的选择字符串在文档中查找特定的单词(Gate),但它没有返回任何结果。
Get-ChildItem -path '.\somedocument.docx' | Select-String -pattern 'Gate'
我做错了什么?我正处于学习 powershell 的初始阶段。
答案1
由于您是 PoSH 世界的新手。首先获得一些指导/培训非常重要,以防止您迷失/沮丧。请参阅此讨论。
帮助教授 PowerShell 学习 PowerShell https://www.reddit.com/r/PowerShell/comments/7oir35/help_with_teaching_others_powershell
CConard96 关于 Get-Children 的说法是正确的,您可以使用 Get-Content 或 .Net 文件库读取文件。
PowerShell 可以完全访问 Windows 提供的任何 .Net 库、COM 和 DOM 接口。
因此,您需要了解并使用正确的工具集。对于您所追求的目标,我不会将 Select-String 用于此用例。
您可以使用 PoSH 来执行此操作,但您还必须使用 Word DOM。例如:
# Instantiate Word object
$wd = New-Object -com word.application
# Oepn a Word doc
$doc = $wd.Documents.Open('D:\Documents\Microsoft Graph API.docx')
# Read all the doc contents
$doc.Range().text
Microsoft Graph APIList windowsInformationProtectionAppLearningSummariesImportant: APIs under the /beta version in Microsoft Graph are in preview and are subject to change. Use of these APIs in production applicatio
ns is not supported.Note: Using the Microsoft Graph APIs to configure Intune controls and policies still requires that the Intune service is correctly licensed by the customer.https://developer.microsoft.com/en-us/g
raph/docs/api-reference/beta/api/intune_wip_windowsinformationprotectionapplearningsummary_list https://social.technet.microsoft.com/wiki/contents/articles/33525.an-introduction-to-microsoft-graph-api.aspx Using the
Microsoft Graph API to access data in Microsoft Intunehttps://blogs.technet.microsoft.com/intunesupport/2016/10/04/using-the-microsoft-graph-api-to-access-data-in-microsoft-intune How to use Microsoft Graph and Off
ice 365 API in a Service or in a Windows App/UWP without a graphical interfacehttps://blogs.msdn.microsoft.com/laurelle/2016/02/12/how-to-use-microsoft-graph-and-office-365-api-in-a-service-or-in-a-windows-appuwp-wi
thout-a-graphical-interface
# Get formatted text
$doc.Range().paragraphs | foreach {$_.range.text}
List windowsInformationProtectionAppLearningSummaries
Important: APIs under the /beta version in Microsoft Graph are in preview and are subject to change. Use of these APIs in production applications is not supported.
Note: Using the Microsoft Graph APIs to configure Intune controls and policies still requires that the Intune service is correctly licensed by the customer.
https://developer.microsoft.com/en-us/graph/docs/api-reference/beta/api/intune_wip_windowsinformationprotectionapplearningsummary_list
https://social.technet.microsoft.com/wiki/contents/articles/33525.an-introduction-to-microsoft-graph-api.aspx
Using the Microsoft Graph API to access data in Microsoft Intune
https://blogs.technet.microsoft.com/intunesupport/2016/10/04/using-the-microsoft-graph-api-to-access-data-in-microsoft-intune
How to use Microsoft Graph and Office 365 API in a Service or in a Windows App/UWP without a graphical interface
https://blogs.msdn.microsoft.com/laurelle/2016/02/12/how-to-use-microsoft-graph-and-office-365-api-in-a-service-or-in-a-windows-appuwp-without-a-graphical-interface
# Find a specific word(s) in the Doc file, for example Graph or under or licensed using the .Net Regualr Expression namespace
[regex]::Matches(($doc.Range().text),'Graph|under|licensed').value
Graph
under
Graph
Graph
licensed
Graph
Graph
$wd.quit()
您不需要上述所有方法,因为我已经为您解决了用例,因为它可以简化。然而,您看,这是可以做到的。
本论坛上还有其他示例。
从 Word 文档中获取特定数据 https://www.reddit.com/r/PowerShell/comments/38dcm7/getting_specific_data_out_of_a_word_document
您甚至可以将文档转换为其他文件类型,然后使用 cmdlet 读取它,但这只是额外的工作。如果没有必要,为什么要这么做呢?