Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/regex/16.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
Regex 使用正则表达式替换文件中的Powershell_Regex_File_Powershell_Replace_Gpo - Fatal编程技术网

Regex 使用正则表达式替换文件中的Powershell

Regex 使用正则表达式替换文件中的Powershell,regex,file,powershell,replace,gpo,Regex,File,Powershell,Replace,Gpo,下午好,我想修改一个首选项文件,用powershell脚本重置默认的chrome放大,该脚本稍后将由gpo部署 策略是删除负责默认缩放的部分设置。用手做的时候效果很好。然而,通过这个脚本,我得到一个消息,首选项文件是坏的 代码: 有人能解释一下代码的错误吗 谢谢大家! 请注意,Out File默认使用UTF-16LE作为文件编码。很可能您需要将其改为UTF-8(事先检查文件的编码): 原来问题出在replace函数上。此外,编码是ASCII码。以下是相关人员的工作代码: #Déclaration

下午好,我想修改一个首选项文件,用powershell脚本重置默认的chrome放大,该脚本稍后将由gpo部署

策略是删除负责默认缩放的部分设置。用手做的时候效果很好。然而,通过这个脚本,我得到一个消息,首选项文件是坏的

代码:

有人能解释一下代码的错误吗


谢谢大家!

请注意,
Out File
默认使用UTF-16LE作为文件编码。很可能您需要将其改为UTF-8(事先检查文件的编码):


原来问题出在replace函数上。此外,编码是ASCII码。以下是相关人员的工作代码:

#Déclarations
$preferencePath = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\Preferences'
$newPreferencePath = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\newpref.txt'
$oldPrefFile = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\PreferencesBAK'
$regex = '("default_zoom_level":)[^\s]\d*.\d*(":)\d*.\d*(},)'

#Créer la version corrigé du fichier de préférence
Get-Content -path $preferencePath | % { $_ -Replace $regex , ' ' } |  Out-File -Encoding ASCII $newPreferencePath

#Renomme les fichiers pour que le bon soit pris en compte
if (Test-Path $oldPrefFile) {
   Remove-Item $oldPrefFile
}
Rename-Item $preferencePath "PreferencesBAK"
Rename-Item $newPreferencePath "Preferences"

我知道你还没有做过,只是想让你知道你可以点击灰色复选标记来接受你自己的答案,这样对社区来说它会更突出。
... | Out-File -Encoding UTF8 $newPreferencePath
#Déclarations
$preferencePath = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\Preferences'
$newPreferencePath = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\newpref.txt'
$oldPrefFile = Join-Path $env:USERPROFILE 'AppData\Local\Google\Chrome\User Data\Default\PreferencesBAK'
$regex = '("default_zoom_level":)[^\s]\d*.\d*(":)\d*.\d*(},)'

#Créer la version corrigé du fichier de préférence
Get-Content -path $preferencePath | % { $_ -Replace $regex , ' ' } |  Out-File -Encoding ASCII $newPreferencePath

#Renomme les fichiers pour que le bon soit pris en compte
if (Test-Path $oldPrefFile) {
   Remove-Item $oldPrefFile
}
Rename-Item $preferencePath "PreferencesBAK"
Rename-Item $newPreferencePath "Preferences"