0
votes

I am trying to change access permissions for a list of files (recursively):

$global:unc = "\\.\"   # unc-format
$files = Get-ChildItem c:\Users\loli\test\ -Filter *.txt -Recurse -Force |
         % { $_.FullName }
$username = Read-Host

giveAccess $files $username

function giveAccess ( $files, $username ) {
    foreach ( $file in $files) {
        icacls ($unc+$dat) /grant $username:F
    }
}

Unfortunatelly, I get the error:

icacls : Ungültiger Parameter: "/grant"
In Zeile:9 Zeichen:13
+             icacls ($unc+$dat) /grant $username:F
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (Ungültiger Parameter: "/grant":String) [], RemoteException
    + FullyQualifiedErrorId : NativeCommandError

Apparently it's complaining about the string of $username, but I don't know how I could change it.

1

1 Answers

4
votes

$username:F is clashing with other PowerShell syntax; a variable with a colon in the name already has a meaning. It's a specific PSProvider or scope, e.g. $variable:PSHome is an environment variable, $global:myCounter is a global scope variable.

$username:F is one variable name, and it's not set, so it evaluates to $null and icacls complains.

Work around it by breaking the two things apart somehow:

icacls foo.txt /grant "$($username):F"

icacls foo.txt /grant $username":F"

icacls foo.txt /grant ($username + ":F")