Flip the name and serial number

Welcome Forums General PowerShell Q&A Flip the name and serial number

This topic contains 2 replies, has 2 voices, and was last updated by

 
Participant
1 month ago.

  • Author
    Posts
  • #178158

    Participant
    Topics: 1
    Replies: 1
    Points: 19
    Rank: Member

    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

     

     

     

     

     

  • #178179

    Participant
    Topics: 1
    Replies: 11
    Points: 194
    Helping Hand
    Rank: Participant

    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

  • #178386

    Participant
    Topics: 1
    Replies: 1
    Points: 19
    Rank: Member

    thank you, it worked like a charm !!!! 🙂

You must be logged in to reply to this topic.