Scan-PC

Let’s start with an easy one. Few years back, I had a colleague who found it too difficult to file out an EXCEL spreadsheet when assigning or when someone return a PC. Consequence, this was a problem to keep an updated asset list in the company. First, I told him, OK, you print a word document where you will fill ‘name, firstname,…’ and get it signed by user. Then you give me the paper, and I’ll take care about our XLS file.

That was also too difficult for him… Unfortunalty (for him), he got fired… not only keeping a XLS file or word document up-to-date was a problem, but just coming to work was for him at the end…

lazy

Anyway, I was also tired of that paperwork just to keep my asset list up-to-date. So what you do when you tired and lazy of doing something? -answer, you build a script that will do it for you 🙂 . And here it is, I’ll explain each function

 


function Test-Ping{

if (Test-Connection $ip -quiet -count 2){write-host -ForegroundColor green "ping $IP OK" ; get-wmi}
else {write-host -ForegroundColor Red "ping $ip not oK, aborting"}
}

$ip = Read-Host "Please enter PC IP address or computername"
Test-Ping

Well, pretty obvious, right? just pinging IP or computername. If there are 2 replies to ping, then write something and go to get-wmi function. I use this function in many of my script, when dealing with remote computer.


function get-wmi
{
try
{
Write-Host "get wmi info for $ip"
$SN = get-wmiobject -computername $ip -class win32_bios -ErrorAction Stop
$DATA = Get-WmiObject -computername $ip -Class Win32_Computersystem | select model,username,name -ErrorAction stop
}
catch{Write-Host "error getting wmi info, aborting for $ip" ; exit}

set-info
}

This fonction will get me serial number of remote machine, model, username of the user connect to it at the moment.

Try/catch is used to catch errors if any


function set-info
{
try
{
$UserName = ($data.username).Substring(15)
$User = get-aduser $UserName
Write-Host "found ad user"
}
catch
{write-host "error finding user"
$username = "not found"
}

$scandate = get-date -Format "dd/MM/yy"
if ($data.model -like "OptiPlex*"){$PCType = "Desktop"}
else {$PCType = "Laptop"}
$ST = $SN.SerialNumber
$hItemDetails = New-Object -TypeName psobject -Property @{

'Group Desc' = $PCType
'Brand Desc' = $data.model
'Mfg Service Tag' = $SN.serialnumber
Username = $UserName
'Computer Name' = $data.name
'HR to Confirm' = $user.Surname
'Display Name' = $user.Name
'Firstname' = $user.givenname
'Scan Date' = $scandate
}

$import = import-csv -Delimiter ";" $file | where {$_."Mfg Service Tag" -notmatch "$ST"}
$import | Export-Csv -Delimiter ";" -NoTypeInformation $file

$hItemDetails | Select-Object -Property 'Group Desc','Brand Desc','Mfg Service Tag','Computer Name','Username','HR to Confirm','Display Name','Scan Date'|Export-CSV -NoTypeInformation -Delimiter ";" -Append -Force $file

if ((Test-Path $tempcsv) -like $true){Remove-Item $tempcsv -force}
$hItemDetails | Select-Object -Property 'Group Desc','Brand Desc','Mfg Service Tag','Computer Name','Username','HR to Confirm','Display Name','Firstname','Scan Date'|Export-CSV -NoTypeInformation -Delimiter ";" -Append -Force $tempcsv
$hItemDetails | Select-Object -Property 'Group Desc','Brand Desc','Mfg Service Tag','Computer Name','Username','HR to Confirm','Firstname','Display Name','Scan Date'|Export-CSV -NoTypeInformation -Delimiter ";" -Append -Force $fullreport

& '.\PC.xlsx'
}

Last function will get information about user AD account in order to get user first,last and displayname. It will tell if this PC is a laptop or desktop.

Finally, I’m creating a new PSobject to add all information I want in my XLS file.

The first import/export I’m doing is to remove the PC from my CSV file if it exists, using the Serial number field as a filter

I then add the info to a TEMPCSV file, deleting the file first if it exists. Finally I add the info to another CSV file, this one contains all PC I scan in time, which can be useful to track who is this PC assigned to, who was it assigned to before…

Finally, I open an XLSX file which I will print to make user sign off. It will populate field like lastname, username,model,SN with the CSV created earlier.

last to mention :

$file= “.\PCscan.csv”
$fullreport = “.\PCScan_Fullreport.csv”
$tempcsv = “.\PCScanC\Temp_files\temp.csv”

Another job done by SPS 🙂

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s