Reputation: 69
I'm converting VBScript to PowerShell and I have an issue with Select Case
. I'm trying to find a way to do a Case Else
.
switch($IncomingText.SubString(0,1)) {
"1" {
switch($IncomingText.SubString(12,3)) {
"601" {
$TempString = ($IncomingText.SubString(0,75))
if (RemoveSpaces($IncomingText.SubString(75,10)) = True) {
$TempString = $TempString + (StrReverse($IncomingText.SubString(75,10))) + ($IncomingText.SubString(85,10))
} else {
$TempString = $TempString + ($IncomingText.SubString(75,20))
}
return $TempString
Case Else
if (RemoveSpaces($IncomingText.SubString(155,10)) = True) {
$TempString = $TempString + (StrReverse($IncomingText.SubString(155,10))) + ($IncomingText.SubString(165))
} else {
$TempString = $TempString + ($IncomingText.SubString(155))
}
return $TempString
}
}
}
}
In VBScript there's this Case Else
. I want to stay in the "601" and proceed with the next section. How do I do it in PowerShell? Since Case Else
doesn't seems to be working for me.
Upvotes: 0
Views: 4742
Reputation: 36332
I'd like to point out that the Switch
command will evaluate scriptblocks similar to an If
statement, so you could just create more complex cases:
switch($IncomingText) {
{$_.SubString(0,1) -eq '1' -and $IncomingText.SubString(12,3) -eq '601'}
{
$TempString = $IncomingText.SubString(0,75)
if (![string]::IsNullOrWhiteSpace($IncomingText.SubString(75,10))) {
$TempString = $TempString + (StrReverse($IncomingText.SubString(75,10))) + ($IncomingText.SubString(85,10))
} else {
$TempString = $TempString + ($IncomingText.SubString(75,20))
}
}
default
{
if (RemoveSpaces($IncomingText.SubString(155,10)) = True) {
$TempString = $TempString + (StrReverse($IncomingText.SubString(155,10))) + ($IncomingText.SubString(165))
} else {
$TempString = $TempString + ($IncomingText.SubString(155))
}
return $TempString
}
}
I would also like to note that it looks like you are removing spaces from a 10 character string, and testing to see if anything is left. If there is you reverse those 10 characters, then add the next 10 characters, if not you simply add all 20 characters as they are. My point is that if the first 10 characters are all spaces then reversing them is harmless, so you may as well just always reverse them. With that in mind your code gets a lot simpler:
Function StrReverse ($MyString){
$arrMyString = $MyString.ToCharArray()
[array]::Reverse($arrMyString)
$arrMyString -join ''
}
switch($IncomingText) {
{$_.SubString(0,1) -eq '1' -and $IncomingText.SubString(12,3) -eq '601'}
{
$TempString = $IncomingText.SubString(0,75) + (StrReverse $IncomingText.SubString(75,10)) + $IncomingText.SubString(85,10)
}
default
{
$TempString + (StrReverse $IncomingText.SubString(155,10)) + $IncomingText.SubString(165)
}
}
Upvotes: 2
Reputation: 23395
You're looking for default
:
Switch ($something) {
1 { .. }
2 { .. }
default { .. }
}
Upvotes: 4
Reputation: 7465
Taken from Windows Powershell site https://technet.microsoft.com/en-us/library/ff730937.aspx
So use default instead of case else.
$a = 5
switch ($a)
{
1 {"The color is red."}
2 {"The color is blue."}
3 {"The color is green."}
4 {"The color is yellow."}
5 {"The color is orange."}
6 {"The color is purple."}
7 {"The color is pink."}
8 {"The color is brown."}
default {"The color could not be determined."}
}
Upvotes: 4