我是否可以使用开关/netonly (类型9登录)启动进程,就像下面命令中的/netonly一样。
"Start-Process powershell -Credential mydomain\mydomainAdmin -ArgumentList '-noprofile -command &{Start-Process notepad -verb runas}'"基本上我登录使用管理员帐户,我想从一些共享复制我的帐户有权访问。我要使用类型9登录(/netonly开关)和传递凭据。
使用下面的命令,我可以做到这一点,但我必须输入密码。
" runas /netonly /user:myadmin\myaccount "robocopy source destination" "请帮我指出正确的方向
发布于 2017-10-03 11:09:53
使用模拟,您可以使用在脚本或其他地方定义的凭据进行netonly类型登录,而无需每次都键入凭据。
(请注意,在此示例中,模拟Write-host时不会写入不同的用户名。这特别是因为新凭证登录类型(INT 9)仅在访问远程资源时模拟所需的用户。)
$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
https://stackoverflow.com/questions/46535321
复制相似问题