如何忽略文件夹名称中的空格和特殊字符

How ignore spaces and special characters in folder names

我们将存储共享为 \192.168.1.26\e$。有几个文件夹(全部共享)供不同部门使用。我想让 ownersusers 有权访问这些共享文件夹中的文件夹。

我为此创建了这个 PowerShell 脚本:

$path = "\192.168.1.26\e$\"
$getlist = Get-ChildItem -Path \192.1681.26\e$ | Select-Object Name
foreach ($folder in $getlist) {
    $out = '\192.168.1.26\e$\{0}' -f $folder.name
    #write-host $out
    #prep arguments
    $getargs = $out,'-ad | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,accesstostring | fl'
    write-host $getargs
    gci $getargs
}

$getlist = Get-ChildItem -Path \192.1681.26\e$ | Select-Object Name 我列出了所有文件夹。但是,某些文件夹名称包含空格和特殊字符,这会导致脚本的其余部分失败。

示例:

It-&-Network
MIS
Account
Sales Reports
Market Report
DATA

当我运行下面的命令时,它按预期工作

gci \192.168.1.26\e$\MIS | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,group,accesstostring | fl

但是我在执行此命令时遇到错误:

gci \192.168.1.26\e$\Sales Reports | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,group,accesstostring | fl

有什么办法可以解决这个问题吗?

解决方案来自

$path = '\localhost\f$\tmp'
$getlist = Get-ChildItem -Path $path | Select-Object Name
foreach ($folder in $getlist) {
    $out = '"{0}\{1}"' -f $path,$folder.name 
    $getargs = $out
    write-host $getargs
    $extraparam = '-ad | where {$_.psiscontainer -eq $true} | get-acl | Select-Object path,owner,accesstostring | fl'
    $run_command = 'gci {0} {1}' -f $getargs,$extraparam
    write-host $run_command
    Invoke-Expression $run_command
}

也可以帮到你 - https://ss64.com/ps/syntax-esc.html 为本地磁盘示例更新,在我的机器上工作。