mercredi 28 octobre 2015

Powershell: How to throw an error if a CSV entry is blank

I've written an extensive script that runs through an AD termination process, and the script can obtain the necessary information from a CSV. How do I make it so that it errors out if the entry is blank in the CSV? I've tried putting in Try-Catch, If-Else, everything that I know how to do. I've tried changing the error action, and I can get it to throw system generated errors (ex. "Cannot bind parameter "Identity" to the target..."), but I cannot get it to do what I want. Please see the code example below:

(Yes, I know that I'm duplicating values. This of importance later on in the script, and not the part I'm having issues with)

    $owner = $user.'Network User ID'}
    $loginID = $user.'Network User ID'
    $Identity = Get-ADUser -Identity $owner -Properties Displayname |Select-Object -ExpandProperty Displayname 
    $manager = $user.'Provide Inbox Access To'
    $NewOwner = $user.'Provide users email group ownership to'
    $NewOwnerID = $User.'Provide users email group ownership To'

What I need it to do is throw an error if ANY entry in the CSV is blank, and terminate. The most promising idea that I tried was:

    If ($Owner -eq $Null)
    {
    Write-Host "Invalid entry, the Network User ID field cannot be blank"
    Write-Host "Press Enter to Exit..."
    Exit
    }
    Else
    {
    #Do everything else
    }

But even that still fails.

In summary, what I need to do is throw a custom terminating error if an entry in the CSV is blank.

Any help is greatly appreciated!

Aucun commentaire:

Enregistrer un commentaire