Powershell 脚本不工作(拼写错误项目)

Powershell script not working (Misspelling Project)

$app = New - Object - ComObject 'Word.Application'
$app.visible = $true
$doc = $app.Documents.Add(
}
$doc.Content.text = 'Here's an exmple of mispeled txt."
$selection = $app.Selection
$report = @(
}
foreach($word in ($doc.Words | Select - ExpandProperty Text)) {
    if (!($app.CheckSpelling($word))) {
        $result = New - Object - TypeName psobject - Property @(Mispelled = $word)
        $sug = $app.GetSpellingSuggestions($word) | Select - ExpandProperty name
        if ($sug) {
            $report += New - Object psobject - Property @ {
                Misspelled = $word;
                Suggestions = $sug
            }
        } else {
            $report += New - Object - TypeName psobject - Property @ {
                Misspelled $word; =
                Suggestions "No Suggestion"
            }
        }
    }
}
$Sreport | Select - Property Misspelled, Suggestions

您好。我在网上找到了这个脚本,我试图通过 Powershell 使用 MS Word 的拼写更正从中输出拼写错误的英语单词列表及其拼写建议。我没有编码知识,所以我不知道为什么脚本不工作。有人可以帮帮我吗?谢谢!

我认为这有效!你没有提到你想要的输出所以希望这没问题。

你的方向是正确的,但有一些语法和括号问题

$app = New-Object -ComObject 'Word.Application'
$app.visible = $false
$doc = $app.Documents.Add()

$doc.Content.text = Get-Content "C:\Temp\TestFile.txt" # This will get the text of the file specified
$selection = $app.Selection
$report = @() # an empty array which we will fill with spelling mistakes

foreach($word in ($doc.Words | Select -ExpandProperty Text)) { # loop through each word in the text
    # if the word is misspelled then process it
    if (!($app.CheckSpelling($word))) {
        
        # get list of correction suggestions for this word
        $suggestion = $app.GetSpellingSuggestions($word) | Select -ExpandProperty name 
        
        # if there are suggestions then add it to the report
        if ($suggestion) {  
            $report += New-Object psobject -Property @{
                Misspelled = $word;
                Suggestions = $suggestion
            }
        } 
        # else if there are no suggestions then add an entry saying just that
        else { 
            $report += New-Object -TypeName psobject -Property @{
                Misspelled = $word;
                Suggestions = "No Suggestion"
            }
        }
    }
}

# push the results to a file 
$report | Out-File "C:\Temp\results.txt"