简体   繁体   中英

How can I pass a comma seperated list of values within PowerShell as a parameter to a CmdLet using splatting with hash tables?

I would like to use "splatting" with hash tables to pass variable parameters to a CmdLet. I want to modify the following command:

New-SmbShare -Name Share$ -Path C:\Folder -ChangeAccess MACHINE\User1

If I do this only for the ChangeAccess parameter , I end up with the following result:

$Args = @{ChangeAccess = 'MACHINE\User1'}
New-SmbShare -Name Share$ -Path C:\Folder @Args

This works very well.

The parameter ChangeAcces accepts a comma seperated list of users, so if I would like to give two users the change permission on the share, I would use the following command:

New-SmbShare -Name Share$ -Path C:\Folder -ChangeAccess MACHINE\User1,MACHINE\User2

If I replace this command the same way with splatting, I end up with this:

$Args = @{ChangeAccess = 'MACHINE\User1,MACHINE\User2'}
New-SmbShare -Name Share$ -Path C:\Folder @Args

Now the command fails with a "CimException". Any ideas how I can solve this?

The parameter -ChangeAccess <string[]> accepts a string or an array of strings. This is why is was failing with ChangeAccess = 'MACHINE\\User1,MACHINE\\User2' It's seeing the whole string as one user, you will need to use the array option for multiple users:

$Args = @{ChangeAccess = 'MACHINE\User1','MACHINE\User2'}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM