简体   繁体   English

在Excel VBA中登录用户名 - 而不是运行Excel的帐户(使用RunAs)

[英]Get logged on username in Excel VBA - not the account running Excel (using RunAs)

I have logged into my workstation with my normal domain credentials. 我使用普通的域凭据登录了我的工作站。 We shall call this AccountA. 我们称之为AccountA。

I then use the "run as a different user" to launch Excel. 然后我使用“以其他用户身份运行”来启动Excel。 We shall call this AccountB. 我们称之为账户B. I do this because the permissions needed to query some SQL servers must be done using AccountB. 我这样做是因为查询某些SQL服务器所需的权限必须使用AccountB完成。

At this point, I need to include a subroutine to launch a Shell to create directories and move files on a remote server. 此时,我需要包含一个子例程来启动Shell以创建目录并在远程服务器上移动文件。 AccountB doesn't have (and cannot have) permissions to do this. AccountB没有(也没有)权限来执行此操作。 My shell gives me an Access Denied message. 我的shell给了我一个拒绝访问的消息。 Fine. 精细。

So now, I need to have VBA return the name of AccountA, the account I have used to log into the computer. 所以现在,我需要让VBA返回AccountA的名称,这是我用来登录计算机的帐户。 How do I do this? 我该怎么做呢?

I have seen quite a few examples on this site as well as others that will return the username running Excel (AccountB). 我在这个网站上看到了很多例子以及其他会返回运行Excel(AccountB)的用户名的例子。 However, I have not seen any examples that will pull AccountA information to pass to the Shell via RunAs to carry out my commands with the proper permissions. 但是,我还没有看到任何会通过RunAs将AccountA信息传递给Shell来执行具有适当权限的命令的示例。 Below are some things I have tried, all returning AccountB (the account used via RunAs to launch Excel) 以下是我尝试过的一些事情,都返回了AccountB(通过RunAs使用的帐户启动Excel)

This application will be used by multiple people with permissions to run cmd shell on the remote server, so AccountA cannot be hardcoded and must be programmatically obtained. 具有在远程服务器上运行cmd shell的权限的多个人将使用此应用程序,因此无法对AccountA进行硬编码,并且必须以编程方式获取。

' Access the GetUserNameA function in advapi32.dll and
' call the function GetUserName.
Declare Function GetUserName Lib "advapi32.dll" Alias "GetUserNameA" _
(ByVal lpBuffer As String, nSize As Long) As Long

Sub XXXXXXXXXX()
'//
ThisWorkbook.Sheets("XXXXXXXXXX").Activate ' select the worksheet for use
cmdString = UCase(Trim(ThisWorkbook.ActiveSheet.Cells(4, 4).Value)) 'get XXXXXXXXXX

'MsgBox cmdString
'retval = Shell("cmd.exe /k " & cmdString, vbNormalNoFocus)
'cmdString = "MkDir \\XXXXXXXXXX\g$\Tempski" 'fails - no permission

'Set the Domain
txtdomain = "XXXXXXXXXX"
'Acquire the currently logged on user account
'txtuser = "XXXXXXXXXX"

 ' Dimension variables
 Dim lpBuff As String * 255
 Dim ret As Long

 ' Get the user name minus any trailing spaces found in the name.
 ret = GetUserName(lpBuff, 255)

 If ret > 0 Then
 GetLogonName = Left(lpBuff, InStr(lpBuff, Chr(0)) - 1)
 Else
 GetLogonName = vbNullString
 End If

MsgBox GetLogonName

Dim objNet As Object
On Error Resume Next
Set objNet = CreateObject("WScript.NetWork")
MsgBox "Network username is: " & objNet.UserName
Set objNet = Nothing

MsgBox "Environ username is: " & Environ("USERNAME")

MsgBox "Application username is: " & Application.UserName    
MsgBox "Network username is: " & WindowsUserName

Dim strUser As String
strUser = ActiveDirectory.user()
MsgBox "Welcome back, " & strUser

CurrentWorkbenchUser = Environ("USERDOMAIN") & "\" & Environ("USERNAME")
MsgBox CurrentWorkbenchUser

Set WSHnet = CreateObject("WScript.Network")
UserName = WSHnet.UserName
UserDomain = WSHnet.UserDomain
Set objUser = GetObject("WinNT://" & UserDomain & "/" & UserName & ",user")
GetUserFullName = objUser.FullName

MsgBox GetFullUserName

'//try to pass user credentials to shell - service account cannot run XXXXXXXXXX
'//This Works when txtdomain and txtuser are passed properly (MUST GRAB THESE SOMEHOW)
'retval = Shell("runas /user:" & txtdomain & "\" & txtuser & " ""cmd.exe /k dir /s *.*""", vbNormalFocus)

End Sub

All the bits of code above return the account used to launch Excel — not what I need ie the account I used to log into the computer. 上面的所有代码都返回用于启动Excel的帐户 - 而不是我需要的帐户,即我用来登录计算机的帐户。

The basic approach is to execute the shell with a "run as" parameter. 基本方法是使用“run as”参数执行shell。

This is code ripped from the ms sight and is just here to make sure that the next guy or gal who comes by has a quick reference. 这是从ms视频中删除的代码,只是为了确保来自下一个人或者gal的人有一个快速参考。

Sub RegisterFile(ByVal sFileName As String)
ShellExecute 0, "runas", "cmd", "/c regsvr32 /s " & """" & sFileName & """", "C:\", 0 'SW_HIDE =0
End Sub

As an aside if you only need the account for access to a SQL server, you should be able to just set the account within the connection string in your vba MACRO. 另外,如果您只需要帐户来访问SQL服务器,您应该只需在vba MACRO中的连接字符串中设置帐户即可。 I've done that for an Oracle DB in the past. 我过去曾为Oracle DB做过这样的事情。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM