Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Powershell 无法覆盖变量false,因为它是只读或常量_Powershell_Error Handling - Fatal编程技术网

Powershell 无法覆盖变量false,因为它是只读或常量

Powershell 无法覆盖变量false,因为它是只读或常量,powershell,error-handling,Powershell,Error Handling,以下是我的代码,其中包括相关部分: Get-ChildItem -LiteralPath $somepath -Directory | ForEach-Object { Step $_.FullName } function Step { Param([string]$subfolder) $folders = Get-ChildItem -LiteralPath $subfolder -Directory -ErrorVariable $HasError -Erro

以下是我的代码,其中包括相关部分:

Get-ChildItem -LiteralPath $somepath -Directory | ForEach-Object {
    Step $_.FullName
}

function Step {
    Param([string]$subfolder)

    $folders = Get-ChildItem -LiteralPath $subfolder -Directory -ErrorVariable $HasError -ErrorAction SilentlyContinue

    if ($HasError) {
        Write-Host $_.FullName "|" $HasError.Message
        return
    } else {
        $hasError, $inactive = FolderInactive $_.FullName
        if ($hasError) {
            #do nothing
        } else {
            if ($inactive) {
                SetFolderItemsReadOnly $_.FullName
            }
        }
    }

    if ($folders) {
        $folders | ForEach-Object {
            Step $_.FullName
        }
    }
}

function SetFolderItemsReadOnly {
    Param([string]$Path)

    $files = Get-ChildItem -LiteralPath $Path -File -ErrorAction Stop

    foreach ($file in $files) {
        try {
            Set-ItemProperty -LiteralPath $file.FullName -Name IsReadOnly -Value $true
        } catch {
            Write-Host $file.FullName " | " $_.Exception.Message
        }       
    }
}
我在
SetFolderItemsReadOnly
函数中的
setItemProperty
中遇到一些错误,即

异常设置“IsReadOnly”:“对路径的访问被拒绝。”

这是由于某些安全权限造成的。然而,在终端中打印此错误后,我还得到一个巨大的红色错误,如下所示:

Cannot overwrite variable false because it is read-only or constant. At pathtoscript:55 char:5 + $folders = Get-ChildItem -LiteralPath $subfolder -Directory -ErrorVariable ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : WriteError: (false:String) [], SessionStateUnauthorizedAccessException + FullyQualifiedErrorId : VariableNotWritable 无法覆盖变量false,因为它是只读或常量。 在pathtoscript:55字符处:5 +$folders=Get ChildItem-LiteralPath$subfolder-Directory-ErrorVariable。。。 + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +CategoryInfo:WriteError:(false:String)[],SessionStateUnauthorizedAccessException +FullyQualifiedErrorId:VariableNotWritable 为什么会发生此错误?

如前所述,参数
-ErrorVariable
只需要变量的名称,而不需要前导的
$
。此外,此变量的用途不是作为是否发生错误的布尔指示器(PowerShell已通过
$?
提供此信息),而是接收实际的错误对象

从:

-ErrorVariable[+]
别名:
ev

将有关命令的错误消息存储在指定变量和
$error
自动变量中。有关详细信息,请键入以下命令:

获取有关自动变量的帮助
默认情况下,新错误消息将覆盖已存储在变量中的错误消息。要将错误消息附加到变量内容,请在变量名称前键入加号(+)

例如,以下命令创建
$a
变量,然后在其中存储任何错误:

Get进程-Id 6-ErrorVariable a
以下命令将所有错误消息添加到
$a
变量:

Get进程-Id 2-ErrorVariable+a

您是否尝试以管理员身份运行脚本?我会发现$HasError变量已初始化为true,并在get ChildItem调用期间进行计算。在-ErrorVariable调用中,尝试删除名称前面的“$”。@MartinBrandl我有。我不知道为什么,有些文件甚至不能被我们的管理员均匀地写入。但是它应该只打印“异常设置”IsReadOnly“等”,但有时它也会出现这种奇怪的错误。@DavidBrabant我的伙计!成功了!错误消失了!今天早上咖啡不够。“我猜$HasError变量已初始化为FALSE,等等。”