首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用开关/netonly运行start-process (类型9登录)

使用开关/netonly运行start-process (类型9登录)
EN

Stack Overflow用户
提问于 2017-10-03 07:59:28
回答 1查看 1.1K关注 0票数 0

我是否可以使用开关/netonly (类型9登录)启动进程,就像下面命令中的/netonly一样。

代码语言:javascript
复制
"Start-Process powershell -Credential mydomain\mydomainAdmin -ArgumentList '-noprofile -command &{Start-Process notepad -verb runas}'"

基本上我登录使用管理员帐户,我想从一些共享复制我的帐户有权访问。我要使用类型9登录(/netonly开关)和传递凭据。

使用下面的命令,我可以做到这一点,但我必须输入密码。

代码语言:javascript
复制
" runas /netonly /user:myadmin\myaccount "robocopy source destination" "

请帮我指出正确的方向

EN

回答 1

Stack Overflow用户

发布于 2017-10-03 11:09:53

使用模拟,您可以使用在脚本或其他地方定义的凭据进行netonly类型登录,而无需每次都键入凭据。

(请注意,在此示例中,模拟Write-host时不会写入不同的用户名。这特别是因为新凭证登录类型(INT 9)仅在访问远程资源时模拟所需的用户。)

代码语言:javascript
复制
$ImpersonationLib = Add-Type -Namespace 'Lib.Impersonation' -Name ImpersonationLib -MemberDefinition @"
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool DuplicateToken(IntPtr token, int impersonationLevel, ref IntPtr duplication);

[DllImport("kernel32.dll")]
public static extern Boolean CloseHandle(IntPtr hObject);
"@ -PassThru
[System.IntPtr]$userToken = [System.IntPtr]::Zero
$success = $ImpersonationLib::LogonUser('YourUserName',      # UserName
                                    'DomainOrWorkstationNameIfLocal',       
# Domain
                                    'Password', #Password
                                    9, # New credentials-based logo
                                    0, # LOGON32_PROVIDER_DEFAULT
                                    [ref]$userToken) 

if ($success -eq $false)
{
     Write-Host 'Failure to execute logon user.'
     Exit
}

$Identity = New-Object Security.Principal.WindowsIdentity $userToken
# Close open handles.
if ($userToken -ne [System.IntPtr]::Zero)
{
  $null = $ImpersonationLib::CloseHandle($userToken)
  $userToken = [System.IntPtr]::Zero
}
# Current user.
Write-Host "Before impersonation: UserName: 
$([Security.Principal.WindowsIdentity]::GetCurrent().Name)" -ForegroundColor Cyan
# Do the impersonation.
$context = $Identity.Impersonate()
# New user.
Write-Host "After impersonation: UserName: $([Security.Principal.WindowsIdentity]::GetCurrent().Name)" -ForegroundColor Cyan


# Return to original user.
$context.Undo()
$context.Dispose()
# Old user.
Write-Host "After undoing impersonation: UserName:     
$([Security.Principal.WindowsIdentity]::GetCurrent().Name)"

登录类型参考:MSDN -Logon user function

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/46535321

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档