I found this via google: http://www.mvps.org/access/api/api0008.htm
'******************** Code Start **************************
' This code was originally written by Dev Ashish.
' It is not to be altered or distributed,
' except as part of an application.
' You are free to use it in any application,
' provided the copyright notice is left unchanged.
'
' Code Courtesy of
' Dev Ashish
'
Private Declare Function apiGetUserName Lib "advapi32.dll" Alias _
"GetUserNameA" (ByVal lpBuffer As String, nSize As Long) As Long
Function fOSUserName() As String
' Returns the network login name
Dim lngLen As Long, lngX As Long
Dim strUserName As String
strUserName = String$(254, 0)
lngLen = 255
lngX = apiGetUserName(strUserName, lngLen)
If ( lngX > 0 ) Then
fOSUserName = Left$(strUserName, lngLen - 1)
Else
fOSUserName = vbNullString
End If
End Function
'******************** Code End **************************
Is this the best way to do it?
You could also do this:
Set WshNetwork = CreateObject("WScript.Network")
Print WshNetwork.UserName
It also has a UserDomain property and a bunch of other things:
http://msdn.microsoft.com/en-us/library/907chf30(VS.85).aspx
You could also use Environ$ but the method specified by the question is better. Users/Applications can change the environment variables.
I generally use an environ from within VBA as in the following. I haven't had the problems that Ken mentions as possibilities.
Function UserNameWindows() As String
UserNameWindows = VBA.Environ("USERNAME") & "#" & VBA.Environ("USERDOMAIN")
End Function
Lots of alternative methods in other posts, but to answer the question: yes that is the best way to do it. Faster than creating a COM object or WMI if all you want is the username, and available in all versions of Windows from Win95 up.
Alternative way to do that - probably the API you mention is a better way to get username.
For Each strComputer In arrComputers
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colItems = objWMIService.ExecQuery("Select * from Win32_ComputerSystem",,48)
For Each objItem in colItems
Wscript.Echo "UserName: " & objItem.UserName & " is logged in at computer " & strComputer
Next
there are lots of way to get the current logged user name in WMI.
my way is to get it through the username from process of 'explorer.exe'
because when user login into window, the access of this file according to the current user.
WMI script would be look like this:
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strIP & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery("Select * from Win32_Process")
For Each objprocess In colProcessList
colProperties = objprocess.GetOwner(strNameOfUser, strUserDomain)
If objprocess.Name = "explorer.exe" Then
UsrName = strNameOfUser
DmnName = strUserDomain
End If
Next
for more detailcheck the link on :
http://msdn.microsoft.com/en-us/library/aa394599%28v=vs.85%29.aspx
Related
I tend to run a lot of commands against remote PCs, but I always check to make sure they are online first. I currently use this code:
If objFSO.FolderExists("\\" & strHost & "\c$") Then
'The PC is online so do your thing
However, when the remote PC is not online, it takes my laptop ~45 seconds before it times out and resolves to FALSE.
Is there a way to hasten the timeout? Or is there another easily implementable solution to determine if a PC is online?
You could use WMI to ping it.
Function IsOnline(strHost As String) As Boolean
Dim strQuery As String
strQuery = "select * from Win32_PingStatus where Address = '" & strHost & "'"
Dim colItems As Object
Set colItems = GetObject("winmgmts://./root/cimv2").ExecQuery(strQuery)
Dim objItem As Object
For Each objItem In colItems
If IsObject(objItem) Then
If objItem.StatusCode = 0 Then
IsOnline = True
Exit Function
End If
End If
Next
End Function
You can use the return code from a ping request:
Function HostIsOnline(hostname)
Set oShell = WScript.CreateObject("WScript.Shell")
Set oShellExec = oShell.Exec("ping -n 1 " & hostname)
While oShellExec.Status = 0
WScript.Sleep(100)
Wend
HostIsOnline = (oShellExec.ExitCode = 0)
End Function
I'm using the following code inside an ActiveX Script job on SQl Server to call an URL every X minutes.
Dim WshShell
Set WshShell = CreateObject("WScript.Shell")
WshShell.Run "iexplore.exe http://www.google.com.br"
Set WsShell = Nothing
it is working but the processes created keep running:
Any way of changin that code to call the URL and kill the recent called process or call it with a "time-to-live". I think it is more secure, I wouldn't want to kill the wrong process.
Following up on the suggestion by #Ted, you can also fetch a URL using native Microsoft capabilities in an in-process fashion. You can do this via a component known as WinHTTP (the latest appears to be WinHTTP 5.1).
See my script below which includes a function to simply obtain the status of a URL. When I run this script I get the following output:
http://www.google.com => 200 [OK]
http://www.google.com/does_not_exist => 404 [Not Found]
http://does_not_exist.google.com => -2147012889
[The server name or address could not be resolved]
If you want the actual content behind a URL, try oHttp.ResponseText. Here's the WinHTTP reference if you are interested in other capabilities as well.
Option Explicit
Dim aUrlList
aUrlList = Array( _
"http://www.google.com", _
"http://www.google.com/does_not_exist", _
"http://does_not_exist.google.com" _
)
Dim i
For i = 0 To UBound(aUrlList)
WScript.Echo aUrlList(i) & " => " & GetUrlStatus(aUrlList(i))
Next
Function GetUrlStatus(sUrl)
Dim oHttp : Set oHttp = CreateObject("WinHttp.WinHttpRequest.5.1")
On Error Resume Next
With oHttp
.Open "GET", SUrl, False
.Send
End With
If Err Then
GetUrlStatus = Err.Number & " [" & Err.Description & "]"
Else
GetUrlStatus = oHttp.Status & " [" & oHttp.StatusText & "]"
End If
Set oHttp = Nothing
End Function
The way you start IE is external, you have little control over the process once it is started.
A better interactive way is like this
Function GetData(strUrl) 'As String
Set web = CreateObject("InternetExplorer.Application")
web.Navigate strUrl
Do While web.Busy
wscript.sleep 100
Loop
Set doc = Nothing
Do Until Not doc Is Nothing
Set doc = web.Document
Loop
strWebPage = doc.all(1).innerHTML 'This does return the head sections
web.Quit
GetData = strWebPage
End Function
wscript.echo GetData("www.google.com")
UPDATE: As it stands now, it looks like this is not a viable solution. After multiple invocations, IE processes begin to accumulate with this approach as well. It appears this behavior has something to do with IE's session management. IE doesn't like to be abruptly terminated.
I found some very useful information about managing processes via WMI here. Using that as a basis, I came up with the code I show below. One of the nice aspects of the WMI approach is that you are given access to the unique ID for the process. I consider my code a starting point as I'm sure further improvements are possible (including the addition of exception handling).
Perhaps others with deeper knowledge of WMI can offer additional advice.
PS: Hope you like that I wrapped this functionality inside a VBScript class called Process.
Option Explicit
' Const PROG = "notepad.exe"
Const TARGET = "http://www.google.com"
Dim PROG : PROG = "C:\Program Files\Internet Explorer\iexplore.exe " & TARGET
Const ABOVE_NORMAL = 32768 ' what are the other priority constants?
Dim oProc : Set oProc = New Process
oProc.Name = PROG
' oProc.Priority = ABOVE_NORMAL
oProc.Launch
WScript.Echo "Launched '" & PROG & "' with process ID '" & oProc.ID & "'"
WScript.Sleep 5000
oProc.Terminate
WScript.Echo "Process " & oProc.ID & " killed."
Set oProc = Nothing
' ----------------------------------------------------------------------
Class Process
Public Computer
Public Name
Public Priority
Public ID
Public IsRunning
Private mHandle
Private Sub Class_Initialize()
Me.Computer = "."
Me.Name = Null
Me.ID = -1
Me.Priority = Null
Me.IsRunning = False
Set mHandle = Nothing
End Sub
Private Sub Class_Terminate()
Set mHandle = Nothing
End Sub
Public Sub Launch()
Dim oWmi, oStartup, oConfig
Dim nPid
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& Me.Computer & "\root\cimv2")
Set oStartup = oWmi.Get("Win32_ProcessStartup")
If Not IsNull(Me.Priority) Then
Set oConfig = oStartup.SpawnInstance_
oConfig.PriorityClass = Me.Priority
End If
Set mHandle = GetObject("winmgmts:root\cimv2:Win32_Process")
mHandle.Create Me.Name, Null, oConfig, nPid
' WScript.Echo "TypeName Is [" & TypeName(mHandle) & "]"
Me.ID = nPid
Me.IsRunning = True
End Sub
Public Sub Terminate()
' mHandle.Terminate ' hmmm, doesn't work...
Dim oWmi
Dim colProcessList, oProc
Set oWmi = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" _
& Me.Computer & "\root\cimv2")
Set colProcessList = oWmi.ExecQuery _
("Select * from Win32_Process Where ProcessId = '" & Me.ID & "'")
For Each oProc In colProcessList ' should be only one process
' WScript.Echo "TypeName Is [" & TypeName(oProc) & "]"
oProc.Terminate
Next
Me.IsRunning = False
End Sub
End Class
Could you consider using a lightweight command line URL retrieval program, like CURL ( http://curl.haxx.se/docs/manpage.html ) or WGET ( http://www.gnu.org/software/wget/ )? These programs can be executed from the command line quite simply:
wget http://www.google.com
curl http://www.google.com
You can execute them from VBScript like this:
sub shell(cmd)
' Run a command as if you were running from the command line
dim objShell
Set objShell = WScript.CreateObject( "WScript.Shell" )
objShell.Run(cmd)
Set objShell = Nothing
end sub
shell "wget http://www.google.com"
The only downside to this is that WGET and CURL won't execute javascript, download affiliated images, or render the HTML; they will simply download the web page. In my experience, I use CURL and WGET regularly as long as I only have to retrieve a single HTML page; but if I have to render something or trigger AJAX functions I use an automatable web browser toolkit like Selenium, WATIN, or IMacros.
I am attempting to create a VBscript that will kill a Windows process that is passed in as a parameter (argument). I have the following code and believe the problem to be at either line 8, 18, or 24 but am not sure what the problem is. When I replace that code with notepad.exe instead of the variable, it works. Any help would be great. Thanks.
Dim prcid
Dim check
Dim Inp
Set Inp = WScript.Arguments
check=0
Set objService = GetObject("winmgmts:")
For Each Process In objService.InstancesOf("Win32_process")
If process.name= "Inp" Then
prcid=process.processid
check=1
Exit For
End If
Next
If check =0 Then
WScript.Quit [ExitCode]
End if
For Each process In objService.InstancesOf("Win32_process")
If process.name= "Inp" Then
If process.processid=prcid Then
strComputer = "."
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colProcessList = objWMIService.ExecQuery _
("SELECT * FROM Win32_Process WHERE Name = 'Inp'")
For Each objProcess in colProcessList
objProcess.Terminate()
Next
End If
Exit For
End If
Next
You are using a variable as a literal:
1. process.name= "Inp" should be process.name = Inp (twice)
2. "SELECT * FROM Win32_Process WHERE Name = 'Inp'" should be "SELECT * FROM Win32_Process WHERE Name = '" & Inp & "'"
And you are using the WScript.Arguments object incorrect: to get the first argument from the command line, use WScript.Arguments(0)
Disclaimer: I did not test your code with these enhancements, I just spotted these errors on first sight. There could be more lurking inside.
Are you sure there is no extension for Inp because when I ran the script on my windows XP system it worked fine. Also on my system all the process have an extension, except for
System and System Idle Process. If there is an extension you will need to use it.
I found this on the internet it has been tested on a lot of systems
Verified on the following platforms
Server 8 No
Windows Server 2008 R2 Yes
Windows Server 2008 Yes
Windows Server 2003 Yes
Windows 8 No
Windows 7 Yes
Windows Vista Yes
Windows XP Yes
Windows 2000 Yes
Script name: WMI_KillProcess.vbs
Created on: 10/05/2010
Author: Dennis Hemken
Purpose: This function kills a process by name,
which is running on a special pc in the network.
Dim strComputer
strComputer = "."
fct_KillProcess "acrord32", strComputer
' or
' strComputer = "192.168.2.13"
' fct_KillProcess "outlook", strComputer
Public Function fct_KillProcess(strProcessName, strComputer)
Dim objWMI
Dim colServices
Dim objService
Dim strServicename
Dim ret
Set objWMI = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colServices = objWMI.InstancesOf("win32_process")
For Each objService In colServices
strServicename = LCase(Trim(CStr(objService.Name) & ""))
If InStr(1, strServicename, LCase(strProcessName), vbTextCompare) > 0 Then
ret = objService.Terminate
End If
Next
Set colServices = Nothing
Set objWMI = Nothing
End Function
In WiX I have a vbScript for use in a Custom Action that will return ListItems of Network Printers. I want to use these ListItems to populate the ComboBox at Install Time because I won't know the printer names on the users system until after starting the installation.
Here is the vbScript. It currently outputs to a text file pending how to work with it to answer my question.
Const ForWriting = 2
Set objNetwork = CreateObject("Wscript.Network")
strName = objNetwork.UserName
strDomain = objNetwork.UserDomain
strUser = strDomain & "\" & strName
strText = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = FALSE")
For Each objPrinter in colPrinters
strText = strText & "<ListItem Text=""" & objPrinter.Name &""" Value="""& objPrinter.Name &"""/>" & vbcrlf
Next
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile _
("C:\Scripts\Printers.txt", ForWriting, True)
objFile.Write strText
objFile.Close
And this is the output :
<ListItem Text="\\xfiles\Canon iR3030 PCL6" Value="\\xfiles\Canon iR3030 PCL6"/>
<ListItem Text="\\xfiles\HP2110" Value="\\xfiles\HP2110"/>
I am hoping to be able to use this output as ListItems for my ComboBox.
<Control Type="ComboBox" Property="cboPrinters_Prop" Id="cboPrinters" Width="206" Height="16" X="19" Y="139" ComboList="yes">
<ComboBox Property="cboPrinters_Prop">
<ListItem Text="" Value=""/>
</ComboBox>
</Control>
If there is a better way or I am approaching this all wrong(I keep trying to think like a developer) please feel free to correct me. I am thick skinned... :)
As I suspected toward the end of my original post there was a different way and I was going about it wrong. At least somewhat. Being new to WiX I didn't take into consideration the fact that the values should be added to the database. Given that I hadn't run across this yet in my experiences I got to learn something new.
So here's what I did to get the values to display in my ComboBox:
1.) Script above was modified to use the collection to send the appropriate values to the database table named ComboBox.
2.) Using Orca I added the ComboBox table. BTW it just holds the information for ListItems used in, wait for it... ComboBoxes.
3.) Added the appropriate Custom Action.
4.) Called it just before the form loads.
Here's the vb Script:
Const ERROR_SUCCESS = 0
Const ERROR_INSTALL_FAILURE = 1603
Const msiViewModifyInsertTemporary = 7
Function LogInfo(msg)
Dim rec
Set rec = Session.Installer.CreateRecord(1)
rec.StringData(0) = msg
LogInfo = Session.Message(&H04000000, rec)
End Function
Function GetNetworkPrinters()
Dim oView, oReccombo
Dim r
LogInfo "INSIDE GetNetworkPrinters"
Set objNetwork = CreateObject("Wscript.Network")
strName = objNetwork.UserName
strDomain = objNetwork.UserDomain
strUser = strDomain & "\" & strName
strText = ""
strComputer = "."
Set objWMIService = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set colPrinters = objWMIService.ExecQuery _
("Select * From Win32_Printer Where Local = FALSE")
Set oView = Session.Database.OpenView("SELECT * FROM `ComboBox`")
oView.Execute
r = 1
For Each objPrinter in colPrinters
r = r + 1
LogInfo "THE PRINTER NAME IS " & objPrinter.Name
Set oReccombo = Session.Installer.CreateRecord(4)
oReccombo.StringData(1) = "cboPrinters_Prop"
oReccombo.IntegerData(2) = r
oReccombo.StringData(3) = objPrinter.Name
oReccombo.StringData(4) = objPrinter.Name
LogInfo "Made it to the call to insert the record"
oView.Modify msiViewModifyInsertTemporary, oReccombo
Next
oView.Close
GetNetworkPrinters = ERROR_SUCCESS
Set oView = Nothing
End Function
Add a Binary entry:
<Binary Id="GetNetworkPrinters" SourceFile="*Enter the full path to the script here* \GetNetworkPrinters.vbs" />
Add the Custom Action:
<CustomAction Id="AddPrintersToComboBox" BinaryKey="GetNetworkPrinters" VBScriptCall="GetNetworkPrinters" Execute="immediate" Return="check" HideTarget="no" Impersonate="yes" />
Add the Call to the Custom Action in the InstallUISequence and the AdminUISequence:
<Custom Action="AddPrintersToComboBox" Before="MaintenanceForm"></Custom>
That is it... Now of course the script needs to be cleaned up to generate better log information and also needs better (some) error handling but it definitely works.
I hope this helps others...
How can I determine if a user, in say Access, is a member of an Active Directory Security Group?
I'd rather not build a whole authentication system into my little Access DB.
Thanks
Allain found this online
Function IsMember(strDomain As String, strGroup _
As String, strMember As String) As Boolean
Dim grp As Object
Dim strPath As String
strPath = "WinNT://" & strDomain & "/"
Set grp = GetObject(strPath & strGroup & ",group")
IsMember = grp.IsMember(strPath & strMember)
End Function
You can get the Windows account info by way of the USERDOMAIN and USERNAME environment vars:
Function GetCurrentUser() As String
GetCurrentUser = Environ("USERNAME")
End Function
Function GetCurrentDomain() As String
GetCurrentDomain = Environ("USERDOMAIN")
End Function
Putting it all together:
If IsMember(GetCurrentDomain, "AD Group", GetCurrentUser) Then
DoStuff()
End If
I'm late to the game with this, but the code you need is below. It gets user names and domain names for you.
Note that I'm not using objGroup.Ismember - that's actually the correct method to use - I'm enumerating the list of groups that the user is in, because it's much easier to debug and there's no appreciable performance penalty.
...And I lifted the code from an earlier project, in which I needed to check membership of a 'Read Reports' group, an 'Edit Data' Group, and an 'Edit System Data' group, so that I could choose which controls to enable and which forms to open read-only. Enumerating groups once was faster than three separate checks.
Public Function UserIsInGroup(GroupName As String, _
Optional Username As String, _
Optional Domain As String) As Boolean
'On Error Resume Next
' Returns TRUE if the user is in the named NT Group.
' If user name is omitted, current logged-in user's login name is assumed.
' If domain is omitted, current logged-in user's domain is assumed.
' User name can be submitted in the form 'myDomain/MyName'
' (this will run slightly faster)
' Does not raise errors for unknown user.
'
' Sample Usage: UserIsInGroup( "Domain Users")
Dim strUsername As String
Dim objGroup As Object
Dim objUser As Object
Dim objNetwork As Object
UserIsInGroup = False
If Username = "" Then
Set objNetwork = CreateObject("WScript.Network")
strUsername = objNetwork.UserDomain & "/" & objNetwork.Username
Else
strUsername = Username
End If
strUsername = Replace(strUsername, "\", "/")
If InStr(strUsername, "/") Then
' No action: Domain has already been supplied in the user name
Else
If Domain = "" Then
Set objNetwork = CreateObject("WScript.Network")
Domain = objNetwork.UserDomain
End If
strUsername = Domain & "/" & strUsername
End If
Set objUser = GetObject("WinNT://" & strUsername & ",user")
If objUser Is Nothing Then
' Insert error-handler here if you want to report an unknown user name
Else
For Each objGroup In objUser.Groups
'Debug.Print objGroup.Name
If GroupName = objGroup.Name Then
UserIsInGroup = True
Exit For
End If
Next objGroup
End If
Set objNetwork = Nothing
Set objGroup = Nothing
Set objUser = Nothing
End Function
Hopefully this late submission is of use to other developers: when I looked this up for the first time, back in 2003, it was like nobody had ever used AD groups in Excel or MS-Access.
Found this online
Function IsMember(strDomain As String, strGroup _
As String, strMember As String) As Boolean
Dim grp As Object
Dim strPath As String
strPath = "WinNT://" & strDomain & "/"
Set grp = GetObject(strPath & strGroup & ",group")
IsMember = grp.IsMember(strPath & strMember)
End Function
Now, I only need the account name of the current user. Too bad Application.CurrentUser doesn't give me their Domain Account name.