How to change the read attribute for a list of files?

I am new to PowerShell. I used a sample script and made a replacement from get-item to get the contents in the first line. The modified script is as follows:

$file = get-content "c:\temp\test.txt"
if ($file.IsReadOnly -eq $true)
{
$file.IsReadOnly = $false
}

So essentially, I am trying to activate the elements contained in the test.txt file, stored as UNC paths

\\testserver\testshare\doc1.doc
\\testserver2\testshare2\doc2.doc

When the script is run, error messages are not reported and no action is taken even on the first write.

+3
source share
2 answers

Short answer:

sp (gc test.txt) IsReadOnly $false

Long answer below


Something is wrong with this.

$file a string[], . , IsReadOnly string[], , , .

, , , , . " " .

Get-Content . :

$filenames = Get-Content test.txt

. , FileInfo, . -Path Set-ItemProperty.

, . , FileInfo . foreach ( ):

$files = (foreach ($name in $filenames) { Get-Item $name })

IsReadOnly:

foreach ($file in $files) {
    $file.IsReadOnly = $false
}

. , , , PowerShell. , , . , .

,

Get-Content test.txt | Get-Item | ForEach-Object { $_.IsReadOnly = $false }

. , string s. Get-Item, , , , : ; , . Get-Item FileInfo , false.

, , , . . , Set-ItemProperty . , Set-ItemProperty -Path.

$files = Get-Content test.txt
Set-ItemProperty -Path $files -Name IsReadOnly -Value $false

, Set-ItemProperty -Path . :

Set-ItemProperty -Path (Get-Content test.txt) -Name IsReadOnly -Value $false

Get-Content , PowerShell, .

, , Set-ItemProperty , , :

Set-ItemProperty (Get-Content test.txt) IsReadOnly $false

:

sp (gc test.txt) IsReadOnly $false

$false 0, , 0 $false . , .

+8

, . , PowerShell, , Set-Writable Set- ReadOnly, , :

Get-Content "c:\temp\test.txt" | Set-Writable

, :

gc "c:\temp\test.txt" | swr

Set-ReadOnly is sro. , .

+2

Source: https://habr.com/ru/post/1719213/


All Articles