Get computer name based of a username from Lansweeper

In the organization that I am working at it is very common to know names of people. Of course this is not always the case but more often than not I am asking myself when wanting to go into a remote computer, “What is the computer name of Timmy Tim?”.

We are using Lansweeper and of course I love the product and it has so many information stored about what is going on within the infrastructure. One of the things I love is that it is keeping up with what computer people are using. But please if you are in need of a solution to manage Assets then take a look at Lansweeper.

So eventually I built a simple PowerShell script to answer the “What is the computer name of Timmy Tim?” question. The script uses Invoke-Webrequest to request the user page of Lansweeper and scrape information out of it and return only the first / last computer name of the user.

I have included parameters as well and very basic error checking so you can use the script in whatever way you want.

The script is not perfect at all as it fulfilled the needs that I had for it. However feel free to change it however you like. If you have any suggestions be sure to let me know.

<#
.SYNOPSIS
This is a simple Powershell script that queries Lansweeper for what computer they last used based on the input username

.DESCRIPTION
The script will attempt to create an connection to the specified Lansweeper site and uses the supplied Credentials to login.
After that it will retrieve all the links that are found on the queried website and gets the one that has the AssetID inside.
It then expands this found AssetID and outputs the computer name inside it trimmed and in capital.

.EXAMPLE
./Get-ComputerName -Username jjansma -Credential DOMAIN\AccountThatHasAccess

.EXAMPLE
./Get-ComputerName -Username jjansma -Credential DOMAIN\AccountThatHasAccess -Lansweeper "http://lansweeper.website.nl" -Domain "companydomain"
#>

[CmdletBinding()]
param
(
[string]$Username,
[pscredential]$Credential,
[string]$Lansweeper = "lansweeper.intranet.nl",
[string]$Domain = "companydomain"
)

$WebRequest = Invoke-WebRequest "http://$Lansweeper/user.aspx?username=$Username&userdomain=$Domain" -Credential $Credential

if (-Not($WebRequest))
{
Write-Error "The web request failed"
Exit
}

$RetrievedComputerName = ((($WebRequest | Select-Object -ExpandProperty Links | Where-Object href -like "*asset.aspx?AssetID=*")[0]) | Select-Object -ExpandProperty innerHTML).Trim().ToUpper()

if (-Not($RetrievedComputerName))
{
Write-Error "No computer name could be found from the request"
Exit
}

return $RetrievedComputerName

Leave a Comment