1
私は現在、Exchangeプログラミングを使用してPowershell CmdLetを作成し、交換してメールボックスを作成しています。VB.NET 1つのコマンドから別のコマンドへのPowershellの結果のパイプ
問題は、私が指定しているExchangeデータベースを「見つけることができません」ため、メールボックスを作成できないことがあります。
私がしようとしているのは、Get-Mailboxを実行し、結果をEnable-Mailboxコマンドにパイプすることです。
Private Function GetCreateCommand() As Command
Dim cmd As New Command("Enable-Mailbox")
cmd.Parameters.Add("Identity", DistinguishedName)
cmd.Parameters.Add("Database")
cmd.Parameters.Add("Alias", UserAlias)
cmd.Parameters.Add("PrimarySmtpAddress", PrimarySMTP)
Return cmd
End Function
:メールボックスを作成する
Private Function GetMailboxCommand() As Command
Dim cmd As New Command("Get-Mailbox")
cmd.Parameters.Add("Database", UserDatabase)
Return cmd
End Function
コマンド:このコマンドは、メールボックスを取得することです
Public Sub CreateMailbox()
Dim getMailbox As Command
getMailbox = GetMailboxCommand()
Dim createCommand As Command
createCommand = GetCreateCommand()
Dim results As String
results = RunCommand(createCommand, getMailbox)
Dim setCommand As Command
setCommand = GetSetCommand()
results = RunCommand(setCommand)
End Sub
:以下
は、私はそれを行うために使用していたコードですすべてのpowershellコマンドを実行するコード:
Private Function RunCommand(ByVal createCommand As Command, ByVal getMailbox As Command) As String
'Create Runspace configuration
Dim rsConfig As RunspaceConfiguration
rsConfig = RunspaceConfiguration.Create()
Dim snapInException As PSSnapInException = Nothing
Dim info As PSSnapInInfo
info = rsConfig.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.Admin", snapInException)
Dim myRunSpace As Runspace
myRunSpace = RunspaceFactory.CreateRunspace(rsConfig)
myRunSpace.Open()
Dim pipeLine As Pipeline
pipeLine = myRunSpace.CreatePipeline()
pipeLine.Commands.Add(getMailbox)
pipeLine.Commands.Add(createCommand)
pipeLine.Commands.Add("Out-String")
Dim commandResults As Collection(Of PSObject)
commandResults = pipeLine.Invoke()
myRunSpace.Close()
Dim sb As String = "Results: "
For Each result As PSObject In commandResults
sb &= result.ToString
Next
Return sb
End Function
エラーが発生したパターンはありますか? –