PowerShell中的cmdkey不';作为登录脚本运行时无法工作?

PowerShell中的cmdkey不';作为登录脚本运行时无法工作?,powershell,Powershell,正在尝试在PowerShell登录脚本中使用cmdkey在凭据管理器中存储凭据。当脚本从PowerShell ISE运行时,一切正常,但当它通过组策略作为登录脚本运行时,除cmdkey外,一切正常。我一辈子都搞不明白为什么除了在登录时运行脚本外,cmdkey在任何地方都可以工作 # Checks if CRM for Outlook is isntalled by checking the folder path $installed = Test-Path "C:\Program Files

正在尝试在PowerShell登录脚本中使用cmdkey在凭据管理器中存储凭据。当脚本从PowerShell ISE运行时,一切正常,但当它通过组策略作为登录脚本运行时,除cmdkey外,一切正常。我一辈子都搞不明白为什么除了在登录时运行脚本外,cmdkey在任何地方都可以工作

# Checks if CRM for Outlook is isntalled by checking the folder path
$installed = Test-Path "C:\Program Files (x86)\Microsoft Dynamics CRM"
# Checks if the CRM has already been configured using the CoreConfigured registry entry
$configured = Get-ItemProperty -Path HKCU:\software\Microsoft\MSCRMClient -Name     "CoreConfigured"

# If CRM is installed and not configured, configure it, if CRM is not installed or     installed and configured, exit
If ($installed -eq "True" -and $configured.CoreConfigured -ne 1) { 

    $message1 = New-object -ComObject Wscript.Shell
    $message1.Popup("Preparing to configure Microsoft CRM for Outlook, please make sure     Outlook is closed.",10,"Systems")

    # Prompts user for email address and Password to configure CRM for Outlook
    $c = Get-Credential -Message "To confgiure CRM, please enter your email address and password:"

    # puts user credentials into Windows Credential Manager using required CRM URLs 
    cmdkey /generic:Microsoft_CRM_https://disco.crm.dynamics.com/ /user: $c.Username  /pass: $c.Password | Out-Null
    cmdkey /generic:Microsoft_CRM_https://disco.crm4.dynamics.com/ /user: $c.Username /pass: $c.Password | Out-Null


    $message2 = New-Object -ComObject Wscript.Shell
    $message2.Popup("Please wait, a notification will appear when the configuration is complete.",10,"Systems")

    # Silenty runs the CRM configuration Wizard with custom XML file
    $exe = "C:\Program Files (x86)\Microsoft Dynamics CRM\Client\ConfigWizard\Microsoft.Crm.Application.Outlook.ConfigWizard.exe"
   &$exe -p /Q /i 'C:\Program Files (x86)\Microsoft Dynamics CRM\Default_Client_Config.xml' /xa /l 'c:\temp\crminstall.txt' | Out-Null

    $message3 = New-Object -ComObject Wscript.Shell
    $message3.Popup("Configuration complete! You may now open Outlook!",10,"Systems")

} 
else {
    exit    
}

我想象cmdkey正在使用Microsoft的数据保护API(DPAPI)加密凭据,以便只有当前用户才能检索凭据。除非加载了用户的会话,否则无法使用此API。脚本运行时,在登录过程中加载DPAPI所需的安全信息可能为时过早。我不确定登录脚本是如何工作的,但请尝试在登录脚本中设置一个延迟,直到返回值为止

以下是使用DPAPI加密的PowerShell代码:

$scope = [Security.Cryptography.DataProtectionScope]::CurrentUser
$encryptedBytes = [Security.Cryptography.ProtectedData]::Protect( $plainBytes, $null, $scope )
$decryptedBytes = [Security.Cryptography.ProtectedData]::Unprotect( $encryptedBytes, $null, 0 )

在logn脚本中添加一个循环,尝试加密/解密一些随机字节数组,直到成功。

我遇到了相同的问题:当作为用户登录脚本运行时,cmdkey在Powershell中不工作

在我的例子中,问题与用户的组成员资格有关。该用户是“超级用户”组的成员,但不是“用户”组(或任何其他组)的成员

据介绍,“超级用户”组“没有默认用户权限”,而“用户”组有权“执行常见任务,如运行应用程序、使用本地和网络打印机”


解决方案是将我的用户添加到“用户”组。这立即解决了问题,并允许cmdkey在Powershell登录脚本中工作。

我在调用cmdkey.exe的Powershell GPO登录脚本中遇到同样的问题。凭据已填充到用户的凭据管理器中,但管理员未显示凭据。我发现凭据正在凭据管理器中保存,但用于提升的用户。如果在提升的命令提示符下运行cmdkey/list,您将在那里看到凭据

什么是
cmdkey
?cmdlet?节目?它在磁盘上的何处?cmdkey是位于C:\Windows\System32\cmdkey.exe中的命令行程序,用于创建、列出和删除存储的用户凭据。尽管它应该可以工作,但是使用exe的完整路径仍然是一个很好的实践。以防万一。。。永远不知道谁会破坏这条路<代码>$env:SystemRoot\System32\cmdkey.exe。我还发现,对于exe,您需要将这样的代码
$c.Username
封装在$()中,以确保其正确计算
$($c.Username)
。如果关闭ISE并从新的ISE会话运行代码,它是否工作?客户端将无法访问您在powershell中可能拥有的配置文件信息。