[英]C# Can not run PowerShell script to add user Active Directory
[英]How to run process using a registered application in Azure Active Directory with powershell script
我正在尝试创建一个使用系统用户帐户启动进程的powershell脚本。 我想在AAD中使用已注册的应用程序作为该系统用户帐户。
这是我创建的代码。
$ApplicationId = "My ApplicationID"
$ServicePrincipalSecret = "My Application's secret Key"
$SecurePassword = ConvertTo-SecureString –String $ServicePrincipalSecret –AsPlainText -Force
$Credential = New-Object System.Management.Automation.PsCredential ($ApplicationId, $SecurePassword)
Start-Process -FilePath notepad -Credential $Credential
这段代码不起作用。 并返回以下错误按摩。
Start-Process:由于错误,无法运行此命令:用户名或密码不正确。 在行:1 char:1 + Start-Process -FilePath notepad -Credential $ Credential + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo:InvalidOperation :( :) [Start-Process],InvalidOperationException + FullyQualifiedErrorId:InvalidOperationException,Microsoft.PowerShell.Commands。 StartProcessCommand
我尝试按照命令检查我的$ Credential是否正确,并成功连接Azure。 所以我的Credential对象是正确创建的。
Connect-AzureRmAccount -Credential $ Credential -Tenant“My Azure tenant ID”-ServicePrincipal
我也尝试过以下代码。
$psinfo = New-Object System.Diagnostics.ProcessStartInfo
$psinfo.FileName = "notepad"
$psinfo.UserName = $ApplicationId
$psinfo.Password = $SecurePassword
$psinfo.Verb = "runasuser"
$psinfo.UseShellExecute = 0
$psinfo.LoadUserProfile = 0
$ps = New-Object System.Diagnostics.Process
$ps.StartInfo = $psinfo
$ps.Start()
这段代码也不起作用。 并返回以下错误按摩。
用“0”参数调用“Start”的异常:“存根收到坏数据”在第1行:char:1 + $ ps.Start()+ ~~~~~~~~~~ + CategoryInfo: NotSpecified:(:) [],MethodInvocationException + FullyQualifiedErrorId:Win32Exception
使用有资格管理Azure Active Directory的用户帐户与实际运行脚本的用户不同,是否有任何好的方法来启动流程? 如果有人给我任何建议将不胜感激。
谢谢。
尝试类似下面的东西。它对我有用。
$uid = "Domain\username"
$pwd = "#pwd"
$Args = "-Verb RunAs -Wait -passthru"
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $Uid, ($pwd | ConvertTo-SecureString -AsPlainText -Force)
Start-Process -FilePath C:\windows\system32\system32\notepad.exe -Credential ($cred) -Argumentlist $Args
这是为了当你想要提示凭证时,否则你可以简单地使用下面的命令
$MyCredential=New-Object -TypeName System.Management.Automation.PSCredential `
-ArgumentList $Uid, ($pwd | ConvertTo-SecureString -AsPlainText -Force)
Start-Process -FilePath C:\windows\system32\system32\notepad.exe -Credential -Credential Get-Credential -Argumentlist $Args
请检查一下,看看它是否适合您,否则将进一步了解您。
我发现我的问题是因为对不存在的用户进行身份验证。 Azure Active Directory中的已注册应用程序不会同步到Azure Active Directory域服务。 因此,即使我尝试使用AAD应用程序服务主体凭据对AADDS执行更改/更新,身份验证也会失败。 使用AAD用户对象而不是服务主体为我工作。 谢谢。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.