Modify Multi Hashtables - powershell

I want to create a script to create VDIs. Basically it's ready, but now I want to modify it to work with gridviews to make it more usable.
My goal is, that you may get a gridview like this code:
$id = 1
$table = #( #{'ID'=$id ++; 'VM Name'="VM0001"; 'Assigned User'=$null},
#{'ID'=$id ++; 'VM Name'="VM0002"; 'Assigned User'=$null},
#{'ID'=$id ++; 'VM Name'="VM0003"; 'Assigned User'=$null} )
$out = $null
$out = $table.ForEach({[PSCustomObject]$_}) |
Select ID, 'VM Name', 'Assigned User' |
Out-GridView -Title "VM Creator" -OutputMode Single -OutVariable selectedID
if ($selectedID -eq $null -or $out.Count -eq 0) {
Write-Host -BackgroundColor Black -ForegroundColor Red "Error: nothing is choosed"
}
if ($selectedID.'Assigned User' -eq $null) {
Write-Host -BackgroundColor Black -ForegroundColor Green "Debug: Input UserName for Desktop" $selectedID.'VM Name'
$newUser = Read-Host "Input UserName"
}
Now when you select an entry without a assigned User you should be able to add a name into the hashtable.
When I tried this:
$table.Add("123", "VM0004", "Pete")
I got the following error (in German):
Für "Add" und die folgende Argumenteanzahl kann keine Überladung gefunden
werden: "3".
In Zeile:8 Zeichen:1
+ $table.Add("123", "VM0004", "Pete")
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodException
+ FullyQualifiedErrorId : MethodCountCouldNotFindBest

Your $table is an array of hashtables, so you need to append another hashtable:
$table += #{'ID'="123"; 'VM Name'="VM0004"; 'Assigned User'="Pete"}
The Add() method can't be used here, because you can't append to a fixed size array (+= works because it doesn't actually append to the array but creates a new array with increased size, copies the elements from the existing array, puts the new value(s) in the free slots, then assigns the new array to the variable).
To be able to modify values of selected rows I'd suggest to use an array of custom objects right away (you're creating them for the gridview anyway).
$table = [PSCustomObject]#{'ID'=$id++; 'VM Name'="VM0001"; 'Assigned User'=$null},
[PSCustomObject]#{'ID'=$id++; 'VM Name'="VM0002"; 'Assigned User'=$null},
[PSCustomObject]#{'ID'=$id++; 'VM Name'="VM0003"; 'Assigned User'=$null}
With that you can update values of a selected row like this:
$selected = $table | Out-GridView -PassThru
$selected | ForEach-Object {
$_.'Assigned User' = 'Chris'
}

Related

Match Unique Value in Column and put all values on that row in to variables

