【问题标题】:command line or batch file to get the name of the computer and find and replace serverName with the computer name in a text file?命令行或批处理文件来获取计算机的名称并在文本文件中查找服务器名称并将其替换为计算机名称?
【发布时间】:2013-05-03 15:12:50
【问题描述】:

命令行或批处理文件获取“计算机名称”并在文本文件中查找“serverName”并将其替换为文本文件中的“计算机名称”?

【问题讨论】:

  • serverName 是文本文件中的唯一行吗? serverName 与计算机名如何对应?你真的需要更好地解释自己,你遗漏了很多信息。

标签: powershell batch-file command


【解决方案1】:

获取主机名很容易。像这样使用 hostname.exe:

$hostname = hostname.exe

或 WMI:

$hostname = (Get-WmiObject Win32_ComputerSystem).Name

替换文件中的文本通常是这样的:

$content = Get-Content file.txt
$content | Foreach {$_ -replace "serverName",$hostname} | Out-File file.txt -Enc ascii

上面的$_ 将代表文件的每一行。这是因为 $content 将包含一组行,这些行将一次一行地从管道中输入。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-25
    • 2010-10-02
    • 2012-11-29
    • 2015-05-16
    • 2022-11-02
    相关资源
    最近更新 更多