xiaoblue
I can't use English very well,I hope you can understant it.
I want create a shorcut on the public destop by the vbscript,then I found that I had to run the script with administrator.I searched and found this method.I use the code in my script just like this:
Set WshShell = WScript.CreateObject("WScript.Shell")
set objshellapp=CreateObject("Shell.Application")
objshellapp.ShellExecute "wscript.exe","D:\mytest\new\1.vbs"," ","runas",1
wscript.echo "ok"
Then I double click the script,but something strange happened,the wscript.exe running repeatedly and can't stop.I don't know how to deal with this problem.
My notebook computer operating system is Win10 Home Edition.
Thank you for your help.
----------------------------
#2 19 Nov 2017 06:35
Pyprohly
I’m not sure why you think you need ShellExecute to create a shortcut?
Here’s an examples of creating a shortcut in the Public Desktop folder.
Code: Select all
Set WshShell = CreateObject("WScript.Shell")
Set Fso = CreateObject("Scripting.FileSystemObject")
strShortcutName = "MyShortcut.lnk"
strArguments = ""
strDescription = "My new shortcut"
strTargetPath = "C:\pathto\My Program.exe"
intWindowStyle = 1
strWorkingDirectory = ""
strPublicFolderPath = WshShell.SpecialFolders("AllUsersDesktop")
With WshShell.CreateShortcut(Fso.BuildPath(strPublicFolderPath, strShortcutName))
.Arguments = strArguments
.Description = strDescription
.TargetPath = strTargetPath
.WindowStyle = intWindowStyle
.WorkingDirectory = strWorkingDirectory
.Save()
End With
----------------------------
#3 19 Nov 2017 10:54
xiaoblue
Thank you for your help.The structure of your code is so clear.
I nead ShellExecute because that the folder permissions are administators and others,not include users. And I don't know how to change the folder permissions by vbscipt.So I want to run script with elevated permissions.
----------------------------
#4 20 Nov 2017 13:03
Pyprohly
Given the importance of running the script as administrator, I see where the need for ShellExecute comes from.
Here’s the same script but it prompts UAC to elevate the script so you don’t have to manually run it as administrator.
Code: Select all
Set WshShell = CreateObject("WScript.Shell")
Set objShellApp = CreateObject("Shell.Application")
Set Fso = CreateObject("Scripting.FileSystemObject")
Sub PromptForElevation()
If WshShell.Run("net.exe sess", 0, True) <> 0 Then
strScriptHost = ""
If LCase(Right(WScript.FullName, 11)) = "cscript.exe" Then
strScriptHost = "cscript.exe"
Else
strScriptHost = "wscript.exe"
End If
objShellApp.ShellExecute strScriptHost, """" & WScript.ScriptFullName & """", "", "RunAs", 1
WScript.Quit
End If
End Sub
PromptForElevation
strShortcutName = "MyShortcut.lnk"
strArguments = ""
strDescription = "My new shortcut"
strTargetPath = "C:\pathto\My Program.exe"
intWindowStyle = 1
strWorkingDirectory = ""
strPublicFolderPath = WshShell.SpecialFolders("AllUsersDesktop")
With WshShell.CreateShortcut(Fso.BuildPath(strPublicFolderPath, strShortcutName))
.Arguments = strArguments
.Description = strDescription
.TargetPath = strTargetPath
.WindowStyle = intWindowStyle
.WorkingDirectory = strWorkingDirectory
.Save()
End With
#5 22 Nov 2017 14:40
xiaoblue
That's great.Now this code can create shortcuts in public folders.
Because my knowledge about VBS is weak, It took me some time to understand the code. I did some tests by modifying the code. But I still can't find out what makes it work properly. Instead of repeating to run itself like my original code. I hope you can tell me some of the details.
thank you very much smile
----------------------------
#6 22 Nov 2017 15:53
Pyprohly
Which part?
----------------------------
#7 23 Nov 2017 14:23
xiaoblue
Like this, this code can run properly:
Code: Select all
Set WshShell=CreateObject("WScript.Shell")
Set objShellApp=CreateObject("Shell.Application")
If WshShell.Run("net.exe sess", 0, True) <> 0 Then
strScriptHost = ""
If LCase(Right(WScript.FullName, 11)) = "cscript.exe" Then
strScriptHost = "cscript.exe"
Else
strScriptHost = "wscript.exe"
End If
objShellApp.ShellExecute strScriptHost, """" & WScript.ScriptFullName & """", "", "RunAs", 1
msgbox "hello world"
WScript.Quit
End If
Code: Select all
Set WshShell=CreateObject("WScript.Shell")
Set objShellApp=CreateObject("Shell.Application")
objShellApp.ShellExecute "wscript.exe", """" & WScript.ScriptFullName & """", "", "RunAs",1
msgbox "hello world"
WScript.Quit
----------------------------
#8 24 Nov 2017 15:31
Pyprohly
The second script will invoke itself when run. This will happen repeatedly as there’s no condition in place to end it. It runs the script as administrator on the second run but the script is not checking if it is running as administrator, and so it repetitively calls itself.
The first script would also repeatedly invoke itself but it does not because of the If statement’s condition which evaluates to true the second time around, testing if the script is running as administrator.
Examining the error code returned from “net.exe sess” is a reliable way to tell if the running script is running as administrator.
----------------------------
#9 27 Nov 2017 10:48
xiaoblue
Sorry, it took so long to reply.
Now I understand this code, it's too nice. But I have another question want to ask you.
I can't find the return value of net sess in official documents(maybe it's because I didn't find it).And the runas verb is also undocumented. How did people discover these ways.
Maybe the sentence is a bit out of order, if you have any questions, please point out.
----------------------------
#10 27 Nov 2017 15:01
Pyprohly
I wouldn’t bother with official documentation. Places like SS64.com tend to be more practical if you’re learning VBScript or Batch.
A lot of the techniques you see used in Batch and VBScript is shared knowledge. It’s not important how the methods were discovered, just know how to use them.
Checking for administrative rights using “net session” was a loan technique I picked up from Batch. The original idea came from this SO post where they explain how they found the technique and why it’s so good for testing for elevation. However, if you look in the comment chain you’ll see that it does have an edge case that could be a problem, but that’s not to say that the other methods don’t have their flaws as well.
As for the “RunAs” verb, I’m sure there are ways to list the available verbs for a given file extension to find verbs like “RunAs”. I vaguely recall doing this before, but there’s really no need to look up and do the derivation.
----------------------------
#11 28 Nov 2017 14:09
xiaoblue
Em, yeah. Now I have a clearer understanding of this method. Your answer give me great help, thank you again smile
original thread: https://ss64.org/oldforum/viewtopic.php?id=2249