Advertisement
Guest User

Untitled

a guest
Jan 15th, 2019
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. Function Set-ComputerName
  2. {
  3.     [CmdletBinding(SupportsShouldProcess=$true)]
  4.     param
  5.     (
  6.         [parameter(Mandatory=$true, Position=0,
  7.             HelpMessage="Letter abbrevation for company e.g JMC, IBM, SCO. This can be anything!")]
  8.         [ValidateScript({
  9.             # Must be 1-3 letters or numbers
  10.             $_ -match '^([a-z]|[A-Z]|[0-9]){1,3}$'
  11.         })]
  12.         [string] $CompanyLetters,
  13.  
  14.         [switch] $AllUppercase,
  15.  
  16.         [switch] $Restart,
  17.  
  18.         [switch] $Force
  19.     )
  20.  
  21.     #This Function will change the hostname to the following format: JMC-LT-12345ABC or JMC-PC-12345ABC for a unique standard.
  22.  
  23.     $SerialNumber = Get-CimInstance -ClassName Win32_BIOS | Select-Object -ExpandProperty SerialNumber
  24.  
  25.     # Checks if Chassis type is: Laptop(9) Notebook(10) Sub Notebook(14)
  26.     if (Get-CimInstance -ClassName win32_SystemEnclosure |
  27.             Where-Object { $_.chassistypes -eq 9 -or $_.chassistypes -eq 10 `
  28.                 -or $_.chassistypes -eq 14})
  29.     {
  30.         $newName = $CompanyLetters + "-LT-" + $SerialNumber
  31.     }
  32.     else
  33.     {
  34.         $newName = $CompanyLetters + "-PC-" + $SerialNumber
  35.     }
  36.  
  37.     if ($PSBoundParameters.ContainsKey("AllUppercase"))
  38.     {
  39.         $newName = $newName.ToUpper()
  40.     }
  41.  
  42.     if ($newName.Length -gt 15)
  43.     {
  44.         throw "$newName is greater than 15 characters and therfore an invalid NetBIOS name."
  45.     }
  46.     elseif ($Force -or $PSCmdlet.ShouldContinue($newName, "Rename local machine to:"))
  47.     {
  48.         $rnArgs = @{
  49.              NewName = $newName
  50.              Confirm = $false
  51.              Force = $null
  52.         }
  53.         if ($PSBoundParameters.ContainsKey("Restart"))
  54.         {
  55.             $rnArgs.Restart = $null
  56.         }
  57.         Rename-Computer @rnArgs
  58.     }
  59. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement