usr/lib/New-HardLink.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
function New-HardLink {
  [CmdletBinding()]
  param(
    [Parameter(Mandatory, Position=0)]
    [ValidateNotNullOrEmpty()]
    [ValidateScript({!!($script:src = Get-Item $_ -ErrorAction 0)})]
    [String]$Source,

    [Parameter(Mandatory, Position=1)]
    [ValidateNotNullOrEmpty()]
    [String]$Target
  )

  end {
    if ($src.PSIsContainer) {
      Write-Verbose 'operation cannot be applied to folders.'
      return $false
    }

    New-Delegate kernel32 {
      bool CreateHardLinkW([buf, buf, ptr])
    }

    $kernel32.CreateHardLinkW.Invoke(
      [buf].Uni($Target), [buf].Uni($src.FullName), [IntPtr]::Zero
    )
  }
}

Export-ModuleMember -Function New-HardLink