give value into powershell script

Viewed 43

I want to use a string inside a PowerShell script. It should be handed over like a variable when executing the script like this:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -c C:\temp\myscript.ps1 "string"

That is working well with just one word. But my string looks like this and should be handed over:

**<UserInputs><UserInput Question="Gruppenname" Answer="&lt;Values Count=&quot;1&quot;&gt;&lt;Value DisplayName=&quot;Humanresources&quot; Id=&quot;af05c5d3-2312-c897-8439-08979d4d0a49&quot; /&gt;&lt;/Values&gt;" Type="System.SupportingItem.PortalControl.InstancePicker" /><UserInput Question="Ausgabe" Answer="Namen" Type="richtext" /></UserInputs>**

This string contains some quotation marks and I have problems injecting it into my script.

inside the script I have this:

$mystring = $Null if($args[0] -ne $Null) { $mystring = $args[0] } $result = $mystring | Select-String -Pattern "DisplayName=&quot;(.*?)&quot;" $result= $result.Matches.Groups[1] $group = $result.value Write-Output "$group" | Out-file C:\temp\group.txt
1 Answers

PowerShell scripts can take parameters, which become variables that are just available to the rest of the script. Use this at the top of your script to setup three variables, one boolean (true false) type, one string type and one integer type.

#stack.ps1
param(
         # Param1 help description
         [Parameter(Mandatory=$true,
                    ValueFromPipelineByPropertyName=$true,
                    Position=0)]
         [string]$MyString="MyDefaultValue",
 
         [int]$MyIntInput,

         [bool]$MyTrueFalseInput
         
)

"Script running!"
"Values recieved!"

"MyString = $MyString"
"MyIntInput = $MyIntInput"
"MyTrueFalse = $MyTrueFalseInput"

Then to pass in values

C:\temp> .\stack.ps1 -MyString Ham -MyIntInput 75 -MyTrueFalseInput $true
Script running!
Values recieved!
MyString = Ham
MyIntInput = 75
MyTrueFalse = True

How to work with odd or complex strings

Now, to pass in a complex string that has quotes, use this syntax:

#using single quotes as a delimiter, ignoring the doubles inside.
$myWeirdString = 'ThisIs"SomeString"WhichHasQuotes"WhichIsWeird' 

## extra case, if it's really odd and has both quote types
$myWeirdString = @"
'ThisIs"SomeString"WhichHasQuotes"WhichIsWeird' 
"@
Related