如何远程修改多台服务器计算机名,计算机名按递增顺序,如computer01,computer02,computer03这种?


PowerShell交流中心分类: 远程管理如何远程修改多台服务器计算机名,计算机名按递增顺序,如computer01,computer02,computer03这种?
0
ruslie asked 10 年 ago

注:这些服务器的登录名及密码都是一致的。

2 Answers
0
Best Answer
Mooser Lee 管理员 answered 10 年 ago
  1. 假设你的已有机器名列表在变量中已经存在,或者在一个文本文件中。
  2. 怎样将1,2,3,4,5构造成001,002,这样然后和现有的机器名拼凑:用string padleft方法:
    PS> 1..5| foreach { $_.tostring().padleft(3,'0') }
    001
    002
    003
    004
    005
  3. 如何远程,假如机器已经开启了远程。可以使用invoke-command命令中执行(rename-computer)方法,否则可以使用wmi方法。比如,更改计算机名
ruslie replied 10 年 ago

非常感谢!请问那如果没有机器名列表,不知道服务器原来的计算机名,只有各个服务器的IP地址的话呢?

Mooser Lee 管理员 replied 10 年 ago

上面对应的命令invoke-command或者wmi,computer参数既支持机器名,也支持IP地址,可以放心使用。

strikene replied 10 年 ago

也可以调用 Win32_ComputerSystem 类ReName 方法进行改名 ReName(“NewName”)或者也可以采用DSC Pull Mode 的方法来获取本机的某些特定信息做计算机名称字符串拼接从而让其自动进行更改

1
codecook 管理员 answered 10 年 ago
#1.首先你得设置一个服务器IP数组
[array]$ipaddress = {"192.168.1.1","192.168.1.2"}

#2.遍历服务器
foreach($ip in $ipaddress){
#设置用户凭据
$pwd= ConvertTo-SecureString "123" -AsPlainText -Force
$cred= New-Object System.Management.Automation.PSCredential("administrator",$pwd)
#建立远程会话并执行相关操作
invoke-command -computer $ip -Credential $cred -ScriptBlock{
  #执行你需要的代码,例如在C盘创建一个文件
  New-Item c:\111.txt -Type file
}
}

 

mad_frog replied 9 年 ago

Invoke-Command : Cannot evaluate parameter ‘ComputerName’ because its argument is specified as a script block and there is no input. A script block cannot be evaluated without input.这个是什么意思呢?如何解决呀

Mooser Lee 管理员 replied 9 年 ago

请指定 server 名称。 Invoke-Command -ComputerName ‘your server’