我有一个PowerShell脚本在PowerShell中运行时运行正常,但在尝试从c#运行时会抛出错误。 powershell脚本正在尝试连接到lync online(skype for business)并列出lync在线用户。
以下是从c#
调用powershell脚本的代码 public static void ImportLyncUsers()
{
RunspaceConfiguration config = RunspaceConfiguration.Create();
using (Runspace myRs = RunspaceFactory.CreateRunspace(config))
{
myRs.Open();
RunspaceInvoke scriptInvoker = new RunspaceInvoke(myRs);
scriptInvoker.Invoke("Set-ExecutionPolicy Unrestricted");
using (PowerShell powerShellInstance = PowerShell.Create())
{
powerShellInstance.Runspace = myRs;
// Import module.
powerShellInstance.Commands.AddCommand("Import-Module")
.AddArgument(
@"C:\Program Files\Common Files\Microsoft Lync Server 2013\Modules\LyncOnlineConnector\LyncOnlineConnector.psd1");
powerShellInstance.Invoke();
powerShellInstance.Commands.Clear();
var filePath = @"F:\PresensoftNewTrunk\Trunk\Lync Archiver\Exchange\Scripts\ImportUsers.ps1";
powerShellInstance.Commands.AddScript(System.IO.File.ReadAllText(filePath));
Collection<PSObject> psOutput = powerShellInstance.Invoke(); //Getting exception here.
// check the other output streams (for example, the error stream)
if (powerShellInstance.Streams.Error.Count > 0)
{
// error records were written to the error stream.
// do something with the items found.
}
}
}
}
这是我在尝试从c#运行ps脚本时遇到的错误。
Message : Object reference not set to an instance of an object.
Data : {}
InnerException :
TargetSite : Void .ctor(IDCRLMode)
StackTrace : at Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor(IDC
RLMode mode)
at Microsoft.Rtc.Admin.Authentication.ManagedIdcrl..ctor()
at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicket
Cmdlet.CreateAndInitializeManagedIdcrl()
at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicket
Cmdlet.get_ManagedIdcrl()
at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicket
Cmdlet.GetLiveIdToken(String remoteFqdn, Int32 port,
PSCredential creds)
at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicket
Cmdlet.ConnectToWebTicketService(String fqdn, Int32 port,
PSCredential creds)
at Microsoft.Rtc.Management.LyncOnlineConnector.GetWebTicket
Cmdlet.BeginProcessing()
at System.Management.Automation.Cmdlet.DoBeginProcessing()
at
System.Management.Automation.CommandProcessorBase.DoBegin()
HelpLink :
Source : Microsoft.Rtc.Admin.AuthenticationHelper
HResult : -2147467261
这是ps脚本的一部分
$secpasswd = New-Object -TypeName System.Security.SecureString
$password = [LyncFoundation.Helpers.StringHelper]::Decrypt($exchangeServer.Password);
$password.ToCharArray() | ForEach-Object { $secpasswd.AppendChar($_) }
$cred = New-Object System.Management.Automation.PSCredential ($exchangeServer.UserName,$secpasswd)
#Get Lync Users
Import-Module LyncOnlineConnector
try{
$CSSession = New-CsOnlineSession -Credential $cred
}
catch [System.Net.WebException]{
if ($_.Exception.Message.Contains("Unable to query AutoDiscover")){
$CSSession = New-CsOnlineSession -Credential $cred -OverridePowershellUri https://admin0a.online.lync.com/OcsPowershellLiveId
}
else{
throw $_.Exception
}
}
我想知道为什么脚本直接从powershell执行时运行顺利,但在尝试从c#调用时给出了错误。我需要设置什么样的环境才能通过此错误?
答案 0 :(得分:3)
代码看起来不错。我遇到了类似的问题和,因为PowerShell的Lync Online是64位的,你必须为该平台编译而不是默认的任何CPU。