Powershell 消息“$文件为只读保护” } Catch{Write Verbose-消息“$File未受保护”-Verbose} #那别忘了关上 $Word.Quit() #结果 #启用Word文档保护 $Word=新对象–comobject Word.Application 尝试 { ($Word.documents.open($File,$false,$false)).ReadOnly 写入警告-消息“$文件为只读保护” } Catch{Write Verbose-消息“$File不受只读保护”-Verbose} #那别忘了关上 $Word.Quit() #结果

Powershell 消息“$文件为只读保护” } Catch{Write Verbose-消息“$File未受保护”-Verbose} #那别忘了关上 $Word.Quit() #结果 #启用Word文档保护 $Word=新对象–comobject Word.Application 尝试 { ($Word.documents.open($File,$false,$false)).ReadOnly 写入警告-消息“$文件为只读保护” } Catch{Write Verbose-消息“$File不受只读保护”-Verbose} #那别忘了关上 $Word.Quit() #结果,powershell,ms-word,Powershell,Ms Word,出于偶然或故意,您可以将两者都设置在一个环境中。我在自动分类场景中遇到过这种情况。这意味着FSRM/RMS/AIP已部署/实施和实施 更新 这是我的工作流程中的一个示例,根据我们的交换,我可以捕获这类东西 Clear-Host $Files | ForEach{ $File = $PSItem "Processing $PSItem" try { Write-Verbose -Message 'Word properti

出于偶然或故意,您可以将两者都设置在一个环境中。我在自动分类场景中遇到过这种情况。这意味着FSRM/RMS/AIP已部署/实施和实施

更新

这是我的工作流程中的一个示例,根据我们的交换,我可以捕获这类东西

