[PowerShell] Import CSV user Script (original) (raw)

Hi guys, I have the following script that does the following: it takes a list of users from CSV files and searches for them in AD. If they exist, it displays them on the screen in cyan; if they don’t, it displays them in red.

I’m doing this as practice for something bigger. But I already have my doubts and I want to share them with the community to see if I’m on the right track.

function Test-ADUser {
    param([Parameter(Mandatory)][String]$CSUR)
    return (New-Object System.DirectoryServices.DirectorySearcher([ADSI]'LDAP://DC=corp,DC=r0,DC=com', "(&(objectClass=user)(sAMAccountName=$CSUR))")).FindOne()
}

function Test-LenCSV {
    param([Parameter(Mandatory)][String]$CSVPath)
    return (Import-Csv -Path $CSVPath -Header NotCount | Measure-Object).Count
}

function Get-LSIADCsvFiles {
    param([Parameter(Mandatory)][String]$Path)

    $csvFiles = @()
    <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>s</mi><mi>v</mi><mi>P</mi><mi>a</mi><mi>t</mi><mi>h</mi><mo>=</mo><mi>G</mi><mi>e</mi><mi>t</mi><mo>−</mo><mi>C</mi><mi>h</mi><mi>i</mi><mi>l</mi><mi>d</mi><mi>I</mi><mi>t</mi><mi>e</mi><mi>m</mi><mo>−</mo><mi>P</mi><mi>a</mi><mi>t</mi><mi>h</mi></mrow><annotation encoding="application/x-tex">csvPath = Get-ChildItem -Path </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal">cs</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span><span class="mspace" style="margin-right:0.2778em;"></span></span><span class="base"><span class="strut" style="height:0.7667em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">G</span><span class="mord mathnormal">e</span><span class="mord mathnormal">t</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal" style="margin-right:0.07153em;">C</span><span class="mord mathnormal">hi</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">d</span><span class="mord mathnormal" style="margin-right:0.07847em;">I</span><span class="mord mathnormal">t</span><span class="mord mathnormal">e</span><span class="mord mathnormal">m</span><span class="mspace" style="margin-right:0.2222em;"></span><span class="mbin">−</span><span class="mspace" style="margin-right:0.2222em;"></span></span><span class="base"><span class="strut" style="height:0.6944em;"></span><span class="mord mathnormal" style="margin-right:0.13889em;">P</span><span class="mord mathnormal">a</span><span class="mord mathnormal">t</span><span class="mord mathnormal">h</span></span></span></span>Path -Depth 0 -Filter "List_*"

    
    ForEach ($csv in $csvPath) {
        <span class="katex"><span class="katex-mathml"><math xmlns="http://www.w3.org/1998/Math/MathML"><semantics><mrow><mi>c</mi><mi>s</mi><mi>v</mi><mi>F</mi><mi>i</mi><mi>l</mi><mi>e</mi><mi>s</mi><mo>+</mo><mo>=</mo></mrow><annotation encoding="application/x-tex">csvFiles += </annotation></semantics></math></span><span class="katex-html" aria-hidden="true"><span class="base"><span class="strut" style="height:0.7778em;vertical-align:-0.0833em;"></span><span class="mord mathnormal">cs</span><span class="mord mathnormal" style="margin-right:0.03588em;">v</span><span class="mord mathnormal" style="margin-right:0.13889em;">F</span><span class="mord mathnormal">i</span><span class="mord mathnormal" style="margin-right:0.01968em;">l</span><span class="mord mathnormal">es</span><span class="mord">+</span><span class="mspace" style="margin-right:0.2778em;"></span><span class="mrel">=</span></span></span></span>csv.FullName
    }

    return $csvFiles
}

if (!(Test-NetConnection -ComputerName vdc0.corp.r0.com -Port 445).TcpTestSucceeded) {

    exit

}

if (!((Get-PSDrive -Name "CSVShare" -ErrorAction SilentlyContinue).Name)) {
    New-PSDrive -Name "CSVShare" -Root "\\vdc0.corp.r0.com\test" -PSProvider "FileSystem"
}
else {
    exit
}

$counter = Get-LSIADCsvFiles "CSVShare:\"

if ($counter.Count) {

    foreach ($count in $counter) {
        if ((Test-LenCSV($count)) -gt 60) {
            Remove-PSDrive -Name CSVShare -Confirm:$false
            exit
        }
    }

    foreach ($count in $counter) {

        Import-CSV -Path $count -Header HOBJ | Foreach-Object {

            if (Test-ADUser(($_.HOBJ -replace '[^\x00-\x7F]', '') -replace '\s', '')) {

                Write-Host $_.HOBJ  -ForegroundColor Cyan
            }
            else {
                Write-Host $_.HOBJ  -ForegroundColor Red
            }
        }
    }
}
Remove-PSDrive -Name CSVShare -Confirm:$false

I describe the functions:

Test-ADUser: Single parameter function which is a String, you have to pass the sAMAccountName from the CSV and it searches for it in my AD, it returns true or false.

Test-LenCSV:The only string parameter is the absolute path to the CSV and counts the number of lines it has.

Get-LSIADCsvFiles:The absolute path of the CSV is passed and it is placed in an array since it can see more than one file, filtered by LIST_*

First of all, I haven’t considered a logging function for the exits of the else statements yet. But that doesn’t worry me right now.

In the first if statement, I run a connection test to the server where the CSVs are stored on port 445, which is the SMB port. If it’s not operational, the script ends.

If it is, in the second if statement, I mount the shared resource in a PSDRIVE so I can access the files more easily later. If the PSDRIVE already exists, the script ends.

I create a variable called $counter to store the CSV paths.

In the first FOREACH statement, I call the function to count the number of lines in the CSVs. If it’s greater than 60, the script ends.

The second FOREACH statement executes as many CSV files as we have. I call the test user function to see if it exists in my AD, and it displays them on the screen. Finally we disassemble the PSDRIVE and finish the script.

Right now I have the following questions:

  1. The Get-LSIADCsvFiles function is well designed, is there any better method to pass a list of files?
    2.When I do the … you can see that I eliminate all the BYTES that do not belong to ASCII values ​​since the CSVs I have are “dirty” I show a capture of a line.

image

As you can see it starts with values ​​0XFF, 0XFE and the name is separated by NULL values, in reality the user line is: 43434256715K Apparently, Import-CSV isn’t affected by that “dirt,” and the -replace commands in the import are unnecessary. Is that true? I haven’t encountered any errors.

Finally, would you suggest any improvements?

Thanks.