I'm working with a CSV with 5 Columns, One of the Columns has unique Values.
Fruit, Number, Car, item, color
apple, 2, Chevy, ball, blue
apple, 1, Ford, ball, green
orange, 3, Ford, string, "red,green"
orange, 5, Mazda, key, red
Banana, 4, Tesla, desk, yellow
I need to search for 3 and have it return orange ford string "red,green" as their own variable
i.e. $fruit1 becomes orange $car1 becomes ford $item becomes string and $color bcomes red,green
I can do the search and have it tell me it found 3, but it still just puts runs everything through $fruit1 and if I tell it to write $fruit1 to a file it just get a repeating mess
I Need to Get output to a TXT file like so
for #3
FRUIT=orange
Car=Ford
ITEM = string
COLOR ="red,green"
whith each value in a different part of the file/newline
I can't post from the machine the script is on. So values changed to match my example
Function LogWrite
{
Param ([string]$logstring)
Add-content $Logfile -value $logstring
}
LogWrite "Started execution of script.ps1"
$masterlist = Import-Csv ($filepath + "\" + "masterlistfile.csv" )
$FruitName = #()
$NumberName = #()
$Carname = #()
$ItemName = #()
$Colorname = #()
$masterlist |ForEach-Object {
$FruitName += $_.fruit
$NumberName += $_.number
$Carname += $_.car
$Itemname += $_.item
$Colorname += $_.color
}
$number = 3
$FruitIdentified
$CarIdentified
$ItemIdentified
$ColorIdentified
LogWrite "NUmber $number to be searched in masterlistfile "
if ($NumberName -eq $number)
{
LogWrite "Number found in the list..."
$Where = [array]::IndexOf($NumberName, $number)
LogWrite "Fruit Name : $FruitrName[$Where] "
$FruitIdentified = $FruitName[$Where]
$CarIdentified = $CarName[$Where]
$ItemIdentified = $ItemName[$Where]
}
You can use the following to read your CSV and then export the result with your expected output:
$number = 3
Import-Csv path/to/csv.csv | ForEach-Object {
if($_.number -eq $number) {
"for #$number"
foreach($prop in $_.PSObject.Properties.Name -ne 'Number') {
'{0}={2}{1}{2}' -f $prop, $_.$prop, ($null, '"')[$prop -eq 'color']
}
}
} | Set-Content path/to/file.ext
Note that Set-Content will overwrite the export file, if you want to append you would use Add-Content as in your function.
To give some context on what the code does:
Read the CSV and convert it to an object with Import-Csv
Loop over all objects and filter where the value of the Number property is equal to $number.
Output for #$number, in this example would be for #3".
Get all properties of the object using PSObject.Properties.Name and exclude the Number property using -ne 'Number'.
Loop over the Property Names and output '{0}={1}' -f $prop, $_.$prop, here we use the Format Operator -f, {0} would be the Property Name and {1} would be the Property Value. {2} will wrap the value with ".." if the Property Name is color.
The output you would be getting using your CSV for input would be:
for #3
Fruit=orange
Car=Ford
item=string
color="red,green"

Powershell: Arrays index acess and check if previous is substring to next

I need to link the job to the subjob: the job is of this format for example ACGN100Q while the subjobs that are attached are sequential and of this format: ACGN-100Q-000T;ACGN-100Q-010T;ACGN-100Q-020T;ACGN-100Q-030T
In my csv file the type of this job ACGN100Q is "TechnologyInteraction" while the subjobs are of type "TechnologyService". I am developing a script that allows me to say for example that the link between ACGN-100Q-000T and ACGN-100Q-010T is of type "TrigerringRelation" and the link between ACGN100Q and ACGN-100Q-000T is of type "RealizationRelation". I need help because I can't make the link.
Here is my starting csv file :
newElements.csv
ID,"Type","Name","Documentation"
eb214110-2b6a-48b2-ba5a-7c13dc3bba39,"TechnologyInteraction","ACGN100Q","Begin Of JobStream"
a46681e7-19a8-4fc5-b747-09679c15ff26,"TechnologyService","ACGN-100Q-000T","Transfert UDM (xACGN000)"
85761a09-1145-4037-a527-66a743def45f,"TechnologyService","ACGN-100Q-010T","move fichier REF to work"
27b126fb-c708-427d-b0a6-ce4747114ac4,"TechnologyService","ACGN-100Q-020T","w_read_account"
bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82,"TechnologyService","ACGN-100Q-030T","w_read_referential"
0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97,"TechnologyService","ACGN-100Q-040T","w_load_CompanyGroup"
1f487986-3cac-4af8-bda2-6400a1c71f48,"TechnologyService","ACGN-100Q-050T","w_load_Company"
And I want to get a file that looks like this:
relation.csv
"ID","Type","Name","Documentation","Source","Target"
"New ID","RealizationRelationship","","","eb214110-2b6a-48b2-ba5a-7c13dc3bba39","a46681e7-19a8-4fc5-b747-09679c15ff26"
"New ID","TriggeringRelationship","","","a46681e7-19a8-4fc5-b747-09679c15ff26","85761a09-1145-4037-a527-66a743def45f"
"New ID","TriggeringRelationship","","","85761a09-1145-4037-a527-66a743def45f","27b126fb-c708-427d-b0a6-ce4747114ac4"
"New ID","TriggeringRelationship","","","27b126fb-c708-427d-b0a6-ce4747114ac4","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82"
"New ID","TriggeringRelationship","","","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97"
"New ID","TriggeringRelationship","","","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97","1f487986-3cac-4af8-bda2-6400a1c71f48"
$result= #()
function linkedRelationCsvToElementsCsv{
#relations.csv headers are ID,"Type","Name","Documentation","Source","Target"
$Type=#()
$Name=#()
$ID=#()
$Documentation=#()
#$pattern="^(WEBX|DWHS|COGN|CLOT|CLAI|BTRE|BISI|BDDO|ARXL|AGSO|AGPC|ACTO|FNET|ARX|AGS|INF|CLA|MEM|SWA|REX)-"
$newElementsCsv=Import-CSV $env:USERPROFILE\Desktop\Archi\newElements.csv |sort ID,"Type","Name","Documentation" -Unique
# Check if type is TechnologyInteraction or TechnologyService and link :TechnologyService to TechnologyInteraction and TechnologyInteraction to TWS id's
ForEach ($line in $newElementsCsv){
$Type += $line.Type
$Name += $line.Name
$ID +=$line.ID
$Documentation += $_.Documentation
#Search for element type in elements.csv
for( $index=0; $index -le $Name.length-1; $index++){
if($Type[$index] -eq 'TechnologyInteraction' -or $Type[$index] -eq 'TechnologyEvent' ){
Write-Warning "Case type TechnologyInteraction founded, new type of RealizationRelationship created "
# if the job is of type "TechnologyInteraction" or "TechnologyEvent", we link it to the TWS id's(TechnologyCollaboration,ef2f510b-924b-439d-8720-0183c7294eb3) in archi.
$newArrayResult= New-Object PsObject -Property #{ID=[guid]::NewGuid().ToString(); "Type"="RealizationRelationship"; "Name"=$Name[$index]; "Documentation"=$Documentation[$index]; "Source"="ef2f510b-924b-439d-8720-0183c7294eb3"; "Target"=$ID[$index]}
$result = $result + $newArrayResult
}elseif ($Type[$index][0].Type -eq 'TechnologyService' -and$Type[$index][1].Type -eq 'TechnologyService' ){
Write-Warning "Case type TechnologyService founded, new type of TriggeringRelationship created "
$newArrayResult2 = New-Object PsObject -Property #{ID=[guid]::NewGuid().ToString(); "Type"="TriggeringRelationship"; "Name"=""; "Documentation"=""; "Source"=$line[$index][0].ID; "Target"=$line[$index][1].ID}
$result = $result + $newArrayResult2
}
}
}
$result |Select-Object -Property ID,"Type","Name","Documentation","Source","Target"| Export-Csv $env:USERPROFILE\Desktop\Archi\relation.csv -NoTypeInformation -Encoding UTF8
}linkedRelationCsvToElementsCsv # Call the function
> the elseIf() not return value.
Thanks you in advance.
The following code generates exactly the output you want for exactly the input you've given. There might be unexpected edge cases, so you should write some tests (e.g. with Pester) to confirm it behaves how you want it to in those edge cases.
The key is that the output for any row includes the ID of the previous row as well as the current row, so we keep the previous row in a variable during the foreach loop so we can inspect it when we process the next row, and the Type in the output just depends on the Type of the previous row.
Note that I've also moved the conversion to / from csv out of the main function so it's easier to unit test the function in isolation.
function ConvertTo-ElementItems
{
param
(
[object[]] $Relations
)
$jobTypes = #( "TechnologyInteraction", "TechnologyEvent" );
$subTypes = #( "TechnologyService" );
$previousItem = $null;
foreach( $item in $Relations )
{
if( $item.Type -in $jobTypes )
{
# start of a new job, but don't output anything
}
elseif( $item.Type -notin $subTypes )
{
# not a subjob type that we recognise
throw "unrecognised subjob type '$($item.Type)' for subjob '$($item.ID)'";
}
elseif( $null -eq $previousItem )
{
# we've got a subjob, but there was no previous job or subjob
throw "no preceding item for subjob '$($item.ID)'";
}
elseif( $previousItem.Type -in $jobTypes )
{
# this is the first subjob after the parent job
new-object PSCustomObject -Property ([ordered] #{
"ID" = "New ID"
"Type" = "RealizationRelationship"
"Name" = "";
"Documentation" = ""
"Source" = $previousItem.ID
"Target" = $item.ID
});
}
else
{
# the preceding item was a subjob as well
new-object PSCustomObject -Property ([ordered] #{
"ID" = "New ID"
"Type" = "TriggeringRelationship"
"Name" = ""
"Documentation" = ""
"Source" = $previousItem.ID
"Target" = $item.ID
});
}
$previousItem = $item;
}
}
And here's an example of using the function:
$ErrorActionPreference = "Stop";
Set-StrictMode -Version "Latest";
$inputCsv = #"
ID,"Type","Name","Documentation"
eb214110-2b6a-48b2-ba5a-7c13dc3bba39,"TechnologyInteraction","ACGN100Q","Begin Of JobStream"
a46681e7-19a8-4fc5-b747-09679c15ff26,"TechnologyService","ACGN-100Q-000T","Transfert UDM (xACGN000)"
85761a09-1145-4037-a527-66a743def45f,"TechnologyService","ACGN-100Q-010T","move fichier REF to work"
27b126fb-c708-427d-b0a6-ce4747114ac4,"TechnologyService","ACGN-100Q-020T","w_read_account"
bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82,"TechnologyService","ACGN-100Q-030T","w_read_referential"
0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97,"TechnologyService","ACGN-100Q-040T","w_load_CompanyGroup"
1f487986-3cac-4af8-bda2-6400a1c71f48,"TechnologyService","ACGN-100Q-050T","w_load_Company"
"#
$expectedCsv = #"
"ID","Type","Name","Documentation","Source","Target"
"New ID","RealizationRelationship","","","eb214110-2b6a-48b2-ba5a-7c13dc3bba39","a46681e7-19a8-4fc5-b747-09679c15ff26"
"New ID","TriggeringRelationship","","","a46681e7-19a8-4fc5-b747-09679c15ff26","85761a09-1145-4037-a527-66a743def45f"
"New ID","TriggeringRelationship","","","85761a09-1145-4037-a527-66a743def45f","27b126fb-c708-427d-b0a6-ce4747114ac4"
"New ID","TriggeringRelationship","","","27b126fb-c708-427d-b0a6-ce4747114ac4","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82"
"New ID","TriggeringRelationship","","","bb0c5e42-5fad-4bd9-8ee9-f41d0b824e82","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97"
"New ID","TriggeringRelationship","","","0b8b76e3-62fa-4527-9f05-2eb4dbaa8e97","1f487986-3cac-4af8-bda2-6400a1c71f48"
"#;
$relations = $inputCsv | ConvertFrom-Csv;
$elements = ConvertTo-ElementItems -Relations $relations;
$outputCsv = ($elements | ConvertTo-Csv -NoTypeInformation) -join "`n";
if( $outputCsv -ne $expectedCsv )
{
throw "output csv doesn't match expected csv";
} else {
write-host "output csv matches expected csv";
}

