locked
Moving from DocTag to AIP RRS feed

  • Question

  • Hi.

    I am moving from DocTag to Azure Information Protection. I found some script as a reference for reading Word document custom attributes. I was able to edit the scritpt to read the relevant attribute and based on its value set the correct AIP label. It works fine, it reads the attribute correctly and then set AIP attribute and refresh visual marking. But even though for every document I get this error

    The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
    At C:\script\DocTag2AIP.ps1:11 char:13
    +     foreach($property in $customProperties) {
    +             ~~~~~~~~~
        + CategoryInfo          : OperationStopped: (:) [], COMException
        + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException

    The script looks like this

    $files = @(Get-ChildItem C:\Users\luberan\Desktop\*.docx -Recurse)
    
    foreach ($file in $files) {
        $fileName = $file.Name
        $filePath = $file.DirectoryName
        $directory = $filePath + "\" + $fileName
        $word = New-Object -comobject Word.Application
        $document = $word.documents.open($directory)
        $binding = "System.Reflection.BindingFlags" -as [type]
        $customProperties = $document.CustomDocumentProperties
        foreach($property in $customProperties) {
            $vl = [System.__ComObject].InvokeMember("value",$binding::GetProperty,$null,$property,$null)
            if ($vl -eq "PU - For Personal Usage") {
                $word.Application.Quit()
                Set-AIPFileLabel -Path $directory -LabelId id1 -PreserveFileDetails
                $word = New-Object -comobject Word.Application
                $document = $word.documents.open($directory)
                $document.Save()
            }
            elseif ($vl -eq "C1") {
                $word.Application.Quit()
                Set-AIPFileLabel -Path $directory -LabelId id2 -PreserveFileDetails
                $word = New-Object -comobject Word.Application
                $document = $word.documents.open($directory)
                $document.Save()
            }
            elseif ($vl -eq "C2") {
                $word.Application.Quit()
                Set-AIPFileLabel -Path $directory -LabelId id3 -PreserveFileDetails
                $word = New-Object -comobject Word.Application
                $document = $word.documents.open($directory)
                $document.Save()
            }
            elseif ($vl -eq "C3") {
                $word.Application.Quit()
                Set-AIPFileLabel -Path $directory -LabelId id4 -PreserveFileDetails
                $word = New-Object -comobject Word.Application
                $document = $word.documents.open($directory)
                $document.Save()
            }
            elseif ($vl -eq "C4") {
                $word.Application.Quit()
                Set-AIPFileLabel -Path $directory -LabelId id5 -PreserveFileDetails
                $word = New-Object -comobject Word.Application
                $document = $word.documents.open($directory)
                $document.Save()
            }
        }
        $word.Application.Quit()
    }

    The provided script is exact copy of the real script, I just replaced AIP label IDs by id1 - id5.

    How to fix the issue please? Or is the script completely wrong?

    Tuesday, October 30, 2018 10:11 AM

Answers

  • Looking at this I am pretty sure that this is how you have to do it.

    $word = New-Object -comobject Word.Application
    $files = Get-ChildItem C:\Users\luberan\Desktop\*.docx -Recurse
    foreach ($file in $files) {
        $doc = $word.documents.open($file.Fullname)
        $labels = foreach($property in $document.CustomDocumentProperties) {
            $vl = [System.__ComObject].InvokeMember('value',[System.Reflection.BindingFlags]::GetProperty,$null,$property,$null)
            switch ($v1){
                'PU - For Personal Usage' { 'id1' }
                'C1' { 'id2' }
                'C2' { 'id3' }
                'C3' { 'id4' }
                'C4' { 'id5' }
                default {}
            }
        }
        $doc.Close()
        foreach($lbl in $labels){
            Set-AIPFileLabel -Path $file.Fullname -LabelId $lbl -PreserveFileDetails
        }
    }
    $word.Quit()
    

    Get all of the settings and close the file then apply the settings.

    Also not that you must use the GUIDs for the "LabelID" so change the switch to return the correct GUIDs.


    \_(ツ)_/


    • Edited by jrv Tuesday, October 30, 2018 10:37 AM
    • Marked as answer by Lukas Beran Tuesday, October 30, 2018 1:41 PM
    Tuesday, October 30, 2018 10:36 AM

All replies

  • Do not create the COM object in the loop.  Create it once outside the loop.  Yu are clogging memory with many copies and that is causing errors.

    ]


    \_(ツ)_/

    Tuesday, October 30, 2018 10:14 AM
  • This is the correct way to mange multiple documents in a loop.

    $word = New-Object -comobject Word.Application
    $files = Get-ChildItem C:\Users\luberan\Desktop\*.docx -Recurse
    foreach ($file in $files) {
        $doc = $word.documents.open($file.Fullname)
        foreach($property in $document.CustomDocumentProperties) {
            $vl = [System.__ComObject].InvokeMember('value',[System.Reflection.BindingFlags]::GetProperty,$null,$property,$null)
            $lbl = switch ($v1){
                'PU - For Personal Usage' { 'id1' }
                'C1' { 'id2' }
                'C2' { 'id3' }
                'C3' { 'id4' }
                'C4' { 'id5' }
    default {$null} }
    if($lbl){ Set-AIPFileLabel -Path $file.Fullname -LabelId $lbl -PreserveFileDetails $doc.Save() $doc.Close()
    } } } $word.Quit()


    \_(ツ)_/



    • Edited by jrv Tuesday, October 30, 2018 10:29 AM
    Tuesday, October 30, 2018 10:27 AM
  • Looking at this I am pretty sure that this is how you have to do it.

    $word = New-Object -comobject Word.Application
    $files = Get-ChildItem C:\Users\luberan\Desktop\*.docx -Recurse
    foreach ($file in $files) {
        $doc = $word.documents.open($file.Fullname)
        $labels = foreach($property in $document.CustomDocumentProperties) {
            $vl = [System.__ComObject].InvokeMember('value',[System.Reflection.BindingFlags]::GetProperty,$null,$property,$null)
            switch ($v1){
                'PU - For Personal Usage' { 'id1' }
                'C1' { 'id2' }
                'C2' { 'id3' }
                'C3' { 'id4' }
                'C4' { 'id5' }
                default {}
            }
        }
        $doc.Close()
        foreach($lbl in $labels){
            Set-AIPFileLabel -Path $file.Fullname -LabelId $lbl -PreserveFileDetails
        }
    }
    $word.Quit()
    

    Get all of the settings and close the file then apply the settings.

    Also not that you must use the GUIDs for the "LabelID" so change the switch to return the correct GUIDs.


    \_(ツ)_/


    • Edited by jrv Tuesday, October 30, 2018 10:37 AM
    • Marked as answer by Lukas Beran Tuesday, October 30, 2018 1:41 PM
    Tuesday, October 30, 2018 10:36 AM
  • Thank you very much for the prompt response. But it does not work. It does not show any error, but it does not convert old labels to new AIP labels - documents are untouched.

    I tried to check what it reads from the documents, but the value $vl is empty

    ...
    $vl = [System.__ComObject].InvokeMember('value',[System.Reflection.BindingFlags]::GetProperty,$null,$property,$null)
            Write-Host $vl
            switch ($vl){
    ...

    But it loads the files correctly. When I did the same for files, I got list of all files in the specified directory

    ...
    foreach ($file in $files) {
        $doc = $word.documents.open($file.Fullname)
        Write-Host $file
    ...

    • Edited by Lukas Beran Tuesday, October 30, 2018 11:41 AM
    Tuesday, October 30, 2018 11:39 AM
  • The documents may have no custom properties set with those names.


    \_(ツ)_/

    Tuesday, October 30, 2018 11:48 AM
  • The documents have properties with those names. When I used my version of the script, it worked, just with the RPC error.

    And the Write-Host in this case should write all custom properties of the document, or not?

    Tuesday, October 30, 2018 12:01 PM
  • Well I will leave you t sort it as I do not have your documents or system.  Just remember that you only need to create the "Word" object once and then open and close documents.

    From what I see of the code your code does not really work although it may appear to work.


    \_(ツ)_/

    Tuesday, October 30, 2018 12:08 PM
  • Now it works, you just have a mistake in a variable name - you open the document to a variable $doc and then you want to access content of the variable via $document :-) So I just renamed all to $document and it works.

    Thanks a lot again!

    Tuesday, October 30, 2018 1:41 PM