I am trying to write a PowerShell script to set an autologon account on a whole whack of computers, the issue we have is that the computers are in a variety of departments and each department has their own autologon account with unique password.
My initial thoughts were to get the script to lookup a CSV file with the account details in it but am running into some trouble doing so. to make matters worse the computer names are <PREFIX>-<ASSETNUMBER>
so the csv would look something like:
CN,UN,PW
Purch,buyme,yuiop
HR,fireme,qwert
IT,fixmeplease,qazwsxedc
Manage,bossme,password
when the computernames are something like:
Purch-12345
HR-23456
IT-34567
Manage-45678
I have managed to split the computername
($Prefix = $env:COMPUTERNAME.split("-")[0])
and am running this to search the csv:
$csv = Import-Csv .\import.csv
$UserName = $csv.UN | where {$csv.CN -eq $prefix}
but all it does is list all the usernames in the table and not the one that corresponds to the $prefix variable
Google is not helping mainly because i don't know the terminology to use to search for the question
The pipe operator |
acts a bit like a loop - it's enumerate the input objects and feed them, one-by-one, to the downstream cmdlet (in this case Where-Object
).
You'll want to iterate over the entire $csv
collection (so that you have access to both the UN
and CN
properties on each object), and then refer to each item in the Where-Object
filter block with the special variable $_
:
$UserName = $csv |Where-Object {$_.CN -eq $prefix} |ForEach-Object UN
The ForEach-Object
call at the end will dereference the UN
property on each object that came through the pipeline, and you should thus be left with the raw username string.