嗨,大家好!
我创建了一个Powershell脚本,该脚本会处理一些存档文件,以对其中一个文件进行一些简单的文本编辑。
我的问题很简单,当我想打开和编辑xxx.ini时,它失败了。 但是,当我添加一个test.ini (或删除xxx.ini,然后将其重新添加),并尝试在此新文件上运行完全相同的脚本时,它就可以工作。
我尝试寻找 IsReadOnly 属性,但它返回false。
任何人都可以帮忙吗?
if ($f -notmatch "bulkPrefEditor"){
# The zip file to be updated
$file = $f
# Is the file read only ? Let's try to force that to false
$isReadOnly = Get-ItemProperty -Path $file | Select-Object IsReadOnly
Write-Log "Is $file Read-Only : $isReadOnly" "INFO" $logfile
Set-ItemProperty -Path $file -Name IsReadOnly -Value $false
Write-Log "Editing following file : $f" "INFO" $logfile
# Load ZipFile (Compression.FileSystem) if necessary
try { $null = [IO.Compression.ZipFile] }
catch { [System.Reflection.Assembly]::LoadWithPartialName('System.IO.Compression.FileSystem') }
# Open zip file with update mode (Update,Read,Create -- are the options)
try {
$fileZip = [System.IO.Compression.ZipFile]::Open( $file,'Update' )
}
catch {
Write-Log "Another process has locked the '$file' file." "ERROR" $logfile
Write-Log "Another process has locked the '$file' file."
continue
}
# Finding the specific file within the zip file
try{
$fileZip.Entries | Where-Object { $_.FullName -match "$innerpath" }
} catch {
Write-Log "Could not find $filepath\$innerpath in $f,have you used anti-slash instead of slash ?" "WARN" $logfile
Write-Log "Could not find $filepath\$innerpath in $f,have you used anti-slash instead of slash "
continue
}
pause
# If needed,read the contents of specific file to $text and release the file so to use streamwriter later
try{
$desiredFile = [System.IO.StreamReader]($fileZip.Entries | Where-Object { $_.FullName -match "$innerpath" }).Open()
$text = $desiredFile.ReadToEnd()
$desiredFile.Close()
$desiredFile.Dispose()
}
catch {
Write-Log "Could not read $f/$innerpath in $f " "WARN" $logfile
Write-Log "Could not read $f/$innerpath in $f "
continue
}
我已经在脚本中添加了一些简单的日志:
2019/11/21 18:40:12信息###################开始脚本... ########### ######### 2019/11/21 18:40:12信息此脚本将对C:\ Users \ pfournet \ Documents \ Factory \ BulkPrefEditor中包含的文件进行操作 2019/11/21 18:40:12 INFO此脚本可以查看(4)文件。 2019/11/21 18:40:12 INFO可用文件列表(单行):20191119-123448387-Copie(2).zip 20191119-123448387-Copie.zip 20191119-123448387.zip BulkPrefEditor.ps1 BulkPrefEditor_Zip.ps1 2019/11/21 18:40:12 INFO已找到该文件:20191119-123448387-Copie(2).zip 2019/11/21 18:40:12 INFO已找到该文件:20191119-123448387-Copie.zip 2019/11/21 18:40:12 INFO已找到该文件:20191119-123448387.zip 2019/11/21 18:40:12 INFO此脚本将替换以下行:use-compression = true和use-compression = false 2019/11/21 18:40:12信息#################### COMMENCING OPERATIONS ################## ### 2019/11/21 18:40:12信息是20191119-123448387-Copie(2).zip只读:@ {IsReadOnly = False} 2019/11/21 18:40:12 INFO编辑以下文件:20191119-123448387-Copie(2).zip 2019/11/21 18:40:13 WARN无法读取20191119-123448387-Copie(2).zip / ul-flow / plugins / interactivedata.ini in 20191119-123448387-Copie(2).zip 2019/11/21 18:40:13信息是20191119-123448387-Copie.zip只读:@ {IsReadOnly = False} 2019/11/21 18:40:13 INFO编辑以下文件:20191119-123448387-Copie.zip 2019/11/21 18:40:13警告无法读取20191119-123448387-Copie.zip/ul-flow/plugins/interactivedata.ini in 20191119-123448387-Copie.zip 2019/11/21 18:40:13 INFO为20191119-123448387.zip只读:@ {IsReadOnly = False} 2019/11/21 18:40:13 INFO编辑以下文件:20191119-123448387.zip 2019/11/21 18:40:14警告无法读取20191119-123448387.zip中的20191119-123448387.zip/ul-flow/plugins/interactivedata.ini 2019/11/21 18:40:14信息#################### ENDING SCRIPT ... ############## ######