How can I append a string that contains "" to an existing string in VB .NET? - vb.net

I am working on a utility that will take the contents of a .ps1 script (Powershell), and run it in the context of a VB.NET Winform. Basically it does this by referencing some Powershell DLLs, opening the .ps1 file, reading a line of text and feeding it into an internal string that will run on a button click event.
Here's the full code I have so far:
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")
' 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
' helper method that takes your script path, loads up the script
' into a variable, and passes the variable to the RunScript method
' that will then execute the contents
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()
If curLine.Contains("") Then
fileContents.AppendLine("$Folder=tbpath.selectedpath")
Else
fileContents.Append(curLine + vbCrLf)
If curLine.Contains ($Folder="") Then
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()
Sorry for the lengthy stuff, but the lines I am curious about are the curLine.Contains part. What I'm trying to do is have the parser detect whether or not the line is a specific one (which reads $Folder = "") and replace the empty quotes with a folder path that is stored in a text box (tbpath.selectedtext). Unfortunately, since the Powershell script requires quotes around the path string (in case there are spaces) I am having trouble figuring out how to do what I want it to do.
What should I do there? Should I build what I want into a new variable (maybe vbPath = tbpath.selectedtext) and put it into the new line? Are there any "best practices"?

Double up the quotes
Dim s As String = "$Folder = """""
s =
$Folder = ""
In your code
If curLine.Contains("$Folder = """"") Then
End If

