0

我正在开发一个 Windows 8.1 商店应用程序,我想获取 GAL 并使用 power shell 脚本添加 GAL。当我尝试添加对 System.Security.SecurityString 的引用时,它会出现如下错误。我正在尝试使用我的 office365 管理员详细信息进行身份验证,但在构建应用程序时出现错误。

'System.Management.Automation.PSCredential' 不包含带有 2 个参数的构造函数

但它包含一个带有两个参数的构造函数,当我构建 Windows 窗体应用程序时,相同的代码运行良好。Windows 8.1 应用程序会出现什么问题?

 System.Uri psURL = new Uri("https://ps.outlook.com/powershell/");
  System.Security.SecureString securePassword1 = safeString("test");
  System.Management.Automation.PSCredential creds1 = new System.Management.Automation.PSCredential("test", securePassword1);

问题

1) System.Security.SecurityString 是否与 Windows 8.1 应用程序或来自 PSCredential 的此错误兼容,为什么因为当我删除安全字符串引用时,PSCredential 错误没有发生,但出现这样的安全字符串错误?

2) 有没有其他方法可以通过 c# 代码在没有安全字符串的情况下连接到 powershell?

4

1 回答 1

0

System.Security.SecureString是 .NET 框架的一部分。

在您的示例代码中看不到如何将字符串转换为安全字符串:

$secure_string_pwd = convertto-securestring "P@ssW0rD!" -asplaintext -force
$username = "username@yourdomain.com"
$cred = New-Object System.Management.Automation.PSCredential $username, $secure_string_pwd

尝试使用以下代码连接到 O365:

Import-Module MSOnline
$O365Cred = Get-Credential
$O365Session = New-PSSession –ConfigurationName Microsoft.Exchange 
     -ConnectionUri https://ps.outlook.com/powershell -Credential $O365Cred 
     -Authentication Basic -AllowRedirection
Import-PSSession $O365Session
Connect-MsolService –Credential $O365Cred

您可能还会发现这篇文章很有趣 - 示例代码取自那里......

于 2015-08-28T12:42:50.597 回答