3

I have a .net application that successfully runs Powershell commands that it pulls from text files- until I tried doing one that is more complicated and contains an if condition. The script works correctly from a PS console but in .NET I only know how to pass in a string for the script, which after reading the file, it adds extra stuff like vblf and even if I take it out, it won't work. Is this even possible?

.NET Runtime Error: Server was unable to process request. ---> The term 'False' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again.

.NET code:

'Grab Powershell script from text (.ps1) file
strScript = File.ReadAllText(ScriptFileName)

'inject the arguments into the script
strScript = InsertArguments(strScript, Arguments)

'Open the runspace and create a pipeline if it's not already open
If psRunspace.RunspaceStateInfo.State = RunspaceState.BeforeOpen Then
  psRunspace.Open()
End If

Dim MyPipeline As Pipeline = psRunspace.CreatePipeline()
MyPipeline.Commands.AddScript(strScript)
Dim psResults As Collection(Of PSObject) = MyPipeline.Invoke()

Powershell Script, stored in ps1 file:

new-mailbox -name $argument1 -DisplayName $argument1 -UserPrincipalName $argument2 Room -DomainController $argument5
if ($argument4 -eq "False") {
   Set-CalendarProcessing $argument1 -BookingWindowInDays 400 -DeleteSubject $false -AutomateProcessing autoaccept -AllBookInPolicy $false -BookInPolicy $argument3 -DomainController $argument5        
} else {
   Set-CalendarProcessing $argument1 -BookingWindowInDays 400 -DeleteSubject $false -AutomateProcessing autoaccept -AllBookInPolicy $true -DomainController $argument5
}

When this script is read in, here is a substring of what gets pulled into strScript:

-Room -DomainController mcexdct1" & vbLf & "if (False -eq "False") {"
3
  • Can you post the error you are getting while you are trying this? Commented Feb 27, 2013 at 17:35
  • I updated the original post with the error and then when I looked at what was in strScript, I realized that my if condition is the real problem. I temporarily changed it to say if (5 -eq 5) and then everything worked- no error and the new mailbox was created in full. So I just need to figure out how to rewrite that syntax Commented Feb 27, 2013 at 20:03
  • I suspect the problem is in InsertArguments, which cuts part of the script as a result of its work, in some cases. Commented Feb 27, 2013 at 20:29

1 Answer 1

3

Fixed it by changing the PowerShell script from this:

if ($argument4 -eq "False") {

to this:

if ("$argument4" -eq "False") {

It works with quotation marks around it. I guess the "junk" I was seeing in the script string (like vblf) is normal- I don't work with reading from text files that much.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.