0

I am using this code as a means of validating a user or group in advance of setting permissions on a folder and in that aspect it works great.

However, somehow the If is returning a 0. If I remove out the If I don't get a 0 echo to console. If I just rem out the add within the if I still get the 0. So it's definitely something in the if. But I have never seen any info returned like this when part of the If.

try {
   if (-not ((New-Object System.Security.Principal.NTAccount($principal)).Translate([System.Security.Principal.SecurityIdentifier]))) {
       $filePermissions.error.Add("'$principal' is not a valid user or group")
   }
} catch {
   $filePermissions.error.Add("'$principal' is not a valid user or group")
}

I also tried adding some > $nulls in the if, like this

if (-not ((New-Object System.Security.Principal.NTAccount($principal) > $null).Translate([System.Security.Principal.SecurityIdentifier]) > $null)) {

and I still see the 0. Obviously something fundamental is happening, related to New-Object I suspect, but I am really not understanding it.

Troyer
  • 6,765
  • 3
  • 34
  • 62
Gordon
  • 6,257
  • 6
  • 36
  • 89
  • 3
    `[void]$filePermissions.error.Add(...)` – Ansgar Wiechers Sep 12 '17 at 12:06
  • Weird. I have using that Add approach for a while now, and never seen the 0. But now that i think about it, everywhere else may have been done as a run script, rather than code executed in the ISE. Might that account for the appliance of new behavior? – Gordon Sep 12 '17 at 12:09
  • 1
    Assuming that `$filePermissions.error` is an arraylist or similar collection the `Add()` method should always have returned the index at which the new element was added. – Ansgar Wiechers Sep 12 '17 at 12:14
  • Hmm. I'll have to explore this some more. Either the way I am using it elsewhere obscures that, or I have just missed it the whole time! But, good to learn some more subtleties. More and more I am finding I need to move beyond pure PowerShell, and I have lots to learn on that front. – Gordon Sep 12 '17 at 12:19
  • I wonder, in most cases where I want to suppress output I have used > $null rather than [void]. In this context, is there a difference? I am tempted to keep sending to $null in pure PowerShell situations, and use [void] in API stuff, but I wonder if there is an argument against that approach? – Gordon Sep 12 '17 at 12:22
  • Also, a nuance. I had been using = [Collections.Generic.List[String]]@(), but I recently learned that = New-Object Collections.ArrayList is superior in general, and the only option when the list is also part of a hash table. So maybe that explains the difference in behavior? – Gordon Sep 12 '17 at 12:29
  • 2
    I'm not sure I'd call `ArrayList` superior, but a distinct difference in behavior is exactly what you're seeing. `ArrayList.Add()` emits the insertion index, `List[T].Add()` doesn't – Mathias R. Jessen Sep 12 '17 at 12:33
  • "Superior" in the sense of not dealing with generics, and having a nicer implementation in a hash table, as discussed here (https://stackoverflow.com/questions/46115019/generic-list-in-a-hash-table). But since this will ALWAYS be a string, I'm not sure that typing isn't a good idea. But this is quickly turning into a new question I think. ;) – Gordon Sep 12 '17 at 13:11
  • And, since I just found this comment that ArrayList is deprecated in favor of List(T), I may just reverse course and stick with List[T], which then avoids the index being returned. https://stackoverflow.com/questions/2309694/arraylist-vs-list-in-c-sharp#2309699 – Gordon Sep 12 '17 at 13:43

0 Answers0