gpt4 book ai didi

powershell - 写输出-文本引号之间的nonewline变量

转载 作者:行者123 更新时间:2023-12-02 23:57:40 24 4
gpt4 key购买 nike

我正在编写一个PowerShell文件,该文件创建一个批处理文件,在包含引号的文本之间具有变量。

请参阅下面的脚本,以便您理解我的意思。

$addnewcomputerhere = Read-Host -Prompt 'Input new computername'
$addnewusernamehere = Read-Host -Prompt 'Input new username'
Write-Output 'powershell -Command "(gc sourcechangeme.txt) -replace ''newcomputername'', '''$addnewcomputerhere'''| Out-File source1changeme.txt"' | Add-Content edittextdone.bat
Write-Output 'powershell -Command "(gc sourcechangeme.txt) -replace ''newusername'', '''$addnewusernamehere'''| Out-File source1changeme.txt"' | Add-Content edittextdone.bat

然后运行脚本 edittextdone.bat如下所示:
powershell -Command "(gc sourcechangeme.txt) -replace 'newcomputername', 'thecomputernamethathasbeengivenonprompt'| Out-File source1changeme.txt"
powershell -Command "(gc sourcechangeme.txt) -replace 'newusername', 'theusernamethathasbeengivenonprompt'| Out-File source1changeme.txt"

相反,它看起来像这样:
powershell -Command "(gc sourcechangeme.txt) -replace 'newcomputername', '
thecomputernamethathasbeengivenonprompt'| Out-File source1changeme.txt"
powershell -Command "(gc sourcechangeme.txt) -replace 'newusername', '
theusernamethathasbeengivenonprompt'| Out-File source1changeme.txt"

我的问题是变量开始新行。

我一直在寻找任何建议,但碰巧遇到了 this

我还阅读了有关 -NoNewline的信息,但无法弄清楚如何正确使用它。

最佳答案

不要依赖隐式字符串连接。使用format operator(-f)将值插入输出字符串中:

'powershell -Command "(gc sourcechangeme.txt) -replace ''newcomputername'', ''{0}''| Out-File source1changeme.txt"' -f $addnewcomputerhere | Add-Content edittextdone.bat
'powershell -Command "(gc sourcechangeme.txt) -replace ''newusername'', ''{0}''| Out-File source1changeme.txt"' -f $addnewusernamehere | Add-Content edittextdone.bat

关于powershell - 写输出-文本引号之间的nonewline变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39830743/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com