1

I'm writing a powershell script that creates folders and rights from a csv list. the script is almost done, only one issue I can't seem to get around. one column is for account names (for the share rights), when I enter on name it works, when I go for more than one it doesn't, now when I try it manulally it works, the only problem is with powershell understanding the cell value in that column the cell value looks like this "Account1" , "Account2" now this works just fine when I type it in manually, it just seems as if powershell isn't interpreting the coma as a seperator when it's imported from the list, I guess it's being interpreted as a part of a string. This is the share rights Code New-SmbShare -Name $dir1 -Path $loc1 -FullAccess $share1 this is the error I get when I run the Script "No mapping between account names and security IDs was done" can someone please offer a solution? Thanks

mklement0
  • 382,024
  • 64
  • 607
  • 775
A_JE
  • 23
  • 5
  • 1
    You're correct that PowerShell interprets the contents as a string literal, it won't convert it to an array for you automatically. But you can easily do that yourself like this: '"abcd","efg"'.split(",") | %{$_.trim('"')} – JonC May 14 '20 at 12:16
  • This is a consequence of the fact that there is a many-to-many relationship between folders and users. You would have an analogous problem if you tried to do the same thing in a single SQL table. – Walter Mitty May 21 '20 at 11:32

1 Answers1

0

New-SmbShare's -FullAccess parameter is typed as [string[]], i.e. as an array of strings (verify with Get-Help New-SmbShare -Parameter FullAccess).

However, in your case "Account1" , "Account2" is the verbatim content of a single string (as you would get with $share1 = '"Account1" , "Account2"') that can be interpreted as an array, but you'll have to do that interpretation yourself, as JonC points out:

New-SmbShare -Name $dir1 -Path $loc1 -FullAccess ($share1 -split '[", ]' -ne '')
  • $share1 -split '[", ]' splits the string by any occurrence of characters ", , or   (space) into tokens; note that this assumes that account names have no embedded spaces, but that is usually safe to assume.

    • You can read more about the regex-based -split operator in this answer.
  • -ne '' filters out empty-string tokens from the resulting array, so that you'll end up with array 'Account1', 'Account2', as intended.


Note that you may be tempted to use Invoke-Expression $share1, but Invoke-Expression should generally be the last resort, due to its security risks: only ever use it on input you either provided yourself or fully trust - see this answer

mklement0
  • 382,024
  • 64
  • 607
  • 775