From 615df344a55028feab2b4e84fba159be1b7b3eeb Mon Sep 17 00:00:00 2001 From: TechniKris <30842158+TechniKris@users.noreply.github.com> Date: Mon, 1 Mar 2021 21:34:59 +0100 Subject: [PATCH] Support for use without admin privileges Admin privilege check moved to the symlink function; $dwFlags adjusted for UNPRIVILEGED_CREATE --- ln.ps1 | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/ln.ps1 b/ln.ps1 index 26d1c45..f84a677 100644 --- a/ln.ps1 +++ b/ln.ps1 @@ -24,13 +24,24 @@ function isadmin { function symlink($target, $link_name, $is_dir) { # CreateSymbolicLink: # http://msdn.microsoft.com/en-us/library/aa363866.aspx - $dwFlags = 0 - if($is_dir) { $dwFlags = 1 } + $dwFlags = 2 + if($is_dir) { $dwFlags = 3 } $kernel = add-type $src -passthru $result = $kernel::createsymboliclink($link_name, $target, $dwFlags) - if(!$result) { "failed!"; exit 1 } # mysterious + if(!$result) { + if(!(isadmin)) { + if(gcm 'sudo' -ea silent) { + "ln: Must run elevated: try using 'sudo ln ...'." + } else { + if(gcm 'scoop' -ea silent) { + "ln: Must run elevated: you can install 'sudo' by running 'scoop install sudo'." + } else { "ln: Must run elevated" } + } + } else { "failed!" } # mysterious + exit 1 + } } function hardlink($target, $link_name) { @@ -82,17 +93,6 @@ if(!$symbolic -and $is_dir) { "ln: Can't create hard links for directories: use -s for symbolic link"; $usage; exit 1 } -if(!(isadmin)) { - if(gcm 'sudo' -ea silent) { - "ln: Must run elevated: try using 'sudo ln ...'." - } else { - if(gcm 'scoop' -ea silent) { - "ln: Must run elevated: you can install 'sudo' by running 'scoop install sudo'." - } else { "ln: Must run elevated" } - } - exit 1 -} - if($symbolic) { symlink $target $link_name $is_dir } else {