1

I'm trying to create a symbolic link with mklink through a Powershell script to a Powershell script on a remote server. this link should come on the desktop of all users.

if(!(test-path -path \$hostname\c$\Users)) { Copy-Item -Path "\dsfpad\Nagios\Nagios_Downtime\Nagios - Schedule Downtime.lnk" -Destination "\$hostname\c$\Documents and Settings\All Users\Desktop\Nagios - Schedule Downtime.lnk" } else { $s=New-PSSession -ComputerName $hostname Enter-PSSession $s Invoke-Command -Session $s -ScriptBlock {& cmd /c mklink "C:\Users\Public\Desktop\Nagios - Schedule Downtime.lnk" "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -file c:\Nagios\Nagios_Downtime_Window_NRDP.ps1"} exit-pssession
remove-pssession $s }

I get symbolic link created for C:\Users\Public\Desktop\Nagios - Schedule Downtime.lnk <<===>> C:\WINDOWS\system32\WindowsPowerShell\v1.0\po wershell.exe -file c:\Nagios\Nagios_Downtime_Window_NRDP.ps1

But when I try to execute the link on a server, it doesn't work. looking at the Target of the link, therer seems to be no target...

Any tips / ideas?

2
  • it seems when i dont't use C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -file in the target, I do get a target in the lnk file, but it does not execute the script...
    – willemdh
    Dec 10, 2013 at 11:43
  • Creating a symlink requires admin level on windows, and the link target must exist. There are multiple types of them, one is similar to hardlink, one is to bind-mounts, and the third is the normal symlink. You know: hope it helps :-)
    – peterh
    Dec 10, 2013 at 13:50

1 Answer 1

2

This post is quite old, but I would just create another .ps1 file that calls the requested .ps1 file:

In your case instead of

& cmd /c mklink "C:\Users\Public\Desktop\Nagios - Schedule Downtime.lnk" "C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -file c:\Nagios\Nagios_Downtime_Window_NRDP.ps1"

I would just use:

"& c:\Nagios\Nagios_Downtime_Window_NRDP.ps1" | Out-File "c:\temp\C:\Users\Public\Desktop\Nagios - Schedule Downtime_lnk.ps1"

If you cannot execute the file you can use -ExecutionPolicy Bypass (see below)

PowerShell -ExecutionPolicy Bypass -File "c:\temp\C:\Users\Public\Desktop\Nagios - Schedule Downtime_lnk.ps1"

(https://support.microsoft.com/en-us/help/2856739/you-cannot-use-a-symbolic-link-to-run-an-exe-file-or-execute-a-script)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.