powershell - 将文件内容与 Powershell 中的字符串进行比较
问题描述
我正在尝试将文本文件的内容与字符串进行比较。听起来很简单,但没有运气!
$mystring = @'
hello
goodbye
'@
set-content c:\temp\file.txt $mystring
if (Test-Path "c:\temp\file.txt") {
$myfile = Get-Content "c:\temp\file.txt" -raw
if ($myfile -eq $mystring) {
write-host 'File same'
}
else {
write-host 'File different'
}
}
else {
write-host 'No file'
}
Write-Host $mystring.Length
Write-Host $myfile.Length
输出
File different
14
16
两个字符串的长度不同,当您打开文件时,底部有一个新行,我假设长度中额外的 2 个字符来自该行。
我错过了什么?
解决方案
-NoNewline
在set-content
命令中追加
所以你的整行将是
Set-Content "c:\temp\file.txt" $mystring -NoNewline
推荐阅读
- javascript - 访问 JSON-LD 对象会产生错误是 Google 结构数据测试工具
- php - 带有 LIKE 的 Symfony Sonata SEO 自定义 sql 查询
- qt - 在 qlabel 中画圆(显示图像流相机)
- javascript - 修复了容器中的右侧边栏,引导程序
- spring-batch - 如何在春季批处理中阅读带有注释的行#
- angular - 如何隐藏 ngx-bootstrap 模态而不破坏模态组件?
- mysql - 从 Aurora Serverless 导出到 S3
- powerbi - Power BI:切片器以选择表中的值
- haskell - 对特定实例的使用发出警告
- nginx - 当使用标头 Cache-control: no-cache 请求时,nginx 是否会重置 URL 的缓存?