Clear-Host
$Files | 
ForEach{
    $File = $PSItem
    "Processing $PSItem"
    try   
    {
        Write-Verbose -Message 'Word properties:
        DocID, FullName, HasPassword, 
        Permission, ReadOnly, Saved, 
        Creator, CurrentRsid, CompatibilityMode' -Verbose

        'DocID', 'FullName', 'HasPassword', 
        'Permission', 'ReadOnly', 'Saved', 
        'Creator', 'CurrentRsid', 'CompatibilityMode' | 
        ForEach {($Word.documents.open($File,$false,$false)).$PSitem}

        Write-Verbose -Message 'File system ReadOnly attribute:' -Verbose
        (Get-ItemProperty $File).IsReadOnly

        Write-Verbose -Message 'Document state' -Verbose
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                        )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
Processing d:\Temp\HSGCopy.docx
VERBOSE: Word properties:
        DocID, FullName, HasPassword, 
        Permission, ReadOnly, Saved, 
        Creator, CurrentRsid, CompatibilityMode
938207550
D:\Temp\HSGCopy.docx
False
True
True
1297307460
12414886
15
VERBOSE: File system ReadOnly attribute:
False
VERBOSE: Document state
What if: Performing the operation "Remove File" on target "D:\Temp\HSGCopy.docx".
#>
清除主机
$Files|
弗雷奇{
$File=$PSItem
“正在处理$PSItem”
尝试
{
写入详细信息-消息的单词属性:
DocID,全名,密码,
权限,只读,已保存,
Creator,CurrentRsid,CompatibilityMode'-Verbose
'DocID'、'FullName'、'HasPassword',
“权限”、“只读”、“已保存”,
“Creator”、“CurrentRsid”、“CompatibilityMode”
ForEach{($Word.documents.open($File,$false,$false))。$PSitem}
写入详细信息-消息“文件系统只读属性:”-详细信息
(获取ItemProperty$文件).IsReadOnly
写入详细信息-消息“文档状态”-详细信息
$TargetFile=(新对象System.IO.FileInfo$PSitem)。打开(
[System.IO.FileMode]::打开,
[System.IO.FileAccess]::读写,
[System.IO.FileShare]::无
)
$TargetFile.Close()
删除项目-路径$PSItem-WhatIf
}
catch[System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
捕获{$PSItem.Exception.Message}
}
#结果

您所说的只读是什么意思?选中的NTFS属性,或者如果文件已在Word进程中打开,则为?@vonPryz。简单地说,如果它是由MSOffice Word配置的。@稍微有些刺耳地说,所有这些都是可能的,有兴趣知道答案,即使它们是基于该标准限制的!与文件被锁定相比,只读和文件级设置之间存在差异。检查使用中的反渗透vs的用例是什么?这两种方法都可以防止您对文档进行更改,但不能打开它们。如果文件系统属性为OS RO set,则必须使用文件系统删除该属性。如果由于正在使用而将其锁定,您可以关闭它,但这样做将丢失所有未保存的更改。因此,随机地这样做是不明智的。我的观点是,你不需要COM来检查Word doc是否是RO。您只需要检查它的属性。这就是GetItemProperty所做的一切。现在,如果您是指文档创建者在文档保护选项中将文档设置为“始终以只读方式打开”,那么请确保这是不同的。OP根本就没有询问邮报中的文档保护问题。在Word中完成任何操作,那么是的,您需要使用COM/DOM来获得它。很高兴您拥有它。进一步的测试表明,这在某种程度上也与
VISIBLE
设置有关。如果为false,则在没有通过PS提示的情况下打开它。我有点模糊,但不管怎样,如果失败,这将“捕获它”,并将所有问题/可见变量设置为true。Epic+1对于接受挑战,我忘记了为什么,但我以前已经检查过docx XML,知道有一种方法可以在没有COM/打开DOCs的情况下进行判断。文档用户、所有者和策略执行人员已经多次从背后攻击我。在我工作过和仍然工作过的所有环境中,我都会确保观察者在适当的位置上进行属性更改等。数据保管人通常不知道数据处于什么状态,然后我接到一个电话,说为什么X或Y是这样。这是高科技的保姆。我的代码只是在文件系统和DOM/COM两个级别上循环遍历属性,因为。查看我的更新,了解我对警报执行的操作,或此类人员提出的问题。
 #Constants
 Clear-Variable ReadOnly
 $missing = [System.Type]::Missing
 $str = ''
 $PASSWD = 'IsPWDProtected?'
 $wdAlertsNone = 0
 $FILENAME = "C:\tmp\readonly.docx"
 $OPENUI = "TRUE"

 #Start Word
 $ObjWord = New-Object -comobject Word.Application
 IF ($OPENUI -eq "FALSE"){$ObjWord.Visible  = $FALSE}ELSE{$ObjWord.Visible  = $TRUE}
 $ObjWord.Application.DisplayAlerts = $wdAlertsNone

 #.Open
 IF (!$ConfirmConversions){$ConfirmConversions = $missing}
 IF (!$ReadOnly){$ReadOnly = $FALSE}
 IF (!$AddToRecentFiles){$AddToRecentFiles = $missing}
 IF (!$PasswordDocument){$PasswordDocument = $PASSWD}
 IF (!$PasswordTemplate){$PasswordTemplate = $PASSWD}
 IF (!$Revert){$Revert = $False} 
 IF (!$WritePasswordDocument){$WritePasswordDocument =  $PASSWD}
 IF (!$WritePasswordTemplate){$WritePasswordTemplate =  $PASSWD} 
 IF (!$Format){$Format = 'wdOpenFormatAuto'} 
 IF (!$Encoding){$Encoding = $missing}
 IF (!$Visible){$Visible = $False} 
 try{$ObjDoc=$ObjWord.documents.open($FILENAME,$ConfirmConversions,$ReadOnly,$AddToRecentFiles,$PasswordDocument,$PasswordTemplate,$Revert,$WritePasswordDocument,$WritePasswordTemplate,$Format,$Encoding,$Visible)}
  catch {
     Write-Error  $_
     Write-Host "Opening Read_Only"
     $ReadOnly = $TRUE
     $Visible = $TRUE
     $ObjDoc=$ObjWord.documents.open($FILENAME,$ConfirmConversions,$ReadOnly,$AddToRecentFiles,$PasswordDocument,$PasswordTemplate,$Revert,$WritePasswordDocument,$WritePasswordTemplate,$Format,$Encoding,$Visible)
 }

#AllDone?
PAUSE
$ObjWord.ActiveDocument.Close(0)
$ObjWord.Quit()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
[gc]::collect()
[gc]::WaitForPendingFinalizers()
sleep 2
PS C:\Users\Admin> C:\tmp\test.ps1
C:\tmp\test.ps1 : The password is incorrect. Word cannot open the document. (C:\tmp\readonly.doc)
    + CategoryInfo          : NotSpecified: (:) [Write-Error], WriteErrorException
    + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException,test.ps1
 
Opening Read_Only
Press Enter to continue...: 
$File = 'd:\Temp\HSGCopy.docx'

# File not in use
Set-ItemProperty -Path $File  -Name IsReadOnly -Value $false
(Get-ItemProperty 'd:\Temp\HSGCopy.docx').IsReadOnly
$File | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
False
What if: Performing the operation "Remove File" on target "D:\Temp\HSGCopy.docx".
#>

# File in use
Set-ItemProperty -Path $File  -Name IsReadOnly -Value $false
(Get-ItemProperty 'd:\Temp\HSGCopy.docx').IsReadOnly
$File | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
False
Exception calling "Open" with "3" argument(s): "The process cannot access the file 'd:\Temp\HSGCopy.docx' because it is being used by another process."
#>


# Change the file attribute
# File not in use
Set-ItemProperty -Path $File  -Name IsReadOnly -Value $true
(Get-ItemProperty 'd:\Temp\HSGCopy.docx').IsReadOnly
$File | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
True
Exception calling "Open" with "3" argument(s): "Access to the path 'd:\Temp\HSGCopy.docx' is denied."
#>

# File in use
Set-ItemProperty -Path $File  -Name IsReadOnly -Value $true
(Get-ItemProperty 'd:\Temp\HSGCopy.docx').IsReadOnly
$File | 
ForEach{
    try   
    {
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                      )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
True
Exception calling "Open" with "3" argument(s): "Access to the path 'd:\Temp\HSGCopy.docx' is denied."
#>
# with Word doc protection off
#>
$Word = New-Object –comobject Word.Application
Try
{
    ($Word.documents.open($File,$false,$false)).ReadOnly
    Write-Warning -Message "$File is protected ReadOnly"
}
Catch {Write-Verbose -Message "$File is not protected" -Verbose}

# then don't forget to close
$Word.Quit()
# Results 
<#
VERBOSE: d:\Temp\HSGCopy.docx is not protected
#>




# With Word doc protection on
$Word = New-Object –comobject Word.Application
Try
{
    ($Word.documents.open($File,$false,$false)).ReadOnly
    Write-Warning -Message "$File is protected ReadOnly"
}
Catch {Write-Verbose -Message "$File is not protected ReadOnly" -Verbose}

# then don't forget to close
$Word.Quit()
# Results 
<#
True
WARNING: d:\Temp\HSGCopy.docx is protected ReadOnly
#>
Clear-Host
$Files | 
ForEach{
    $File = $PSItem
    "Processing $PSItem"
    try   
    {
        Write-Verbose -Message 'Word properties:
        DocID, FullName, HasPassword, 
        Permission, ReadOnly, Saved, 
        Creator, CurrentRsid, CompatibilityMode' -Verbose

        'DocID', 'FullName', 'HasPassword', 
        'Permission', 'ReadOnly', 'Saved', 
        'Creator', 'CurrentRsid', 'CompatibilityMode' | 
        ForEach {($Word.documents.open($File,$false,$false)).$PSitem}

        Write-Verbose -Message 'File system ReadOnly attribute:' -Verbose
        (Get-ItemProperty $File).IsReadOnly

        Write-Verbose -Message 'Document state' -Verbose
        $TargetFile = (New-Object System.IO.FileInfo $PSitem).Open(
                                            [System.IO.FileMode]::Open, 
                                            [System.IO.FileAccess]::ReadWrite, 
                                            [System.IO.FileShare]::None
                        )
        $TargetFile.Close()  
        Remove-Item -Path $PSItem -WhatIf  
    }
    catch [System.Management.Automation.ItemNotFoundException]{$PSItem.Exception.Message}
    catch {$PSItem.Exception.Message}
}
# Results
<#
Processing d:\Temp\HSGCopy.docx
VERBOSE: Word properties:
        DocID, FullName, HasPassword, 
        Permission, ReadOnly, Saved, 
        Creator, CurrentRsid, CompatibilityMode
938207550
D:\Temp\HSGCopy.docx
False
True
True
1297307460
12414886
15
VERBOSE: File system ReadOnly attribute:
False
VERBOSE: Document state
What if: Performing the operation "Remove File" on target "D:\Temp\HSGCopy.docx".
#>