简体   繁体   中英

Powershell Loop to Write Password Protected Files

I'm trying to read excel files into Powershell, open, password protect them and write them back. I can do it individually but within a loop the script fails:

#working individually
$f = ("C:my\path\Out Files\1234dv.xlsx")
$outfile = $f.FullName + "out" 
$xlNormal = -4143 

$xl = new-object -comobject excel.application 
$xl.Visible = $True 
$xl.DisplayAlerts = $False     
$wb = $xl.Workbooks.Open($f)
$a = $wb.SaveAs("C:my\path\Out Files\test.xls",$xlNormal,"test")
$a = $xl.Quit() 

$a = Release-Ref($ws) 
$a = Release-Ref($wb) 
$a = Release-Ref($xl) 



#not working in loop, error after
function Release-Ref ($ref) { 
    ([System.Runtime.InteropServices.Marshal]::ReleaseComObject( 
    [System.__ComObject]$ref) -gt 0) 
    [System.GC]::Collect() 
    [System.GC]::WaitForPendingFinalizers()  
    } 

foreach ($f in Get-ChildItem "C:\my\path\Out Files"){
    $ff = $f
    $outfile = $f.FullName + "out" 
    $xlNormal = -4143 

    $xl = new-object -comobject excel.application 
    $xl.Visible = $True 
    $xl.DisplayAlerts = $False     
    $wb = $xl.Workbooks.Open($ff)
    $a = $wb.SaveAs("C:\my\path\Out Files\test.xls",$xlNormal,"test")
    $a = $xl.Quit() 

    $a = Release-Ref($ws) 
    $a = Release-Ref($wb) 
    $a = Release-Ref($xl) 
}

Sorry, we couldn't find 1234dv.xlsx. Is it possible it was moved, renamed or deleted? At line:16 char:5 + $wb = $xl.Workbooks.Open($ff) + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], COMException + FullyQualifiedErrorId : System.Runtime.InteropServices.COMException COM object that has been separated from its underlying RCW cannot be used. At line:17 char:5 + $a = $wb.SaveAs("C:\\my\\path ... + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : OperationStopped: (:) [], InvalidComObjectException + FullyQualifiedErrorId : System.Runtime.InteropServices.InvalidComObjectException

That error repeats for all four test files I'm working with.

I'm not really familiar with Powershell so I relied on MS docs, and I couldn't password protect the files in python so thought this would be easier. I know this doesn't address the password yet either but trying to get the loop to work first. Any help would be greatly appreciated. Thank you.

You should use

$wb = $xl.Workbooks.Open($ff.FullName)

To give Excel the full file path. Otherwise, $ff is a FileInfo object where a string (path) is required

As for the password protection, I suggest this can help you.

slight off topic for your question , but not for your intent :

from a security perspective using .xls passwords is not security, it is merely an annoyance.

it you need security, then i suggest you use something like Azure Information protection that allows you to encrypt , and share the file securely only with those that need access.

You still need to create your xls or .xlsx files (or any other file for that matter) then you can the powershell simply loop over them :

PS C:\>foreach ($file in (Get-ChildItem -Path \\server1\Docs -Recurse -Force | 
    where {!$_.PSIsContainer} |
    Where-Object {$_.Extension -eq ".xls"})) {
       Protect-RMSFile -File $file.PSPath -InPlace -DoNotPersistEncryptionKey All -TemplateID "e6ee2481-26b9-45e5-b34a-f744eacd53b0" -OwnerEmail "IT@Contoso.com"
}

https://docs.microsoft.com/en-us/powershell/module/azureinformationprotection/protect-rmsfile?view=azureipps

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM