# Cannot validate argument on parameter 'Identity'. The argument is null or empty.

Guys.

I have this Powershell script that i am running as part of a ARS scheduled task. When the schedule task runs i see the below error. However if i run the script outside of ARS then it runs fine.

Any ideas?

PS>TerminatingError(Set-QADUser): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
>> set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPostGet' \$Request
PS>set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPreSearch' \$Request
PS>set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPostGet' \$Request

```\$AzureLocation = \$Task.DirObj.Parameters.Value("AzureLocation").Value

\$date = Get-Date
\$date = \$date.ToString("dd-MMM-yyyy")
\$time = Get-Date -Format "dd-MMM-yyyy-HH-mm"
\$CSVLocation = "C:\Temp\OneDrive-Import-Export"

\$logfile = "\$CSVLocation\Logs\\$AzureLocation-OneDrive-\$time.txt"
Start-Transcript -Path \$logfile -Append

#CSV Import Location
\$CSVImport = "\$CSVLocation\\$Date\\$AzureLocation-OneDrive.csv"

Import-Csv \$CSVImport | ForEach-Object {

\$OneDriveSiteUrlContents = [string]\$_.OneDriveSiteUrl

\$TargetUser = Get-QADUser -LdapFilter "(userprincipalname=\$CurrentUser)" | select -expandproperty DN

}```

• For some reason, \$TargetUser is not getting populated.

Just for grins, perhaps add -SearchRoot "DC=MYDomain,DC=Com" in your Get-QADUser command line.  Obviously, this should the actual DN of the domain where your users reside.

• Hi mate.

I have changed that as per your suggestion but still the same issue.

In the CSV files that it is reading there could potentially be one or two accounts that dont actually exist. When its run manually it just displays an error and steps over them moving to the next with no issues. In the scheduled task its like it hits an issue and then just craps out from that point on.

```PS>TerminatingError(Set-QADUser): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
>> TerminatingError(ForEach-Object): "Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again."
Cannot validate argument on parameter 'Identity'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
>> set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPreGet' \$Request
PS>set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPostGet' \$Request
PS>set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPreGet' \$Request
PS>set-psdebug -trace 0
PS>s5367706b-f0e4-4789-92ac-c4b8f57e2faa 'onPostGet' \$Request```

```\$AzureLocation = \$Task.DirObj.Parameters.Value("AzureLocation").Value

\$date = Get-Date
\$date = \$date.ToString("dd-MMM-yyyy")
\$time = Get-Date -Format "dd-MMM-yyyy-HH-mm"
\$CSVLocation = "C:\Temp\OneDrive-Import-Export"

\$logfile = "\$CSVLocation\Logs\\$AzureLocation-OneDrive-\$time.txt"
Start-Transcript -Path \$logfile -Append

#CSV Import Location
\$CSVImport = "\$CSVLocation\\$Date\\$AzureLocation-OneDrive.csv"