簡體   English   中英

在cmd腳本中運行powershell命令並替換字符串內容

[英]Running a powershell command inside cmd script and replacing string content

我正在嘗試在cmd腳本中運行powershell命令來替換文本文件內容

set file="C:\myfile.txt"

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace "Latest", "FOO" } | Set-Content %file%"

我收到以下錯誤:

您必須在“ -replace”運算符的右側提供一個值表達式。

我究竟做錯了什么?

謝謝

編輯 (不確定與答案是否相關...)

文件內容是

\\10.10.10.10\Shared\Latest

您沒有正確嵌套字符串

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace "Latest", "FOO" } | Set-Content %file%"
                    ^----------------------------------------------------^
               string start                                        string end

使用單引號:

powershell -command "(Get-Content %file%) | ForEach-Object { $_ -replace 'Latest', 'FOO' } | Set-Content %file%"

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM