Skip to content

Instantly share code, notes, and snippets.

@brianbunke
Last active November 12, 2015 00:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brianbunke/23c6d6981842f8970b03 to your computer and use it in GitHub Desktop.
Save brianbunke/23c6d6981842f8970b03 to your computer and use it in GitHub Desktop.
2015-11 PowerShell.org Scripting Games Puzzle
# Ideally this is [string[]]
param([string]$VMNameStr)
# Copying from old: Split up names by commas, trim the ends, output to screen
$VMNameStr -split "," | %{$_.trim()}
# Not [string[]], so will only accept one value
param([string]$VMNameStr)
# Don't define an empty array up top if you're not appending into it below
$VMs=@()
$VMNames=@()
# If there's a comma, we need to split into multiple names
if($VMNameStr.indexof(",") -gt 0) {
# $Trace only intended as debugging, can be ignored
$Trace="Found Comma..."
# Split up the names on commas, then trim up the ends
# This is the useful line, the other stuff makes it worse
$VMs=$VMNameStr -split "," | %{$_.trim()}
# Still ignoring here because it's not called below
$trace+="Length = $($VMs.length)"
$trace+=$VMs -is [array]
# This is the long way of coding ForEach
for($i=0;$i -lt $VMs.length;$i++){
# Continuation of ForEach logic
if($VMs[$i] -gt ""){
# Create $vmname0, value is the string name of current VM
# More variables that will not be called later
set-variable -Name ("vmname" + $i) -value $VMs[$i]
# Append the current VM name into $VMNames
$VMNames+=$VMs[$i]
}
}
}
# VMNameStr --> $VMName0 --> $VMNames
else{$VMName0=$VMNameStr;$VMNames=$VMName0}
# Show your triumphant split of names by comma
# Format varies depending on your input into the parameter
$VMNames
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment