unable to run Exchange Powershell through vb.net application - vb.net

So I'm going round in circles trying to get this to work, I've been trying for two days and I just can't figure it out.
I have the following vb function that takes a created powershell script, and should run it in powershell. Everything works fine, until the point at which the command pipeline is invoked. At this point, no commands run.
As you can see, I have tried to add the Microsoft.Exchange.Management.PowerShell.E2010 snapin to the runspace, it didn't like that at all stating something along the lines of the snapin didnt exist (which it does), and also when I run the code as shown, no commands are recognised as valid. I even added the specific command "Add-PSSnapin" to try and load any Exchange snapins, but it states that "Add-PSSnapin" is not recognised as a valid command.
If I pause the program just before the commands are involked, I can see every command within the pipeline, in the correct format. If I copy and paste the command text in the pipeline directly into a powershell window, it runs fine.
My code is below, any suggestions welcome.
edit: I have also tried adding the line "Add-PSSnapin Ex" (with an asterisk each side of Ex - I cant figure the formatting out on this, sorry)
to try and load the Exchange PS Snapins as the first thing the script would run (opposed to setting this up in the runspace) but no luck
Private Function scriptRunner(ByVal scripttorun As String) As String
Dim initial As InitialSessionState = InitialSessionState.CreateDefault()
Dim result As String = ""
Dim lineFromScript As String = ""
Dim reader As New StreamReader(tempScript)
Dim rsConfig As RunspaceConfiguration = RunspaceConfiguration.Create()
Dim snapInException As New PSSnapInException
Dim strUserName As String = "DOMAIN\USER"
Dim strPassword As String = "PASSWORD"
Dim SecuredPSWD As New System.Security.SecureString()
For Each character As Char In strPassword
SecuredPSWD.AppendChar(character)
Next
Dim wsmConnectionInfo As WSManConnectionInfo
Dim strSystemURI As String = "http://SERVER.DOMAIN/powershell?serializationLevel=Full"
Dim strShellURI As String = "http://schemas.microsoft.com/powershell/Microsoft.Exchange"
Dim powerShellCredentials As PSCredential = New PSCredential(strUserName, SecuredPSWD)
wsmConnectionInfo = New WSManConnectionInfo(New Uri(strSystemURI), strShellURI, powerShellCredentials)
Dim runspace As Runspace = RunspaceFactory.CreateRunspace(wsmConnectionInfo)
Runspace.Open()
' runspace.RunspaceConfiguration.AddPSSnapIn("Microsoft.Exchange.Management.PowerShell.E2010", snapInException)
Dim pipeLine As Pipeline = runspace.CreatePipeline()
Dim command As Command = New Command("")
' TEST >> pipeLine.Commands.Add("Add-PSSnapin *Ex*")
Do While reader.Peek() <> -1
lineFromScript = Nothing
lineFromScript = reader.ReadLine()
pipeLine.Commands.Add(lineFromScript)
'command.Parameters.Add(lineFromScript)
'pipeLine.Commands.Add(command)
Loop
'' Run the contents of the pipeline
Dim psObjCollection As Collection(Of PSObject) = pipeLine.Invoke()
runspace.Close()
runspace.Dispose()
Return ""
End Function

I ended up working around the problem rather than fixing it.
I moved the script code into the vb.net application, and wrote each line to a file, i.e.
writer.WriteLine("Add-PSSnapin *Ex*")
Then I loaded the script through PowerShell as an application;
Dim exeStartInfo As System.Diagnostics.ProcessStartInfo
Dim exeStart As New System.Diagnostics.Process
exeStartInfo = New System.Diagnostics.ProcessStartInfo("C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe")
exeStartInfo.Arguments = ("-command work\scriptbuilder.ps1")
exeStartInfo.WorkingDirectory = "C:\ExchangeManager\"
exeStartInfo.UseShellExecute = False
exeStart.StartInfo = exeStartInfo
exeStart.Start()
exeStart.Close()
Not ideal but it got the job done.

Related

Read and Write to specific line in textfile with VB.Net 2

I have tried using the solution (given below) for the "Read and Write to specific line in textfile with VB.Net" problem asked 4 years ago
Dim filePath As String = "E:\myFile.txt"
Dim lines() As String = System.IO.File.ReadAllLines(filePath)
If lines.Length > 4 AndAlso lines(4).StartsWith("ORIGIN ") Then
lines(4) = "ORIGIN ""250"""
System.IO.File.WriteAllLines(filePath, lines)
End If
But each time I encounter the following error:
The process cannot access the file 'file_path' because it is being
used by another process.
Any idea as to why this happens?
Your code works OK for me. System.IO.File.ReadAllLines() is supposed to close the file afterwards.
Maybe something else is preventing write access to the file. (Some application you're using to see if it's working?!) To check that, replace
Dim lines() As String = System.IO.File.ReadAllLines(filePath)
with
Dim lines() As String = {"0","1","2","3","ORIGIN 4", "5"}
and see if it still fails.

VB.Net Process.Start - Verb

I want my vb.net Program to launch notepad and elevate the credentials however there is no prompt and the program just opens.
Dim process As System.Diagnostics.Process = Nothing
Dim processStartInfo As System.Diagnostics.ProcessStartInfo
processStartInfo = New System.Diagnostics.ProcessStartInfo()
processStartInfo.FileName = "notepad.exe"
processStartInfo.Verb = "runas"
processStartInfo.Arguments = ""
processStartInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Normal
processStartInfo.UseShellExecute = True
process = System.Diagnostics.Process.Start(processStartInfo)
There's a lot of unnecessary code there. You're using two lines to declare and set your two variables when you only need one each time. You're setting the FileName property explicitly when you can pass an argument to the constructor and you're also setting three properties to their default values. I just stripped it down to the bare minimum:
Dim psi As New ProcessStartInfo("notepad.exe") With {.Verb = "runas"}
Dim proc = Process.Start(psi)
When I ran that code from a new WinForms app with just a Button on the form and I got a UAC prompt as expected.

running powershell script with parameters with vb.net

i have a powershell script that i want to kick off with vb.net code, however when it gets to the part where i invoke the pipeline i get the error "Cannot process command because of one or more missing mandatory parameters: District County FMS."
the powershell script is very simple, it takes 3 parameters and write-host's the param values back to the shell. pretty much my question is, how do i make it work? thank you everyone for you time.
vb.net(the other professionals i work with have boiled the error down to 'get-process' not being the right command for my scriptParams variable, but we're not sure which command to use)
Sub Main()
Console.WriteLine(RunScript(LoadScript(Directory.GetCurrentDirectory() + "/CreateProject.ps1 ")))
Console.ReadLine()
End Sub
Private Function RunScript(ByVal scriptText As String) As String
' create Powershell runspace
Dim MyRunSpace As Runspace = RunspaceFactory.CreateRunspace()
' open it
MyRunSpace.Open()
' create a pipeline and feed it the script text
Dim MyPipeline As Pipeline = MyRunSpace.CreatePipeline()
MyPipeline.Commands.AddScript(scriptText)
' add an extra command to transform the script output objects into nicely formatted strings
' remove this line to get the actual objects that the script returns. For example, the script
' "Get-Process" returns a collection of System.Diagnostics.Process instances.
MyPipeline.Commands.Add("Out-String")
'[ay]create a command object by bassing the command to the constructor
Dim scriptParams As New Command("get-process")
'[ay]pass parameters to the command
scriptParams.Parameters.Add("District", "D1")
scriptParams.Parameters.Add("County", "Lee")
scriptParams.Parameters.Add("FMS", "101000")
MyPipeline.Commands.Add(scriptParams)
' execute the script
Dim results As Collection(Of PSObject) = MyPipeline.Invoke()
' close the runspace
MyRunSpace.Close()
' convert the script result into a single string
Dim MyStringBuilder As New StringBuilder()
For Each obj As PSObject In results
MyStringBuilder.AppendLine(obj.ToString())
Next
' return the results of the script that has
' now been converted to text
Return MyStringBuilder.ToString()
End Function
Private Function LoadScript(ByVal filename As String) As String
Try
' Create an instance of StreamReader to read from our file.
' The using statement also closes the StreamReader.
Dim sr As New StreamReader(filename)
' use a string builder to get all our lines from the file
Dim fileContents As New StringBuilder()
' string to hold the current line
Dim curLine As String = ""
' loop through our file and read each line into our
' stringbuilder as we go along
Do
' read each line and MAKE SURE YOU ADD BACK THE
' LINEFEED THAT IT THE ReadLine() METHOD STRIPS OFF
curLine = sr.ReadLine()
fileContents.Append(curLine + vbCrLf)
Loop Until curLine Is Nothing
' close our reader now that we are done
sr.Close()
' call RunScript and pass in our file contents
' converted to a string
Return fileContents.ToString()
Catch e As Exception
' Let the user know what went wrong.
Dim errorText As String = "The file could not be read:"
errorText += e.Message + "\n"
Return errorText
End Try
End Function
PowerShell
[CmdletBinding()]
Param(
[Parameter(Mandatory = $True)]
[string]$District,
[Parameter(Mandatory = $True)]
[string]$County,
[Parameter(Mandatory = $True)]
[string]$FMS
)
Write-Host "District: $Dsistrict"
Write-Host "County: $County"
Write-Host "FMS: $FMS"
These examples are in C#, but it's the same API.
https://blogs.msdn.microsoft.com/kebab/2014/04/28/executing-powershell-scripts-from-c/
More C# examples, but these are using the RunspaceFactory API you already have in your pipeline.
Execute PowerShell Script from C# with Commandline Arguments
I'm new to PowerShell but may this help you
in your code
scriptParams.Parameters.Add("District", "D1")
scriptParams.Parameters.Add("County", "Lee")
scriptParams.Parameters.Add("FMS", "101000")
MyPipeline.Commands.Add(scriptParams)
I think this ADD new params to your script so value for already exit params not set
You need to pass param values to your script not add new param
I know this is an old question but there was no good answer and I just spent too long getting this to work to not share my knowledge.
In the VB code, you'd remove the following:
MyPipeline.Commands.AddScript(scriptText)
MyPipeline.Commands.Add("Out-String")
Then, in place of the Out-String addition, add:
Dim myCommand As Command = New Command(scriptText, True)
' Add your parameters here.
' Obviously you can add variables instead of static strings if needed.
myCommand.Parameters.Add("District", "D1")
myCommand.Parameters.Add("County", "Lee")
myCommand.Parameters.Add("FMS", "101000")
' You can add switches in this manner as well. For example, a switch "I"
' You need to include the - with switches
myCommand.Parameters.Add("-i")
' Finally, apply your command object to the pipeline and proceed as normal
MyPipeline.Commands.Add(myCommand)

Writing to Command Line Not Working

An application I need to use (USB capture utility) has a .cmd version that I can call from my Visual Basic code. I am able to launch the application and put it in "command line mode" like this:
Public Class MyClass
Dim StreamWriteUtility As System.IO.StreamWriter
Dim StreamReadUtility As System.IO.StringReader
Dim ProcessInfo As ProcessStartInfo
Dim Process As Process
Public Sub StartUSBCapture(ByVal DataStorageLocation As String)
Dim ProcessInfo As ProcessStartInfo
Dim Process As New Process
ProcessInfo = New ProcessStartInfo("C:\FW_Qualification_Suite\data-center-windows\data-center\bin\datacenter.cmd", "-c ")
ProcessInfo.CreateNoWindow = True
ProcessInfo.UseShellExecute = False 'Must be changed if redirect set to True
ProcessInfo.RedirectStandardInput = True
Process = Process.Start(ProcessInfo)
SWUtility = Process.StandardInput
While True
SWUtility.WriteLine("run") 'Looping for test to ensure this isn't a timing issue
End While
End Sub
End Class
This launches the application and opens a separate command line window that should accept further commands (i.e., capture, run, stop, etc). However, I am having trouble getting those subsequent commands to show up in the command line window. I've tried redirecting the standard input of the process, but still nothing shows up in the console window.
Can anyone tell how I'm supposed to actually get these commands from my Visual Basic program into this application?

Getting DNS Cache in vb.net and returning domains

What's the best way to get the DNS Cache in Visual Basic and returning recently resolved domains? I only need the domains to compare them whit a list.
Function GetDnsCache()
Dim DNSCache As New Process
DNSCache.StartInfo.FileName = "ipconfig"
DNSCache.StartInfo.Arguments = "/displaydns "
DNSCache.StartInfo.UseShellExecute = False
DNSCache.StartInfo.RedirectStandardOutput = True
DNSCache.
DNSCache.Start()
MsgBox(DNSCache.StandardOutput.ReadToEnd())
DNSCache.WaitForExit()
End Function
This is not the cleanest way of doing this and it also takes ages to parse and load.
I would do something like this. It executes ipconfig /displaydns and appends the output to a file. Then, the file is read line by line and displayed wherever you want (I've used a Listbox)
Dim Shell = CreateObject("Wscript.Shell")
Shell.run("cmd /c ipconfig /displaydns >> C:\ipconfig.txt")
Using reader As New IO.StreamReader("C:\ipconfig.txt")
While Not reader.EndOfStream
Dim currentLine As String = reader.ReadLine()
ListBox1.Items.Add(currentLine)
End While
End Using
Should you like to display the info into a MsgBox...
Dim Shell = CreateObject("Wscript.Shell")
Shell.run("cmd /c ipconfig /displaydns >> C:\ipconfig.txt")
Dim reader as As New IO.StreamReader("C:\ipconfig.txt")
MsgBox(reader.ReadToEnd.ToString, MsgBoxStyle.Information)