I have 200 domain computers and i need to change the host name and rejoin to domain on a click.
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))"
net user Administrator <Password>
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
New-LocalUser
Get-LocalGroupMember
Add-LocalGroupMember
# 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.