Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Function Set-ComputerName
- {
- [CmdletBinding(SupportsShouldProcess=$true)]
- param
- (
- [parameter(Mandatory=$true, Position=0,
- HelpMessage="Letter abbrevation for company e.g JMC, IBM, SCO. This can be anything!")]
- [ValidateScript({
- # Must be 1-3 letters or numbers
- $_ -match '^([a-z]|[A-Z]|[0-9]){1,3}$'
- })]
- [string] $CompanyLetters,
- [switch] $AllUppercase,
- [switch] $Restart,
- [switch] $Force
- )
- #This Function will change the hostname to the following format: JMC-LT-12345ABC or JMC-PC-12345ABC for a unique standard.
- $SerialNumber = Get-CimInstance -ClassName Win32_BIOS | Select-Object -ExpandProperty SerialNumber
- # Checks if Chassis type is: Laptop(9) Notebook(10) Sub Notebook(14)
- if (Get-CimInstance -ClassName win32_SystemEnclosure |
- Where-Object { $_.chassistypes -eq 9 -or $_.chassistypes -eq 10 `
- -or $_.chassistypes -eq 14})
- {
- $newName = $CompanyLetters + "-LT-" + $SerialNumber
- }
- else
- {
- $newName = $CompanyLetters + "-PC-" + $SerialNumber
- }
- if ($PSBoundParameters.ContainsKey("AllUppercase"))
- {
- $newName = $newName.ToUpper()
- }
- if ($newName.Length -gt 15)
- {
- throw "$newName is greater than 15 characters and therfore an invalid NetBIOS name."
- }
- elseif ($Force -or $PSCmdlet.ShouldContinue($newName, "Rename local machine to:"))
- {
- $rnArgs = @{
- NewName = $newName
- Confirm = $false
- Force = $null
- }
- if ($PSBoundParameters.ContainsKey("Restart"))
- {
- $rnArgs.Restart = $null
- }
- Rename-Computer @rnArgs
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement