Public/Utils/Test-WritePermission.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
function Test-WritePermission {
  <#
    .SYNOPSIS
    Tests if it's possible to create file in given location (path)

    .DESCRIPTION
    Creates a random file in given location. Deletes if it was created. Returns Boolean True or False

    .PARAMETER Path
    Location where test write should occur

    .EXAMPLE
    Test-WritePermission -Path c:\SomePath
    Will create random file in given location. Will return True or False

    .INPUTS
    Path to test

    .OUTPUTS
    Boolean True or False
  #>


  [cmdletbinding()]
  [OutputType([Boolean])]

  Param(
  [Parameter(Mandatory,HelpMessage='Provide path where to test write possibility')]
  [ValidateScript({Test-Path -Path $_ -IsValid -PathType Container})]
  [string[]]
  $Path
  )

  foreach ($testPath in $Path) {
    $testFileName = "TestFile_{0}" -f (Get-Date -Format 'yyyyMMddHHss')
    $testfile = New-Item -Path $testPath -Name $testFileName -ItemType File -ErrorAction SilentlyContinue
    if (-not $testfile) {
      Write-Log -Error -Message "Unable to write in Path {$testPath}. Verify your permissions."
      $false
      break
    }
    else {
      Remove-Item -Path $testfile -Force
      $true
    }
  }
}