2

I was trying to create a simple script that would go to a server and get the acl details of a folder. I tested the command:

Invoke-command -Computername Servername -ScriptBlock {(Get-Acl "\\Server\Folder\user folders").access | ft- auto}

This worked ok. However when I was trying to put it into a script that would allow me to enter the path in via a variable I always get:

Cannot validate argument on parameter 'Path'. The argument is null or empty. Supply an argument that is not null or empty and then 
try the command again.
    + CategoryInfo          : InvalidData: (:) [Get-Acl], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.GetAclCommand

Here is my script:

#get folder permissions from remote computer
$serverName = read-host "Please enter the name of the target server"
$folderPath = "\\server_name\Folder\user folders"
#read-host "Please enter the full path to the target folder"
Invoke-command -ComputerName $serverName -ScriptBlock {(get-acl $folderPath).access | ft -wrap} 

Its probably something very simple, but I'd appreciate the help.

1 Answer 1

1

The issue is because you are trying to use the $folderPath variable, but on the remote computer that variable does not exist.

You will need to pass it through as an argument. There are multiple ways to do so, two such ways are below:

# Add desired variable to ArgumentList and define it as a parameter
Invoke-command -ComputerName $serverName -ArgumentList $folderPath -ScriptBlock {
  param($folderPath)  
  (get-acl $folderPath).access | ft -wrap
}

OR

# In PS ver >= 3.0 we can use 'using'
Invoke-command -ComputerName $serverName $folderPath -ScriptBlock {(get-acl $using:folderPath).access | ft -wrap}
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for your response. I know its simple, but how would I do that?
No worries, I've added two examples of how to achieve this.
thank you so much! the first example solved my issue.
That's a pretty common question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.