Contents

VM Lab - Read Only status

Contents

In this next installment of my  VM Lab series of posts, I have created a tiny set of tools which basically do exactly as the names imply. While I admit, not quite hard to do, perhaps you’d even prefer to type things manually, but as mentioned in my previous post, for me it’s all about re-usability.

Requirements

In one of the upcoming installments of my VM Lab series, I will create a template VHD file, which will need to be set to Read Only in order to prevent changes to be made accidentally. In another, I will need to copy a current Unattend.xml file, check if it was set to Read Only and if it was, remove that value so we can customize the contents.

So I made a small set of tools that did just that.

Functions

As the scripts aren’t too complex, but do server a solid purpose for the rest of the series, I’ve provided the code below.

BEWARE

These functions are meant for a test environment. I’ve borrowed Jeffery Hicks' disclaimer, because it simply says it all:

DO NOT USE IN A PRODUCTION ENVIRONMENT UNTIL YOU HAVE TESTED THOROUGHLY IN A LAB ENVIRONMENT. USE AT YOUR OWN RISK. IF YOU DO NOT UNDERSTAND WHAT THIS SCRIPT DOES OR HOW IT WORKS, DO NOT USE IT OUTSIDE OF A SECURE, TEST SETTING.

I’ve set the scripts to a max height as to not flood the page, but rest assured, everything’s there. Please do note that functions might refer to other functions, this is intended as a complete toolset, not just 1 piece.

If you have questions on the techniques used or suggestions on how to improve something, please let me know in the comment section!

Get-ReadOnly

function Get-ReadOnly {

<#
	.SYNOPSIS
		Checks the ReadOnly status of a file.

	.DESCRIPTION
		Checks the ReadOnly status of a file.
		The script allows for multiple files to be checked.
    Value is validated before checked.

	.PARAMETER FileName
		Provide the filename [full path] which needs to be checked
    Accepts input from pipeline

	.EXAMPLE
				PS C:\> Get-ReadOnly -FileName C:\Test\foo.txt

				Checks to see the ReadOnly status for the file C:\Test\foo.txt

	.EXAMPLE
				PS C:\> 'C:\Test\foo.txt','C:\Test\bar.txt' | Get-ReadOnly

				Checks the ReadOnly status for both files C:\Test\foo.txt and C:\Test\bar.txt

	.NOTES

    Created by: Robert Prüst
    Blog: http://powershellpr0mpt.com
    Version: 1.0
    Module: PSP-VMLab

#>

  [CmdletBinding()]

  param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [ValidateScript({Test-Path -Path $_})]
    [string[]]$FileName
  )

  process {
    Write-Verbose "Starting $($MyInvocation.Mycommand)"
    foreach ($File in $FileName) {
        Write-Verbose "Checking current ReadOnly status for '$File'"
        (Get-ItemProperty -Path $File -Name IsReadOnly).IsReadOnly
    }

  }

}

```


## Set-ReadOnly

function Set-ReadOnly {

<#
	.SYNOPSIS
		Sets the ReadOnly status of a file.

	.DESCRIPTION
		Checks the ReadOnly status of a file and sets it to ReadOnly if this was not already done.
		The script allows for multiple files to be checked.

	.PARAMETER FileName
		Provide the filename [full path] which needs to be checked.
    Accepts input from pipeline.
    Value is validated before checked.

	.EXAMPLE
				PS C:\> Set-ReadOnly -FileName C:\Test\foo.txt

				Checks to see the Read Only status for the file C:\Test\foo.txt and sets it to ReadOnly if this is not already the case.

	.EXAMPLE
				PS C:\> 'C:\Test\foo.txt','C:\Test\bar.txt' | Set-ReadOnly

				Checks the ReadOnly status for both files C:\Test\foo.txt and C:\Test\bar.txt and sets them to ReadOnly if this is not already the case

	.NOTES

    Created by: Robert Prüst
    Blog: http://powershellpr0mpt.com
    Version: 1.0
    Module: PSP-VMLab

#>

  [CmdletBinding()]

  param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [ValidateScript({Test-Path -Path $_})]
    [string[]]$FileName
  )

  process {
      Write-Verbose "Starting $($MyInvocation.Mycommand)"

      foreach ($File in $FileName){
      Write-Verbose "Checking current ReadOnly status for '$File'"
      $Status = Get-ReadOnly -FileName $File

        if (!($Status)){
          Write-Verbose "Setting file '$File' to ReadOnly"
          Set-ItemProperty -Path $File -Name IsReadOnly -Value $true
          Write-Output "ReadOnly set for file '$File'"
        } else {
          Write-Output "'$File' already set to ReadOnly"
        }
      }
  }

}

```


## Remove-ReadOnly

function Remove-ReadOnly {

<#
	.SYNOPSIS
		Removes the ReadOnly status of a file.

	.DESCRIPTION
		Checks the Read Only status of a file and removes the ReadOnly value if this is required.
		The script allows for multiple files to be checked.

	.PARAMETER FileName
		Provide the filename [full path] which needs to be checked.
    Accepts input from pipeline.
    Value is validated before checked.

	.EXAMPLE
				PS C:\> Remove-ReadOnly -FileName C:\Test\foo.txt

				Checks to see the ReadOnly status for the file C:\Test\foo.txt and removes it if currently set.

	.EXAMPLE
				PS C:\> 'C:\Test\foo.txt','C:\Test\bar.txt' | Set-ReadOnly

				Checks the ReadOnly status for both files C:\Test\foo.txt and C:\Test\bar.txt and removes it if currently set.

	.NOTES

    Created by: Robert Prüst
    Blog: http://powershellpr0mpt.com
    Version: 1.0
    Module: PSP-VMLab

#>


  [CmdletBinding()]

  param (
    [Parameter(Mandatory=$true,ValueFromPipeline=$true)]
    [ValidateScript({Test-Path -Path $_})]
    [string[]]$FileName
  )

  process {
    Write-Verbose "Starting $($MyInvocation.Mycommand)"

    foreach ($File in $FileName) {
      Write-Verbose "Checking current ReadOnly status for '$File'"
      $Status = Get-ReadOnly -FileName $File

      if ($Status){
        Write-Verbose "Removing ReadOnly from '$File'"
        Set-ItemProperty -Path $File -Name IsReadOnly -Value $false
        Write-Output "ReadOnly removed for file '$File'"
      } else {
        Write-Output "'$File' not set to ReadOnly"
      }
    }
  }

}
```