Skip to main content

PowerShell + SCCM Tip: Get MachineName for a User

While doing Application deployments , we have cases where the User forgets to specify the Machine Name to which a software needs to be deployed to.

We use Query-Based Deployment rules in ConfigMgr where we add the machine names to a Collection named after the Application..have already automated this process ;)

But if the machine name is not specified then we either:

  1. Get the Machine name from SCCM Reports if available.
  2. Drop an email to the Requester for the same
For getting the machine name for a User in ConfigMgr 07 we use the Reports obviously :D


The report is located under "Users" node in ConfigMgr Reports page.

But actually this can be retrieved using WMI/CIM too.

The Class which has this info is SMS_R_System under namespace Root/SMS/site_<SiteCode> and let's take a look at it.

Note: Before going further I have already set the ComputerName parameter and namespace parameter in the $PSDefaultParameters. Below is how you do it in PS Console.
$PSDefaultParameterValues = @{"Get-WMIObject:Namespace"="Root/SMS/site_DEX";"Get-WMIObject:ComputerName"="DexSCCM";"Get-CIMCLass:Namespace"="Root/SMS/site_DEX";"Get-CImClass:ComputerName"="DexSCCM"}

Now let's look at the properties of the class we are interested in. 




Now let's use the Get-WMIObject or Get-CIMInstance to get a machine name for a user "Administrator"

Note doing this in my Lab so just going ahead with the User "Administrator". This works for both ConfigMgr 07 and 12 that's the beauty of using WMI ;)

Get-CimInstance -Query "Select NetbiosName from SMS_R_System where lastlogonusername='Administrator'"
You will notice that running the above will only return back the netbiosname property...doing this as it is the only information am after right now.

For the above WMI/CIM query we need to know the SamAccountName of the User, so suppose we have a User Named "Dexter POSH" then we could leverage ADSI here to filter out the User's matching the name.

So I have written a Function named Get-SCCMUserComputer which does all this.

After you have loaded the function then you can run it like below to pass samaccountnames to the -Identity parameter
Get-SCCMUserComputer -identity administrator -Verbose
VERBOSE: [BEGIN] WSMAN is responsive
VERBOSE: Operation '' complete.
VERBOSE: [BEGIN] [WSMAN] CIM SESSION - Opened
VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = select * from SMS_ProviderLocation where ProviderForLocalSite = true,
'queryDialect' = WQL,'namespaceName' = root\sms'.
VERBOSE: Operation 'Query CimInstances' complete.
VERBOSE: [BEGIN] Provider is located on DexSCCM.dexter.com in namespace root\sms\site_DEX
VERBOSE: Perform operation 'Query CimInstances' with following parameters, ''queryExpression' = Select NetbiosName from SMS_R_System where LastlogonUserName='adminis
trator','queryDialect' = WQL,'namespaceName' = root\sms\site_DEX'.

SamAccountName                                                                     ComputerName                                                                     
--------------                                                                     ------------                                                                     
administrator                                                                      DEXTERDC                                                                         
VERBOSE: Operation 'Query CimInstances' complete.
VERBOSE: [END] Ending the Function

Now there is another parameter by the name -Name :P
So what it does is it uses ADSI to search the domain for matching users and prompts you to select one out of those to move forward. Let me show that real quick so if I invoke the function like below:
Get-SCCMUserComputer -Name Admin -Verbose

Now the Script will use "*Admin*" to search the AD using ADSI and ask you to select the Users (multiple Users can be selected... :) )you want to get the netbiosname for..below is a screenshot showing this:




Only other thing worth mentioning is that when using the -Name parameter how the ADSI filter is created.  If you specify a Name without any spaces then an asterix is added to both start and end to filter out using ADSI.
For Ex - In above example "Admin" was specified and the filter used is "*Admin*"

But if I specify an argument "Dexter POSH" then the filter used is "Dexter*POSH" I did this because if someone is specifying the name in this way then he probably knows what is the first and last name he is searching for.

Not saying the best way but am going ahead with this....any inputs are welcomed.


NOTE -- Sometimes this information might not be populated there in SCCM so if this is not available then one needs to drop an email asking the User for the Machinename.

Hope this saves a few Mouse clicks for someone.
This is it for today's post.

Below is the Gist and Technet Script Link:

http://gallery.technet.microsoft.com/Get-Machine-Name-of-a-User-a6617e46

Popular posts from this blog

Test connectivity via a specific network interface

Recently while working on a Private cloud implementation, I came across a scenario where I needed to test connectivity of a node to the AD/DNS via multiple network adapters.  Many of us would know that having multiple network routes is usually done to take care of redundancy. So that if a network adapter goes down, one can use the other network interface to reach out to the node. In order to make it easy for everyone to follow along, below is an analogy for the above scenario: My laptop has multiple network adapters (say Wi-Fi and Ethernet) connected to the same network. Now how do I test connectivity to a Server on the network only over say Wi-Fi network adapter?

PowerShell + SCCM : Run CM cmdlets remotely

Today I saw a tweet about using implicit remoting to load the Configuration Manager on my machine by Justin Mathews . It caught my eye as I have never really tried it, but theoretically it can be done. Note - The second tweet says "Cannot find a provider with the name CMSite", resolution to which is in the Troubleshooting section at the end.

Azure DevOps Tips & Tricks - Find private REST APIs

Original source -  Azure DevOps Tip - Find private APIs Often working with Azure DevOps, I hit a wall trying to automate some tasks but there are no REST API's made public yet. It was one of those task of automating creation of Environments in multi-stage YAML based pipelines in AzDO. Quick research reveals that this has been requested in uservoice  (please upvote). Let's see one of the very simple ways to discover some of these APIs.