D
D
Dmitry Shumov2020-05-25 13:53:23
PowerShell
Dmitry Shumov, 2020-05-25 13:53:23

Why doesn't the PowerShell script work when there is no value in the attribute?

Greetings!
Somehow, I relaxed a little during self-isolation :( - I'm dumb a little.
Tell me, tell me what the problem could be:
There is a script that logs the user's login / logout to the server:

Param(
[Parameter(Position=0)]
[ValidateSet("Logon","Logoff","Unknown")]
[string]$Status="Unknown"
)
 
#no spaces in the filter
[adsisearcher]$searcher="samaccountname=$env:username"
#find the current user
$find = $searcher.FindOne()
#get the user object
[adsi]$user = $find.Path
#define a string to indicate status
$note= @("{0} {1} to {2}" -f (Get-Date),$status.ToUpper(),$env:computername)
[array]$NewVals = @()
$NewVals = $note + ($user.GetEx('url'))[0..10]
#DEBUG, output values in ISE
#$NewVals |  out-host
#update the user property
$user.PutEx(2, 'url', $NewVals ) 
#commit the change
$user.SetInfo()
#DEBUG, output values in ISE
#$user.GetEx('url')

The script writes to the url attribute when and where the user went. So, if this attribute has no values ​​\u200b\u200bin general, then it gives an error:
Exception calling "GetEx" with "1" argument(s): "The directory property cannot be found in the cache.
"
At \\nasta.local\SYSVOL\nasta.local\Policies\{A2FAB552-E94C-4675-9F3A-B50D0EB03516}\User\Scripts\Logon\Set-UserStatus.ps1:16 char:1
+ $NewVals = $note + ($user.GetEx('url'))[0..10]
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : CatchFromBaseAdapterMethodInvokeTI

If you manually put down any value there, then the script works fine and the necessary entry is added there.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
MaxKozlov, 2020-05-25
@dshumov

Apparently because if the attribute has no values ​​\u200b\u200bin general, then a particular user does not have such an attribute. You can verify this through DSA or ADISEDIT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question