Hi folks!
I´m working on a first logon script to rename fresh installed computers and sometimes add them to AD and sometimes add a description to them. I´m new on powershell scripting and maybe is a dumb question, but that´s the script
do
{
Write-Host "This computer is a laptop or a Workstation? [Default: 1 - Laptop]
1 - Laptop
2 - Workstation"
$computertype = Read-Host
$ctValidate =$false
if ($computertype -eq "" -or $computertype -eq "1") {$computertype = "LT"; $ctValidate = $true}
if ($computertype -eq "2") {$computertype = "WS"; $ctValidate = $true}
if ($ctValidate -eq $false) {Write-Host "Invalid input, it will be considered a laptop"; $computertype = "LT"}
$mf = ""
do
{Write-Host "What is the computer´s manufacturer?"
$mf = Read-Host
$mf = $mf.ToUpper()} while ($mf -eq"")
$serial = (Get-WmiObject win32_bios).serialnumber
$computername = "$computertype-$mf-$serial"
Write-Host "The computer will be renamed to "$computername", ok [Y/N]?"
$nameconfirmation = Read-Host
$nameconfirmation = $nameconfirmation.ToUpper()
} while ($nameconfirmation -ne "Y")
Write-Host "The computer will join the domain? [Y/N]"
$willjoin = Read-Host
$willjoin = $willjoin.ToUpper()
if ($willjoin -eq "Y")
{
Write-host "Renaming and adding the computer to domain xyz.com"
$credentials = New-Object System.Management.Automation.PsCredential(“xyz.com\script”, (ConvertTo-SecureString “xyzabc123” -AsPlainText -Force))
Add-Computer -DomainName “xyz.com” -NewName $computername -Credential $credentials -Force
Write-Host "Do you want to add a description to this computer on AD? [Y/N]"
$willADDesc = Read-Host
$willADDesc = $willADDesc.ToUpper()
if ($willADDesc -eq "Y")
{
Write-Host "What is the description do you want to add?"
$description = Read-Host
Set-ADComputer $computername -Description $description -credential $credentials
}
Restart-Computer
}
else
{
Write-Host "Renaming Computer..."
$username = "$env:COMPUTERNAME\abc"
$credentials = New-object System.Management.Automation.PSCredential ($username,(ConvertTo-SecureString "123654xxxas" -AsPlainText -Force))
Rename-Computer -NewName $computername -LocalCredential $credentials -Restart
}
The problem is that with it the add description is not working and when the computer new name already exists in the ad I receive a error. How can I solve this?
Thanks in advance for any help you guys can provide