Do you want curLine.Contains() to check for a literal string $Folder=""? If so, you need to put the whole string in double quotes and escape the inner ones. Double quotes are escaped by doubling them. Also, if you want to insert a variable, you need to concatenate it with the string literals, so the command should probably look like this:
If curLine.Contains ("$Folder = """"") Then
fileContents.Append("$Folder = """ & tbpath.selectedpath & """")
End If

Related

Writing/Reading from text files in VB.net

I am a student in computer science and for a project I need to be able to read from a text file in a way that each line is assigned to a space within an array. This should happen so that each line of text file is read in the order that it appears in the text file. I would also appreciate any methods of writing to a text file as well.
If this question is already explained, could you please direct me to the existing answer.
Things to note:
1) I am coding in a console application in VB.NET
2) I am relatively new at coding
You can do it like this:
Dim sFile As String = "D:\File.txt"
Dim aLines As String() = System.IO.File.ReadAllLines(sFile)
System.IO.File.WriteAllLines(sFile, aLines)
Here's a sample from the official documentation:
Imports System.IO
Public Class Test
Public Shared Sub Main()
Dim path As String = "c:\temp\MyTest.txt"
Dim sw As StreamWriter
' This text is added only once to the file.
If File.Exists(path) = False Then
' Create a file to write to.
Dim createText() As String = {"Hello", "And", "Welcome"}
File.WriteAllLines(path, createText)
End If
' This text is always added, making the file longer over time
' if it is not deleted.
Dim appendText As String = "This is extra text" + Environment.NewLine
File.AppendAllText(path, appendText)
' Open the file to read from.
Dim readText() As String = File.ReadAllLines(path)
Dim s As String
For Each s In readText
Console.WriteLine(s)
Next
End Sub
End Class
Remarks
This method opens a file, reads each line of the file, then adds each line as an element of a string array. It then closes the file. A line is defined as a sequence of characters followed by a carriage return ('\r'), a line feed ('\n'), or a carriage return immediately followed by a line feed. The resulting string does not contain the terminating carriage return and/or line feed.
Module Module1
Sub Main()
'Declare four variables
Dim oReader As New System.IO.StreamReader(".\archive01.txt") 'This file has to exist in the aplication current directory.
Dim oWriter As New System.IO.StreamWriter(".\archive02.txt") 'This file will be created by the software.
Dim oArray() As String = {}
Dim oString As String = Nothing
'For reading from .\archive01.txt and to load in oArray().
oString = oReader.ReadLine
While Not oString Is Nothing
If UBound(oArray) = -1 Then 'Ubound = Upper Bound, also exist LBound = Lower Bound.
ReDim oArray(UBound(oArray) + 1)
Else
ReDim Preserve oArray(UBound(oArray) + 1)
End If
oArray(UBound(oArray)) = New String(oString)
oString = oReader.ReadLine
End While
oReader.Close()
'For writing from oArray() to .\archive02.txt.
For i = 0 To oArray.Count - 1 Step 1
oWriter.WriteLine(oArray(i))
Next
oWriter.Close()
End Sub
End Module
Hi, try with this code. It works well. I hope that this helps to you to learn how to do this kind of things. Thank you very much. And happy codding!. :)

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)

Visual Basic Powershell query Server Pending Reboot status

I am new to VB.net & Powershell.
Never the less I am programming an application which querys the PendingReboot status of our Win2012 Servers.
There is this wonderful script which I implemented into my programm which does exactly that:
https://gallery.technet.microsoft.com/scriptcenter/Get-PendingReboot-Query-bdb79542
This is the code I use from Zainnab, to get access from VB2010.net to Powershell an return the result as a string.
'Takes script text as input and runs it, then converts
'the results to a string to return to the user
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")
‘ 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
#
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
......
Dim ausgabe2 As String
ausgabe2 = RunScript(LoadScript("c:\FHServerStat\PS_PR.stat\60.2.ps1"))
------
I managed to get it partially working.
How ever the problem which I encounter is, by executing the script in powershell the rusult is correct.
By executing the script through visual basic, SOME returned values are incorrect, but see for yourself:
enter image description here
Please help me out on this
Regards
Okun

dispose of IO.File.ReadAllLines

I am reading very large text files (6-10 MB). I am splitting the text files in to multiple new text files. There is common "header" and "footer" in the "read" text file that I will store as variable to be called at later time. I can't figure out how to properly dispose of IO.File.ReadAllLines. I'm concerned this will be held in memory if I don't dispose of it properly.
Text.Dispose or Text.Close isn't valid.
Dim testHeader As String
Dim testSite As String
Dim testStart As String
Dim testStop As String
Dim testTime As String
Dim text() As String = IO.File.ReadAllLines("C:\Users\anobis\Desktop\temp.txt")
testHeader = text(0)
testSite = text(text.Length - 4)
testStart = text(text.Length - 3)
testStop = text(text.Length - 2)
testTime = text(text.Length - 1)
text.dispose()
Later in the program I will be initiating another StreamReader and want to avoid conflicts and memory resource issues. I am new at coding so be gentle! Thanks!
' Open temp.txt with "Using" statement.
Using r As StreamReader = New StreamReader("C:\Users\anobis\Desktop\temp.txt")
' Store contents in this String.
Dim line As String
line = r.ReadLine
' Loop over each line in file, While list is Not Nothing.
Do While (Not line Is Nothing)
If line Like (sourceSN.Text + "*") Then 'Substitute in source serial number "xxxxxx*"
file.WriteLine(line)
End If
' Read in the next line of text file.
line = r.ReadLine
Loop
End Using
file.WriteLine(testSite)
file.WriteLine(testStart)
file.WriteLine(testStop)
file.WriteLine(testTime)
' Close transfer.txt file
file.Close()
You don't need to dispose of it. It returns a managed string array, who's lifetime is managed by the garbage collector. Internally, File.ReadAllLines is disposing of the underlying native file handle it created to read all of the lines for you.

trying to read a delimited text file from resources - but it wont run

I'm having a problem where instead of reading a text file from the location string, I changed it to read the text file from the resource location and it breaks my program. I've also used the insert snippet method to get most of this code, so it is safe to say I don't know what is going on. Could some one please help?
'reads the text out of a delimited text file and puts the words and hints into to separate arrays
' this works and made the program run
' Dim filename As String = Application.StartupPath + "\ProggramingList.txt"
'this dosnt work and brings back a Illegal characters in path error.
dim filename as string = My.Resources.ProggramingList
Dim fields As String()
'my text files are delimited
Dim delimiter As String = ","
Using parser As New TextFieldParser(filename)
parser.SetDelimiters(delimiter)
While Not parser.EndOfData
' Read in the fields for the current line
fields = parser.ReadFields()
' Add code here to use data in fields variable.
'put the result into two arrays (the fields are the arrays im talking about). one holds the words, and one holds the corresponding hint
Programingwords(counter) = Strings.UCase(fields(0))
counter += 1
'this is where the hint is at
Programingwords(counter) = (fields(1))
counter += 1
End While
End Using
the error
ex.ToString()
"System.ArgumentException: Illegal characters in path.
at System.IO.Path.CheckInvalidPathChars(String path)
at System.IO.Path.NormalizePathFast(String path, Boolean fullCheck)
at System.IO.Path.NormalizePath(String path, Boolean fullCheck)
at System.IO.Path.GetFullPathInternal(String path)
at System.IO.Path.GetFullPath(String path)
at Microsoft.VisualBasic.FileIO.FileSystem.NormalizePath(String Path)
at Microsoft.VisualBasic.FileIO.TextFieldParser.ValidatePath(String path)
at Microsoft.VisualBasic.FileIO.TextFieldParser.InitializeFromPath(String path, Encoding defaultEncoding, Boolean detectEncoding)
at Microsoft.VisualBasic.FileIO.TextFieldParser..ctor(String path)
at HangMan.Form1.GetWords() in I:\vb\HangMan\HangMan\Form1.vb:line 274" String
The TextFieldParser constructor you use expects the name of a file. Instead, it gets the contents of the file. That goes Kaboom, the file content is not a valid path to a file. You'll need to the constructor that takes a Stream and use the StringReader class to provide the stream. For example:
Dim fields As String()
Dim delimiter As String = ","
Dim fileContent As String = My.Resources.ProggramingList
Dim stringStream as New System.IO.StringReader(fileContent)
Using parser As New TextFieldParser(stringStream)
REM etc...
End Using
This is a bit wasteful of memory but not an issue if the text is less than a megabyte or so. If it is more then you shouldn't put it in a resource.
When you debug this code, what is the value of the variable filename after you read it from My.Resources.GamesList? Is it a valid string, does it point to you're file?