Powershell try/catch/finally isn't executing right (or I've completely hosed it)

I have a script that checks for cyclic groups.
The script takes all groups in a domain (parent groups), checks the membership of those groups and adds any member with an objectClass of 'group' to an array (child groups).
The script then checks the child groups to see if the parent is a member of the child (yeah, it's allowed but still not a good idea).
I added a try/catch/finally block so I could get the actual group names instead of the truncated error message that PowerShell returns.
The problem is, the script stops at the first error it encounters instead of continuing on.
This is the first try/catch I've done, so please bear with me.
Here's the script:
$original_ErrorActionPreference = 'Continue'
$ErrorActionPreference = 'Stop'
Import-Module -Name ActiveDirectory
$domains = #('corp.com', 'dom1.corp.com', 'dom2.corp.com')
foreach($domain in $domains){
Write-Host $domain -ForegroundColor Yellow
$parents = Get-ADGroup -server $domain -Properties name,objectclass -Filter * #get all domain groups
write-host $parents.count
$table = #()
$pGroupCount = #($parents).Count
$record = #{
'Parent' = ''
'Child' = ''
'Nester' = ''
}
foreach($parent in $parents){
Write-Host $parent.name -ForegroundColor Green
The script works up to this point.
This is the part that fails-
try { #get members in the parent that are groups
$children = Get-ADGroupMember -Identity $parent | Where-Object{$_.ObjectClass -eq 'group'} | Select-Object name,distinguishedName,objectClass
} catch [Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember]{
Write-Host $parent.name ' must be checked manually' -ForegroundColor blue -BackgroundColor Yellow
$parent.distinguishedName | Out-String -Width 4096 | Out-File -FilePath "$env:USERPROFILE\desktop\$domain-manualCheck.txt" -Width 5120 -Append
} finally {
$pGroupCount = $pGroupCount - 1
write-host $children.count ' - ' $children.name -ForegroundColor Gray
Write-Host $pGroupCount ' groups to go' -foregroundColor yellow
foreach($child in $children){ #get members in the children that are groups AND that have the same name as the parent
$nested = Get-ADGroupMember $child.name | Where-Object {$_.objectClass -eq 'group' -and $_.name -eq $parent.name}
$nestedCount = #($nested).count
if ($nestedCount -gt 0){
foreach($nester in $nested){
Write-Host $parent.name -ForegroundColor White
Write-Host $nestedCount -ForegroundColor Magenta
Write-Host $nester.name -ForegroundColor Cyan
$record.'Parent' = $parent.name
$record.'Child' = $child.name
$record.'Nester' = $nester.name
$objRecord = New-Object psobject -Property $record
$table += $objRecord
}
}
}
$table | Export-Csv -Path "$env:USERPROFILE\desktop\$domain-Group-Report.csv" -NoTypeInformation
$error | out-string -width 4096 | Out-File -FilePath "$env:USERPROFILE\desktop\$domain-Errors.txt" -Width 5120 -Append
}
}
}
$ErrorActionPreference = $original_ErrorActionPreference
As soon as the script hits the first group that has an issue, this is the error that's returned (#comments are added):
PS C:\Users\admin_j\Desktop> .\gtest.ps1
corp.com #current domain
283 #total group count
Exchange Servers #current group
6 - Exchange Install Domain Servers Exchange Install Domain Servers Exchange Install Domain Servers Exchange Install Domain Servers Exchange Install Domain Servers #6 groups within the parent, groups are from sub-domains
Exchange Install Domain Servers
282 groups to go
Get-ADGroupMember : Cannot find an object with identity: 'Exchange Install Domain Servers' under: 'DC=corp,DC=com'.
At C:\Users\admin_j\Desktop\gtest.ps1:46 char:15
+ $nested = Get-ADGroupMember $child.name | Where-Object $_.objectClass -eq ' ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ObjectNotFound: (Exchange Install Domain Servers:ADGroup) [Get-ADGroupMember], ADIdentityNotFoundException
+ FullyQualifiedErrorId : Cannot find an object with identity: 'Exchange Install Domain Servers' under: 'DC=corp,DC=com'.,Microsoft.ActiveDirectory.Management.Commands.GetADGroupMember
Why, instead of writing the bad group (in this case 'Exchange Install Domain Servers' under: 'DC=corp,DC=com'.) to a file, did the script stop? The group does indeed exist.
Should I add another block to catch any 'object not found' errors and send those to a file?
Thank you!
As Will's comment implies, you have indeed hosed your catch clause by specifying a type literal not matching an exception you'd ever expect thrown.
The general syntax for a catch clause is as follows
catch [catch-type-list] <statement block>
Where [catch-type-list] is an optional list of exception types that the associated statement block will act as an exception handler for.
That means, that this catch clause:
catch [Microsoft.ActiveDirectory.Management.Commands.GetADGroupMem‌​ber] {
# ...
}
Will only ever handle errors caused by an exception of the type [Microsoft.ActiveDirectory.Management.Commands.GetADGroupMem‌​ber] - this is of course not an exception type, and so the associated statement block will never execute.
In order for your catch clause to make sense in this context, specify a relevant exception type:
try{
Get-ADGroupMember -Identity $parent
}
catch [Microsoft.ActiveDirectory.Management.ADServerDownException]{
# DC is unreachable, abort
}
catch [Microsoft.ActiveDirectory.Management.ADIdentityResolutionException]{
# Group identity not resolved, add to list and continue
}
catch {
# Something else, completely unforeseen, happened, you might want to re-throw and return from your function
}
The last catch clause, in which the type list has been omitted is known as a general catch clause, and will handle any exception that didn't match any of the preceding catch clauses.

