I have wanted to share some of the more troublesome powershell scripts that I have had to find or build over the years.
Many companies have a need for gathering the serial number of the monitor via a script. For years I know many used the same vbs script but I wanted a ps script. So here is one that works and is in production.
I will explain it as thats very frustrating for many who are just learning that no one really explains the script.
I will paste the script and fill in the instructions with comments in the code.
#created on 10/27/2015
#written by salve terram but function was from internet search.
you wont need to modify this function part here. down below i will show you were where you need to modify for your place
Function Get-MonitorInfo
{
[CmdletBinding()]
Param
(
[Parameter(
Position=0,
ValueFromPipeLine=$true,
ValueFromPipeLineByPropertyName=$true)]
[alias("CN","MachineName","Name","Computer")]
[string[]]$ComputerName = $ENV:ComputerName
)
found the way to get the computername in a very convoluted way but hey that works.
Begin {
$pipelineInput = -not $PSBoundParameters.ContainsKey('ComputerName')
}
Process
{
uses wmi to get the monitor info into an array
Function DoWork([string]$ComputerName) {
$ActiveMonitors = Get-WmiObject -Namespace root\wmi -Class wmiMonitorID -ComputerName $ComputerName
this is the array
$monitorInfo = @()
foreach statement. nothing crazy here
foreach ($monitor in $ActiveMonitors)
{
$mon = $null
conversion of the objects to char type for those who need it. also created the $mon object which is where the info is added
$mon = New-Object PSObject -Property @{
ManufacturerName=($monitor.ManufacturerName | % {[char]$_}) -join ''
ProductCodeID=($monitor.ProductCodeID | % {[char]$_}) -join ''
SerialNumberID=($monitor.SerialNumberID | % {[char]$_}) -join ''
UserFriendlyName=($monitor.UserFriendlyName | % {[char]$_}) -join ''
ComputerName=$ComputerName
WeekOfManufacture=$monitor.WeekOfManufacture
YearOfManufacture=$monitor.YearOfManufacture}
this is the variable that is accepting all of the data from the array and keeps adding into it
$monitorInfo += $mon
}
Write-Output $monitorInfo
}
since that was a function about here is the actual work.
if pipelineinput is true, meaning does data exist then continue.
if ($pipelineInput) {
run the DoWork function
DoWork($ComputerName)
} else {
foreach ($item in $ComputerName) {
DoWork($item)
}
}
}
}
#now that we are out of the functions this is the part of the script that you will need to modify.
now you are calling the main function above
Get-MonitorInfo
this is just where i kept my logs and such so if you dont want this then remove or edit the paths.
if(!(test-path("C:\Packages\logonscript"))){
mkdir "C:\Packages\logonscript"
}
$logondir = "C:\Packages\logonscript"
$getmon = Get-MonitorInfo
if (Test-Path("$logondir\monitor.txt")){
ri $logondir\monitor.txt
}
i found that we had monitors that had variable length serial numbers and that threw the issue with reporting this #off. I had to write the below parts to check for length of the serial number and adjust accordingly.
now you might not need this at all depending on your environment but some parts of it are good to see in use.
foreach ( $mon in $getmon){
$unusmon = $mon.SerialNumberID
$unusmon.length
i had to insert these blanks based on length
$blanks = "0
00
0"
$hey = $unusmon -replace $blanks, "n" $hey.length if ($hey.Length -eq "16") { $blanks = "
00
0"
$hey = $unusmon -replace $blanks, "n" if ($hey.Length -eq "16") { $blanks = "
00" $hey = $unusmon -replace $blanks, "
n"
}
}
i found i needed to force the encoding to ASCII so that the website that interpreted the form wouldnt blow up
$unusmon -replace $blanks, "`n" | out-file -append $logondir\monitor.txt -encoding ASCII
}
So anyways here it is. Maybe this will be useful for someone.
A database of serial numbers and model numbers for all the equipment in an enterprise would be a useful thing for the IT department to have; It could also aleret whne a piece of hardware goes missing, is moved to another machine, or is added.
It wouldn't even have to run often.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit
yup. exactly. i ran this code above weekly. have had this at a few places.
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit