尝试从 C# 运行 powershell 脚本时出错
Getting error when trying to run powershell script from C#
我有一个 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# 调用时却给我错误。我需要设置什么样的环境才能通过这个错误?
代码看起来不错。我遇到了类似的问题,因为 PowerShell 的 Lync Online 是 64 位的,所以你必须 为该平台 编译而不是默认的 Any CPU.
我有一个 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# 调用时却给我错误。我需要设置什么样的环境才能通过这个错误?
代码看起来不错。我遇到了类似的问题,因为 PowerShell 的 Lync Online 是 64 位的,所以你必须 为该平台 编译而不是默认的 Any CPU.