Power shell using the Trim function

HI I have the following line to check variables from two different systems.
foreach ($val in $stafflist)
{
$found = 0
foreach ($user in $aduser)
if ((($val.Surname.trim() -eq $user.surname.trim()) -And ($val.'First Name'.trim() -eq $user.Givenname.trim())) -or (($val.Surname.trim() -eq $user.surname.trim()) -And ($val.'Known As'.trim() -eq $user.Givenname.trim())))
{
$found = 1
try
{
if ($user.EmployeeID -ne $null)
{
$val.'First Name' + " " + $val.'Known As' + " " + $val.surname + " EmployeedID already set as " + $user.EmployeeID | Out-File $outfile -Append
}
else
{
set-aduser -Identity $user -EmployeeID $val.'Person Number'
$val.'First Name' + " " + $val.'Known As' + " " + $val.surname + " Employeed set to " + $user.EmployeeID| Out-File $outfile -Append
}
} ## end of Try
Catch
{
$val.'First Name' +" " + $val.surname + "Can not be updated in AD" | Out-File $outfile -Append
}
}
So this checks each user in two lists ($stafflist and $aduser) against each other searching for matches, and when it finds a match it updates the employee ID in Active directory with the ID from the HR database.
The trouble is it has an or operator in it to account for the fact that in the HR database system either the "first name" or "known as name" or indeed both might be filled in and need to be checked.
I want to use the .Trim function in case some one has left in white spaces, but it throws and error if i place it against a variable and that variable end up as null, which is often the case with the "known as name" variable.
Can any one suggest the most efficient way to do this check and trim for each variable.
I could trim all the variables upfront after checking they are not null.
or test if they are null and if so pass them through different test strings to avoid errors but that all means more lines of code and slower execution of code.
If any one has a concise way to achieve this I would be grateful for the suggestion.
In the end I am just adding this to the start of the loop
if ($val.Surname -eq $null){} else {$val.Surname = $val.Surname.trim()}
if ($val.'First Name' -eq $null){} else {$val.'First Name' = $val.'First Name'.trim()}
if ($val.'Known As' -eq $null){} else {$val.'Known As' = $val.'Known As'.trim()}
So it early on checks for null values if not null trims it and stores it back to the variable.

how to create a display with arrays

I want to create table where it offers the user some options like change the ip to static and dynamic and i have a hash table like this. I just wanted to know the other ways to create this table without having to spam the $box variable
$Box=#("г","="," ","¬","¦","-","L","¦","¦")
Write-Host $Box[0]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[3]
Write-Host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Title " "$box[2] $box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $box[8]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[7]
write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2] $MenuItems[0] $box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$MenuItems[1]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $Box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$Box[2]$box[2]$box[2]$box[4]
Write-host $box[4]$box[2]$box[2]$box[2]$box[2]$box[2]$MenuItems[2]$Box[2]$box[2]" "$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[2]$box[4]
Write-host $box[6]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[1]$box[5]
$UserInput = Read-Host "Please make a selection [1-3]"
switch($UserInput)
{
1 {Set-DHCP}
2 {Set-StaticIP}
3 {exit}
default {Main}
}
You can consolidate that code a lot by using .NET string formatting. For example, the first two lines would look like the following:
$Lines = #(); # Create an empty array
$Lines += ('{0}' + '{1}'*33 + '{2}') -f $Box[0], $box[1], $box[3]; # Format and add the first line
$Lines += ('{0}' + '{1}'*9 + '{2} ' + '{1}'*10 + '{0}') -f $Box[4], $box[2], $Title; # Format and add the second line
...
...
...
Write-Host -Object ($Lines -join "`n"); # Write out all lines, joined by a line separator.