0

I've been trying to rename a domain computer with the following script:

$username = "domain\username"
$password = "password"
$ip = ((ipconfig | findstr [0-9].\.)[0]).Split()[-1]
$hostname = (nslookup $ip)[3]
$hostname = $hostname.replace(" ", "")
$hostname = $hostname.split(":")[1]
$hostname = $hostname.split(".")[0].ToLower()
Rename-Computer -NewName $hostname -DomainCredential $username  -Restart -Force

It does everything I desire apart from inputting the password which at this point is a manual process. Can someone advise me on how to get it to input from $password into the prompt box so that I can completely automate the process?

Alternatively, if there's a better way to do it in Powershell, I'm open to going another direction.

BenH
  • 9,766
  • 1
  • 22
  • 35
Wulf
  • 19
  • 1
  • 6

1 Answers1

1

You can use this code:

$Username = "DomainUserName"
$Password = "PlainPassword" | ConvertTo-SecureString -AsPlainText -Force
$Creds = New-Object System.Management.Automation.PSCredential($Username ,$Password)


Rename-Computer -NewName $newComputerName -ComputerName $OldName -Restart -DomainCredential $Creds
4c74356b41
  • 69,186
  • 6
  • 100
  • 141
Venkatakrishnan
  • 776
  • 11
  • 26
  • I made a few minor changes to bring this inline with my original above script but it worked perfectly. Thanks so much for your help! – Wulf Jan 31 '17 at 18:37
  • The only thing that needs to be added (since I'm using a deployment environment to use this script) is adding the line at the beginning: Set-ExecutionPolicy -ExecutionPolicy Bypass. This will allow it to Bypass the Execution Policy that blocks non-signed powershell scripts/commands if it's locked down through GPOs. – Wulf Jan 31 '17 at 22:47