Flip the name and serial number

hi,

Hope this is an easy one, I need to flip the serial and name. right now it works but I get the Serial number first then "SiteA(b)(C)

we need SiteA-serialnumber

 

 

Write-Host “Please enter your desired Department [1-3] [Default 1]:

  1. SiteA
  2. SiteB
  3. SiteC"
    $DM = Read-Host

$validate = $false
if ($DM -eq “” -or $DM -eq “1”) { $DM = “SiteA-”; $validate = $true }
if ($DM -eq “2”) { $DM = “SiteB-”; $validate = $true }
if ($DM -eq “3”) { $DM = “SiteC-”;$validate = $true }
if ($validate -eq $false) { Write-Host “Invalid input, defaulting to [1].”; $DM = “SiteA-”}

$newname = “$(Get-WmiObject win32_bios | select -expand serialnumber)$DM”
Rename-Computer -NewName $newname -Force
$DomainToJoin = “test.com

 

i get this

 

Rename-Computer : Fail to rename computer ‘TESTWIN10B’ to ‘GCHTPV2SiteA-’ due to the following exception: Access is denied.
At line:14 char:1

  • Rename-Computer -NewName $newname -Force
  • CategoryInfo : OperationStopped: (TESTWIN10B:String) [Rename-Computer], InvalidOperationException
  • FullyQualifiedErrorId : FailToRenameComputer,Microsoft.PowerShell.Commands.RenameComputerCommand

 

 

 

 

 

Hi Oscar,

 

probabluy the easiest solution is to just split out a few of those compressed lines and take your time building out the strings you need.

in the case of

 

$newname = "$(Get-WmiObject win32_bios | select -expand serialnumber)$DM"
you can split this out to
$SerialNumber = Get-WmiObject win32_bios | select -expand serialnumber

$NewName = $SerialNumber + ‘-’ + $DM


also for your validation section you could use a Switch (shoutout to Kevin Marquette) instead of the series of If statements.

switch ( $DM ) { 1 { $Site = 'SiteA' } 2 { $Site = 'SiteB' } 3 { $Site = 'SiteC' } default {$Site = 'SiteA' } } Write-Host $Site
I hope that helps.

 

-Conor

thank you, it worked like a charm !!! :slight_smile: