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 $x.x27;“我不能和”一起工作`";_Powershell_Match_Special Characters_Contains - Fatal编程技术网

Powershell $x.x27;“我不能和”一起工作`";

Powershell $x.x27;“我不能和”一起工作`";,powershell,match,special-characters,contains,Powershell,Match,Special Characters,Contains,如果我生成一个包含Out file的文本文件,其中包含`n,并且我想证明是否存在`n,那么结果总是错误的 代码如下: $x="c:\temp\test.txt" "`n" | Out-File $x $x.contains("`n") #False 或 或 我做错了什么?$x是一个值为c:\temp\test.txt的字符串-由于字符串c:\temp\test.txt中没有换行符,Contains()返回$false 要查看文件中是否有换行符,请使用Get Content-Raw,将其内容检

如果我生成一个包含
Out file
的文本文件,其中包含
`n
,并且我想证明是否存在
`n
,那么结果总是错误的

代码如下:

$x="c:\temp\test.txt"
"`n" | Out-File $x
$x.contains("`n") #False 


我做错了什么?

$x
是一个值为
c:\temp\test.txt
的字符串-由于字符串
c:\temp\test.txt
中没有换行符,
Contains()
返回
$false

要查看文件中是否有换行符,请使用
Get Content-Raw
,将其内容检索为一个大的多行字符串:

(Get-Content $x -Raw).Contains("`n")

您正在测试的是文件名而不是内容。FYI
-Contains
用于测试数组是否包含对象且无法处理字符串。你需要使用
-Like
,例如:
if($string-Like“*blah*”){#do stuff}
你好,再次感谢你的回答@马蒂亚斯:我试过你的命令,但我收到一条错误信息。参数-Raw未知。
$x -match "`n" #False
(Get-Content $x -Raw).Contains("`n")