简体   繁体   English

从txt文件读取行以重命名服务器-Powershell

[英]read line from txt file to rename server - powershell

I'm trying to read a certain line from a text file using powershell, which can then be used to run with netdom to rename each DC. 我正在尝试使用powershell从文本文件读取特定行,然后可以将其与netdom一起使用以重命名每个DC。 The line is always line number 3 and starts with Servername= . 该行始终是第3行,并以Servername=开头。 I need to grab the text after the equals sign to then use with netdom. 我需要在等号后获取文本,然后将其与netdom一起使用。 It was working at first, but netdom would complain about the syntax. 起初它在工作,但是netdom会抱怨语法。 Is this because it is a string?. 这是因为它是字符串吗? Now servername is returned as the full line: 现在,将servername作为完整行返回:

C:\config.txt:3:ServerName servername

and not just the servername. 不仅是服务器名。

 $servername = Select-String -Path "C:\config.txt" -Pattern "servername="
 $servername = $servername.tostring().split("=")
 netdom renamecomputer localhost /NewName:$servername /force

我认为您必须在第二行后面附加[1]才能选择所需的拆分字符串,即:

$servername = $servername.tostring().split("=")[1]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM