PoshCode Archive  Artifact Content

Artifact be832d06a9763587399efbb22596f6f3976cea049446f6682147b43bc3d694f8:

  • File Set-FileWriteable.ps1 — part of check-in [98e90b1b53] at 2018-06-10 13:08:04 on branch trunk — A demo script which work against remote session output as well as local output: (user: unknown size: 1630)

# encoding: ascii
# api: powershell
# title: Set-FileWriteable
# description: A demo script which work against remote session output as well as local output:
# version: 0.1
# type: function
# license: CC0
# function: Set-FileWriteable
# x-poshcode-id: 2274
# x-archived: 2010-10-04T16:23:27
#
# This function removes the ReadOnly flag from files, and works on files output from invoke-command $session on remote computers
#
function Set-FileWriteable {
#.Example
# $s = New-PSSession $ComputerName
# C:\PS>$files = Invoke-Command $s { ls }
# .... 
# C:\PS>Set-FileWriteable $files
#
param(
   [Parameter(Mandatory=$true,ValueFromPipeline=$true)]   
   $File
,
   [switch]$Passthru
)
process {
   foreach($path in @($file)) {
      write-verbose "'$path' is on '$($path.PSComputerName)'"
      if($path.PSComputerName) {
         Invoke-Command $path.PSComputerName {
            param([string[]]$path,[switch]$passthru)
            $files = Get-Item $path
            foreach($f in $files) {
               if($f.Attributes -band [IO.FileAttributes]"ReadOnly") {
                  $f.Attributes = $f.Attributes -bxor [IO.FileAttributes]"ReadOnly"
               }
            }
            write-output $files
         } -Argument $path | Where { $Passthru }
      } else {
         $files = Get-Item $path
         foreach($f in $files) {
            if($f.Attributes -band [IO.FileAttributes]"ReadOnly") {
               $f.Attributes = $f.Attributes -bxor [IO.FileAttributes]"ReadOnly"
            }
         }
         if($Passthru) { write-output $files }
      }
   }
}
}