Seth Garrett
asked on
PS Script to rename computer based on current logged in user and device type
Hi guys,
Can someone please help with the powershell script for changing the computer name based on current logged in user and device type.
PS: We don't have AD/DC - nothing on prem. All standalone PC's connected to AzureAD. So we will push this script down to the machine using the our MDM tool.
For example:
If the current logged in user on the PC is AzureAD\JohnSmith and Device Type is a Laptop, then the computer should be renamed as JohnSmith-L
Basically just get the parts in bold above and concatenate them and then set it as the computer name.
I've got a few bits together for getting the Name and Device Type. Just cant get my head around how to set the name as desired.
To get the current logged in user name:
To get the device type:
Any help with this or may be even some pointers in the right direction would be appreciated. Many thanks.
Can someone please help with the powershell script for changing the computer name based on current logged in user and device type.
PS: We don't have AD/DC - nothing on prem. All standalone PC's connected to AzureAD. So we will push this script down to the machine using the our MDM tool.
For example:
If the current logged in user on the PC is AzureAD\JohnSmith and Device Type is a Laptop, then the computer should be renamed as JohnSmith-L
Basically just get the parts in bold above and concatenate them and then set it as the computer name.
I've got a few bits together for getting the Name and Device Type. Just cant get my head around how to set the name as desired.
To get the current logged in user name:
$(Get-WMIObject -class Win32_ComputerSystem | select username).username
To get the device type:
function check-chassis {
BEGIN {}
PROCESS {
Write-Output "Processing $_ which is a:-"
$computer = "$_"
$chassis = Get-WmiObject win32_systemenclosure -computer $computer | select chassistypes
if ($chassis.chassistypes -contains '3'){Write-Output "Desktop"}
elseif ($chassis.chassistypes -contains '4'){Write-Output "Low Profile Desktop"}
elseif ($chassis.chassistypes -contains '5'){Write-Output "Pizza Box"}
elseif ($chassis.chassistypes -contains '6'){Write-Output "Mini Tower"}
elseif ($chassis.chassistypes -contains '7'){Write-Output "Tower"}
elseif ($chassis.chassistypes -contains '8'){Write-Output "Portable"}
elseif ($chassis.chassistypes -contains '9'){Write-Output "Laptop"}
elseif ($chassis.chassistypes -contains '10'){Write-Output "Notebook"}
elseif ($chassis.chassistypes -contains '11'){Write-Output "Hand Held"}
elseif ($chassis.chassistypes -contains '12'){Write-Output "Docking Station"}
elseif ($chassis.chassistypes -contains '13'){Write-Output "All in One"}
elseif ($chassis.chassistypes -contains '14'){Write-Output "Sub Notebook"}
elseif ($chassis.chassistypes -contains '15'){Write-Output "Space-Saving"}
elseif ($chassis.chassistypes -contains '16'){Write-Output "Lunch Box"}
elseif ($chassis.chassistypes -contains '17'){Write-Output "Main System Chassis"}
elseif ($chassis.chassistypes -contains '18'){Write-Output "Expansion Chassis"}
elseif ($chassis.chassistypes -contains '19'){Write-Output "Sub Chassis"}
elseif ($chassis.chassistypes -contains '20'){Write-Output "Bus Expansion Chassis"}
elseif ($chassis.chassistypes -contains '21'){Write-Output "Peripheral Chassis"}
elseif ($chassis.chassistypes -contains '22'){Write-Output "Storage Chassis"}
elseif ($chassis.chassistypes -contains '23'){Write-Output "Rack Mount Chassis"}
elseif ($chassis.chassistypes -contains '24'){Write-Output "Sealed-Case PC"}
else {Write-output "Unknown"}
}
END{}
}
"localhost" | check-chassis
Any help with this or may be even some pointers in the right direction would be appreciated. Many thanks.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks guys for the input.
Managed to achieve most of the requirement (with your help above) except for device name in desired format.. which I cant get my head around.
Using this if the current logged-in user is JohnSmith and is on a Laptop, it renames the machine as JohnSmith-L which is good.
However, with AzureAD joined devices the whoami returns current loggedin user as AzureAd\JohnSmith, so using above script renames the computer as: AzureAD\JohnSmith-L
2 questions here:
1. Any idea how to get rid of AzureAD\ from AzureAD\JohnSmith?
2. Is -Restart absolutely necessary OR if not will it just take effect when it restarts next (which is ideal)?
Managed to achieve most of the requirement (with your help above) except for device name in desired format.. which I cant get my head around.
$user = $(Get-WMIObject -class Win32_ComputerSystem | select username).username | split-path -Leaf
$chassis = Get-WmiObject win32_systemenclosure | select-object -expandproperty chassistypes
switch ($chassis)
{
8 { $ct="L";break;}
9 { $ct="L";break;}
10 { $ct="L";break;}
11 { $ct="L";break;}
14 { $ct="L";break;}
default { $ct = "D" }
}
$newname = $user + "-" + $ct
Write-Output($env:COMPUTERNAME + " Will Be Renamed:" + $newname)
Rename-Computer -NewName $newname -Restart -WhatIf
Using this if the current logged-in user is JohnSmith and is on a Laptop, it renames the machine as JohnSmith-L which is good.
However, with AzureAD joined devices the whoami returns current loggedin user as AzureAd\JohnSmith, so using above script renames the computer as: AzureAD\JohnSmith-L
2 questions here:
1. Any idea how to get rid of AzureAD\ from AzureAD\JohnSmith?
2. Is -Restart absolutely necessary OR if not will it just take effect when it restarts next (which is ideal)?
Any idea how to get rid of AzureAD\ from AzureAD\JohnSmith?
$username = "AzureAD\JohnSmith";
$username.Split("\")[1];
absolutely necessary OR if not will it just take effect when it restarts next (which is ideal)?You can omit and it will take effect on next reboot
$user = $(Get-WMIObject -class Win32_ComputerSystem | select username).username | split-path -Leaf
That should have stripped off the AZURE-AD\ part
That should have stripped off the AZURE-AD\ part
ASKER
Thanks David. That worked perfectly!
Open in new window
What do you want for device type, the chassis?