首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >批处理脚本来重命名计算机并在一次单击中连接到域

批处理脚本来重命名计算机并在一次单击中连接到域
EN

Stack Overflow用户
提问于 2020-05-28 15:29:21
回答 1查看 3.9K关注 0票数 0

我有200台域计算机,我需要更改主机名并在单击时重新加入到域。

  1. 首次手术
代码语言:javascript
复制
Powershell.exe "-ExecutionPolicy Bypass Add-Computer -DomainName <Domain Name> -ComputerName <Old_ComputerName> -NewName <New_ComputerName> -$credential = New-Object System.Management.Automation.PSCredential($username = "<Domain\User ID>", ($password = <Password> | ConvertTo-SecureString -asPlainText -Force))" 
  1. 第二次手术
代码语言:javascript
复制
net user Administrator <Password>
  1. 重新启动计算机

但是,在cmd中以提升的权限执行上述脚本时。我得到了下面的错误。

代码语言:javascript
复制
At line:1 char:206
+ ... ement.Automation.PSCredential($username = Domain\User ID, ($passw ...
+                                                                 ~
Missing argument in parameter list.
    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : MissingArgument  

我们正在寻找一个单行命令来执行上面的操作。

EN

回答 1

Stack Overflow用户

发布于 2020-05-29 05:20:06

此命令语法不正确。

这样做是一个脚本并运行脚本(使用PS2EXE工具作为exe交付给用户),因此不需要批处理文件。

代码语言:javascript
复制
# Begin Script
$AddComputerSplat = @{
    DomainName   = <Domain Name> 
    ComputerName = <Old_ComputerName> 
    NewName      = '<New_ComputerName>'
    $credential  = New-Object System.Management.Automation.PSCredential($username = 'Domain\UserID', 
    ($password   = 'Password' | ConvertTo-SecureString -asPlainText -Force))
}
Add-Computer @AddComputerSplat

获取-本地用户 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1 新本地用户 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/new-localuser?view=powershell-5.1 Get-LocalGroupMember https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember?view=powershell-5.1 添加-本地组成员 https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/add-localgroupmember?view=powershell-5.1

代码语言:javascript
复制
# End Script

或者,如果您只想使用cmd.exe并调用consoelhost,那么这种方法应该适用于您。

代码语言:javascript
复制
$Command = 'Add-Computer -DomainName <Domain Name> -ComputerName <Old_ComputerName> -NewName <New_ComputerName> -$credential = New-Object System.Management.Automation.PSCredential($username = "<Domain\User ID>", ($password = <Password> | ConvertTo-SecureString -asPlainText -Force))"'
powershell -ArgumentList '-NoExit', 'NoProfile', '-ExecutionPolicy Bypass', "-Command  &{ $ConsoleCommand }"
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62068509

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档