简体   繁体   中英

Batch Script to rename computer and join to domain on a single click

I have 200 domain computers and i need to change the host name and rejoin to domain on a click.

  1. First operation
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. Second operation
net user Administrator <Password>
  1. Restart computer

But, While executing the above script in cmd with elevated privileges. I am getting the below error.

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

We are looking for a single line command to do the above operation.

This command syntax is not correct.

Do this is a script and run the script (deliver to users as an exe using the PS2EXE tool), thus no need for a batch file.

# 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

Get-LocalUser

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localuser?view=powershell-5.1

New-LocalUser

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/new-localuser?view=powershell-5.1

Get-LocalGroupMember

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/get-localgroupmember?view=powershell-5.1

Add-LocalGroupMember

https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.localaccounts/add-localgroupmember?view=powershell-5.1

# End Script

Or if you just want to use cmd.exe and call the consoelhost, then this approach should work for you.

$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 }"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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