Get-SmbShareAccess Cannot validate argument on parameter 'Name'

Hello, I apologize for the grammar, I use the Google translator.
I’m a beginner with powershell, I’m trying to build a simple script to get the Share permissions of some shared folders. The script works fine if I don’t use a variable to indicate the folder.
If I use the $ namefolder variable instead, it gives me an error.
The script is:

$s = New-PsSession -ComputerName QTPSRV-01P
##Invoke-Command -Session $s {Get-SmbShare} | export-csv D:\Export\share.txt
$namefolder = 'share1'
$namefolder
Invoke-Command -Session $s {Get-SmbShareAccess $namefolder} ##| export-csv D:\Export\test1.txt

And the result is:

Cannot validate argument on parameter ‘Name’. The argument is null. Provide a valid value for the argument, and then try running the command again.

    • CategoryInfo : InvalidData: (:slight_smile: [Get-SmbShareAccess], ParameterBindingValidationException*
    • FullyQualifiedErrorId : ParameterArgumentValidationError,Get-SmbShareAccess*
    • PSComputerName : QTPSRV-01P*

How am I wrong to write the $ namefolder variable?
Thank you very much!!

1 Like

variable $namefolder will not be accessible inside the scriptblock and in remote until explicitly passed. You can either use $Using prefix or Parameter blocks.

Using: Invoke-Command -Session $S -ScriptBlock {Get-SMBShareAccess $Using:namefolder}

Param block:

Invoke-Command -Session $S -ScriptBlock {
  Param($Share)
  Get-SMBShareAccess $Share
} -ArgumentList $namefolder
1 Like

Thank you very much kvprasoon, you made it clear to me why, now it works !!