我遇到了从文件中提取文本的问题。这是我最终想要做的:
这是我正在处理的部分的片段:
$FileContents = Select-String -Path "C:\Users\me\Documents\file.ini" -Pattern "NoRequest"
Run Code Online (Sandbox Code Playgroud)
当我使用该行时,我得到以下信息:
PS> $文件内容 Documents\file.ini:222:NoRequest = false Documents\file.ini:281:NoRequest = false Documents\file.ini:347:NoRequest = false Documents\file.ini:387:NoRequest = false Documents\file.ini:1035:NOREQUEST = true Documents\file.ini:1047:NoRequest = true Documents\file.ini:1160:NOREQUEST = true Documents\file.ini:1242:NOREQUEST = true
这就是我想要得到的:
PS> $文件内容 错误的 错误的 错误的 错误的 真的 真的 真的 真的
我尝试将代码通过管道传输到Select-Object -Last 1解决方案中,但由于我的变量是一个数组,因此它选择数组中的最后一行,而不是每行的最后一个字。我还想过只取数组并执行“数组中的 foreach 行,将除最后一个字外的所有内容都剪掉”。我猜这会奏效,但似乎这是一个笨拙的解决方案。我确信有一个简单有效的解决方案,但我只是没有看到。
我有一个脚本工作,该脚本使用哈希表存储和比较值,然后将输出写入文本文件。这是在我的输出中有和没有NoNewLine的情况下的示例:
使用NoNewLine:
All servers are all on Version 19.1.5
Run Code Online (Sandbox Code Playgroud)
没有NoNewLine:
All server are all on
Version 19.1.5
Run Code Online (Sandbox Code Playgroud)
因此,NoNewLine解决了该问题,但是我已经在具有Powershell 5.0的服务器上对其进行了测试。当我在另一台服务器上对其进行测试时,Powershell无法识别NoNewLine,因为它在版本4.0中。不幸的是,将所有服务器都安装在较新版本的PS上不是一种选择,因此,有没有一种简便的方法来完成NoNewLine等效的旧PS版本可以处理的工作?我已经搜索了一段时间,但没有找到任何好的替代品。我尝试做整个“ n r”件事,但无法使它正常工作。这是我使用NoNewLine的代码:
if (@($TableVersion.Values | Group-Object).Count -eq 1) {
if (@($TableHash.Values | Group-Object).Count -eq 1) {
Write-Output "The servers are all on " $TableVersion.Serverapps01 | Out-File D:\Results$Timestamp.txt -NoNewline;
}
else {
Write-Output "The servers are all on " $TableVersion.Serverapps01 " but there are differences in the build files." | Out-File D:\Results$Timestamp.txt -NoNewline;
}
}
else {
@(Write-Output "The server versions …Run Code Online (Sandbox Code Playgroud)