I have a VBScript that is hosted in a network share. I have a link set up on a page to access that script via the web browser (i.e. \\server\script.vbs). Is it possible to pass in a variable/argument into that script via a hyperlink? I already tried \\server\script.vbs Argument and \\server\script.vbs%20Argument neither work. The script works fine with no argument. I scoured the web trying to find a way and fear that it is not possible so I thought I would ask here before I just make separate scripts with the arguments built in instead. The script sample is below:
Dim Var1
If Wscript.Arguments.Count = 0 then
WScript.Echo "Missing parameters"
Else
Var1 = wscript.arguments(0)
MsgBox "Passed in Variable: " + Var1
End If
You can use ASP, but you need to add the feature to the IIS role on the web server, configure the authentication (strongly recommend Windows authentication, NOT anonymous), and create an ASP page to capture the inline parameter (querystring) value and display it. If the page is "test.asp", the sample below would be called at http://myserver/test.asp?arg1=123. It would display "You entered: 123"
<%
x = Trim(Request.QueryString("arg1"))
Response.Write "You entered: " & x
%>
I'm not sure you can, however you could link instead to an ASP page and have it run the VBS file - this code for that page should be close:
<SCRIPT LANGUAGE=vbscript>
dim oWs
Set oWs= CreateObject("WScript.shell")
oWs.Run "\\yourserver\yourscript.vbs?ParameterNameHere=" & Request.QueryString("ParameterNameHere"), 1, True
Set oWs = Nothing
</SCRIPT>
Related
We have an old Microsoft Access font end that serves as the GUI to our user database. I was never much of a VBA person so as I go through fixing bugs I'm learning as I go.
Our Access DB has a number of commands to sync info to Active Directory. One such command is to add a user to a group. However, whenever the group contains a / the group is never added.
The debug produces this as:
Run-time error -2147463168 (80005000)': Automation Error".
Printing out the targetgroup shows the DN as I expect it. Trying to escape the / before the GetObject doesn't help and causes its own auth error.
Here's the top part of the function -
Function AddGroup(TargetGroup, strUserID, Optional strOptReqBy)
Dim objDL
Set objUser = GetObject("LDAP://" & GetDName(CStr(strUserID)))
Set objDL = GetObject("LDAP://" & TargetGroup)
On Error Resume Next
objDL.Add (objUser.ADsPath)
objDL.SetInfo
On Error GoTo 0
This works fine if the group does not contain a /.
Debug points to Set objDL = GetObject("LDAP://" & TargetGroup)
Looking for some input on why this is happening. Thanks!
In an LDAP path, the / is a separator. Not only is the // used near the beginning, but you can also specify the server you want to connect to, followed by a /, then the DN of the object, like this:
LDAP://example.com/DC=example,DC=com
That's necessary if the computer you're running this from isn't not joined to the same (or trusted) domain than the domain you're connecting to.
So that means that if the DN of the object you want to bind to has a /, it will think that everything before the / is a server to connect to and it explodes.
So you just need to escape it, which, as you've already learned, is done with a \:
LDAP://OU=This\/That,DC=example,DC=com
So yeah, a simple replace will do:
Set objUser = GetObject("LDAP://" & Replace(GetDName(CStr(strUserID)), "/", "\/")
Don't feel bad. Even Microsoft has this bug in their code.
For better or worse we are launching an Access db from Sharepoint. Note that this db is not PUBLISHED to SP, people just double-click the link and open the db on their desktops.
So now we need to begin imposing the equivalent of some roles-based edit restrictions. I know there is a VBA CurrentWebUser function and a CurrentWebUserGroups which provides some basic data about who's accessing an Office file from Sharepoint. However my reading and limited experimenting with this stuff leads me to suspect that, for Access at least, these will only work with published dbs, and not ones that are just being launched and run locally, like we're doing.
Is there anything I can get from SP in a case like this? Web user and user group would be useful, so would whichever site/page the link is being clicked on. Is any of this available?
Thanks.
rabbit
Well, not in any simple way.
As you've already determined, Application.CurrentWebUser just returns Null.
However, there are several ways to query the user information from SharePoint.
The recommended way (also by me) if you're going to work with SharePoint extensively, is to use the CSOM api, which requires a .Net language, so you'll have to create a COM module, authenticate it separately, and that's all a lot of work.
However, if you're only using simple GET requests, you can also use the REST API and re-use the authentication MS Access uses itself (since MS Access uses MSXML2 to submit web requests to SharePoint, we can create our own MSXML2.XMLHTTP object and it will re-use the cookies Access uses).
The following code uses the JSONInterpreter object I've shared here on GitHub. You could convert it to use XML and MSXML if you don't want that dependency, though.
To execute a request, I use the following code, that assumes the Access application is authenticated, but if it isn't, it connects to the SharePoint site using ADO.
(For this code, MySiteName is a global variable containing the URL of your SharePoint site, without a trailing slash)
Public Function SPRestGetJSON(Site As String, Request As String) As String
Dim tries As Long
Dim Success As Boolean
Do
'Try to execute request
tries = tries + 1
Dim xmlHttpReq As Object 'MSXML2.XMLHTTP60
Set xmlHttpReq = CreateObject("Msxml2.XMLHTTP.6.0") 'New MSXML2.XMLHTTP60
xmlHttpReq.Open "GET", Site & Request, False
xmlHttpReq.setRequestHeader "Content-Type", "application/json"
xmlHttpReq.setRequestHeader "Accept", "application/json;odata=nometadata"
xmlHttpReq.send
Dim root As JSONInterpreter
Set root = New JSONInterpreter
root.JSON = xmlHttpReq.responseText
If Not root.Exists("odata.error") Then
Success = True
End If
If Not Success And tries = 1 Then
'Connect to SharePoint using WSS + ADO to create auth cookies inside MSXML
Dim conn As Object 'ADODB.Connection
Set conn = CreateObject("ADODB.Connection") 'New ADODB.Connection
conn.Open "Provider=Microsoft.ACE.OLEDB.12.0;WSS;DATABASE=" & Site
On Error Resume Next
conn.Execute "SELECT 1 From SomeTable" 'Execute to non-existent table but connect to sharepoint
On Error GoTo 0
conn.Close
End If
Loop While tries < 2 And Success = False
SPRestGetJSON = xmlHttpReq.responseText
End Function
Then, we can use that in a simple function:
Public Function GetSPUsername() As String
Dim jsi As New JSONInterpreter
jsi.JSON = SPRestGetJSON(MySiteName, "/_api/Web/CurrentUser")
GetSPUsername = jsi.item("LoginName").VBAVariant
End Function
Getting groups is also available. This code returns an array of dictionary objects, you can view the available keys in the locals window:
Public Function GetSPGroups() As Variant 'Array of dictionaries
Dim jsi As New JSONInterpreter
jsi.JSON = SPRestGetJSON(SiteName, "/_api/Web/CurrentUser/Groups")
GetSPGroups = jsi.item("value").VBAVariant
End Function
Then, to get the title of the first group the current user is a member of in the immediate window, we can use:
?GetSPGroups(0)!Title
i'm a beginner of vbscript , i need you help , my question is how do i do to get a variable from cmd and show it in vbscript for example get a ping from www.google.com and show it in a msgbox in vbscript help me code :
dim cmd,x
set cmd = createobject("wscript.shell")
x= cmd.run("cmd /k ping www.google.com ",1,true)
Get that output and show it in a msgbox later , help me
Here an example of how to do that.
The response of the ping that is checked is in Dutch but that doesn't matter for your case.
Set objExec = CreateObject("WScript.Shell").exec("ping www.google.com")
With objExec
Do While .Status = 0
WScript.Sleep 10
Do While Not .StdOut.AtEndOfStream
WScript.Echo .StdOut.ReadLine
'Check the .StdErr to see if it is at the end of its
'stream. If not, call ReadLine on it
If Not .StdErr.AtEndOfStream Then
.StdErr.ReadLine
End If
Loop
Loop
End With
An advise though, don't begin scripting in vbscript, it's a dead end.
Choose some modern scripting language like Python or still better for beginners: Ruby.
Be sure to use cscript as engine in stead of wscript, execute the following to set that as default.
wscript //H:Cscript
Your vbscript is then one single line
puts `ping www.google.com`
I've played with ASP classic and VB Script a few years since that's what we have at our company, but this is really bothering me. We bought the CJWSoft ASP Protect, and did some customizing, if anyone is familiar with it. Every page I log into ends with a question mark at the end of the URL and displays an internal server 500 error (using a testing server: Windows Web Server 2008 R2). One would think it would always go to the default.asp page upon login, but that doesn't happen unless I open a new window, which it's set to not store cookies. I can access any page I log into after I clear the question mark at the end of the URL. I have IT guys here, but we're not sure what's causing it.
(IP Address/Default.asp? [or] IP Address/password_admin/default.asp?) produces an
Internal Server Error 500.
Remove the ? and I'm into any of the pages on the server. Why?
I think the following script may have something to do with it, or whatever relates to it...
If Session("PasswordAccess") = "No" Then
Thispage = Request.ServerVariables("script_name")
Else
'Thispage = Request.ServerVariables("script_name") & "?" & Request.Querystring & Request.Form
'Setting Below is more secure than the setting above which allows form post data to be re-returned to the page
Thispage = Request.ServerVariables("script_name") & "?" & Request.Querystring
End If
Please help me resolve the question mark, anything else is a grand bonus!
What about only appending that ? if the query string isn't empty?
Like
<%
Thispage = Request.ServerVariables("script_name")
Dim qst : qst = Request.ServerVariables("QUERY_STRING")
if qst<>"" then Thispage = Thispage & "?" & qst
%>
Is it possible to include in a VB.net 2008 Project a VBScript (test.vbs) and run it if its while the processing necessary? But the main thing is it should be possible to BUILD just one .exe.
If so, can you also receive values / arguments from the VBS file?
Here is an example, although it's pointless, but it is used for unterstanding:
VB.net -> exe is running
the exe runs please_find_the_coputername.vbs
The script please_find_the_coputername.vbs -> obtained the computer name and sends this variable to VB.net
VB.Net displays the computer name via Msgbox().
Note: I know that I can read out the computer name with VB.net but this example is only for understanding my questions.
Edit:
HI #maxedev thank you for your answer.
Wow.. its nice trick.
But I want only to do this VBScript code in VB.net:
Dim strComputer
strComputer = "LP-BKR"
Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" _
& strComputer & "\root\cimv2")
Set colComputer = objWMIService.ExecQuery ("Select * from Win32_ComputerSystem")
For Each objComputer in colComputer
Wscript.Echo "Logged-on Domain: " & objComputer.Domain
Wscript.Echo "Logged-on UserName: " & objComputer.UserName
Wscript.Echo "Logged-on ComputerName: " & objComputer.Name
Next
set objWMIService = Nothing
set colComputer = Nothing
I searched the whole day to get the same Value... but didn't find anything. That's why I decide to do that in this way. But if I think, the trick with clipboard is risky. It pushes the still clipboard text away. How can I realize it?
I'm not sure exactly what you're trying to accomplish, but you could write to a text file and then read it through vb.net - or you could do something like this post to use the clipboard to pass info ie :
VBS:
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "cmd.exe /c echo hello world | clip", 0, TRUE
VB.NET:
MessageBox.Show(Clipboard.GetText)
--shows "hello world"
One solution would be to add a reference to the MS Script Control:
http://msdn.microsoft.com/en-us/library/aa227400(v=vs.60).aspx
Using that, you can add literally add code (VBScript) with the AddCode() method then run it and get the output back. I have a tiny example here.
Windows automatically provides the information you're looking for in environment variables:
%USERNAME% -> username of the logged in user
%USERDOMAIN% -> WINS name of the domain the user is logged into
%USERDNSDOMAIN% -> FQDN of the domain the user is logged into
%COMPUTERNAME% -> hostname of the computer