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.
Related
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"
I inherited a script which loops through a set of servers in a server list and then outputs some stuff for each one. It uses StringBuilder to append stuff to a variable and then spits out the results...how do I get the script to store the contents so I can display it at the VERY end with the results of the entire foreach instead of having it print (and then overwrite) on each iteration?
Currently my results look like this:
ServerName1
Text1
Next run:
ServerName2
Text 2
How do I get it to store the data and then output the following at the end so I can email it?
ServerName1
Text1
ServerName2
Text2
My code:
foreach($Machine in $Machines)
{
Invoke-Command -ComputerName $Machine -ScriptBlock{param($XML1,$XML2,$XML3,$URL)
[System.Text.StringBuilder]$SB = New-Object System.Text.StringBuilder
$X = $SB.AppendLine($env:COMPUTERNAME)
if (Test-Path <path>)
{
$PolResponse = <somestuff>
$PolResponse2 = <somestuff>
Write-Host "[1st] $PolResponse" -ForegroundColor Magenta
Write-Host "[2nd] $PolResponse2" -ForegroundColor Magenta
$X = $SB.AppendLine($PolResponse)
$X = $SB.AppendLine($PolResponse2)
}
else
{
$PolResponse = "[1st] No Response"
$PolResponse2 = "[2nd] No Response"
Write-Host $PolResponse -ForegroundColor Red
Write-Host $PolResponse2 -ForegroundColor Red
$X = $SB.AppendLine($PolResponse)
$X = $SB.AppendLine($PolResponse2)
}
} -ArgumentList $XML1, $XML2, $XML3, $URL
}
# Sending result email
<<I want to send the TOTALITY of $SB here>>
You can start by moving the StringBuilder variable declaration outside of the for loop (prior to it)
[System.Text.StringBuilder]$SB = New-Object System.Text.StringBuilder
then FOR LOOP
I don't know if this will be a good solution for what you're asking for or not, but what you could do is create a txt file and every loop in the foreach loop add the information to a txt file. This is one way to store all of the information and then have all of it together at the end.
New-Item -Path "\\Path\to\file.txt" -Itemtype File
Foreach(){
$Stuff = # Do your stuff here
Add-Content -Value $stuff -Path "\\Path\to\file.txt"
}
# Email .txt file ?
# You could use Send-MailMessage to do this possibly
Hopefully this can be helpful for your goal.
I'm writing a script that has a lot of output, and can take multiple computer names. The output announces the computer name, then a lot of info about that particular computer. I want to have a series of #s above and below where it announces the computer name before each section of info, but would like to see if I can have the amount of #s be the same as the length of the provided computer name(s). For example:
########
COMPNAME
########
or
##############
LONGERCOMPNAME
##############
I'd rather not have to have an if else for every possible case, such as
if ($compname.length -eq "8") {
Write-Host "########"
Write-Host "$compname"
Write-Host "########"
} elseif ($compname -eq "9") {
Write-Host "#########"
Write-Host "$compname"
Write-Host "#########"
and so on. If I have to, I will, it'd just be ten of those or so. Or I could just use some amount of #s that will always definitely cover at least the max length a computer name might be.
You're gonna love this feature of PowerShell. You can "multiply" a string.
Try this:
$sep = '#'
Write-Output ($sep*5)
$names = "Hello World", "me too", "goodbye"
$names | % {
Write-Output ($sep*($_.Length))
Write-Output $_
Write-Output ($sep*($_.Length))
}
OUTPUT
#####
###########
Hello World
###########
######
me too
######
#######
goodbye
#######
I'd recommend wrapping Kory Gill's suggestion in a custom function, so that you have an easy way of formatting any given name:
function Format-ComputerName([string]$ComputerName) {
$separator = '#' * $ComputerName.Length
'{0}{1}{2}{1}{0}' -f $separator, [Environment]::NewLine, $ComputerName
}
Or a fixed-width banner:
"{0}`r`n# {1,-76} #`r`n{0}" -f ('#' * 80), $compname;
e.g.:
################################################################################
# LONGERCOMPNAME #
################################################################################
You can also add dates, times, Etc.:
"{0}`r`n# {1:G} : {2,-54} #`r`n{0}" -f ('#' * 80), (Get-Date), $compname;
e.g.:
################################################################################
# 04/02/2017 16:42:07 : LONGERCOMPNAME #
################################################################################
More info on string formatting here
you can do this
$NbChar=5
#method 1 (best)
'#' * $NbChar
#method 2
New-Object System.String "#", $NbChar
#method 3
-join (1..$NbChar | %{"#"})
#method 4
"".PadLeft($NbChar, '#')
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.
I am having a text file wich uses fixed width for separating columns.
I'm loading the file and create a new column which concatinates the values of the first two columns.
The problem I have that when exporting the data I need to define a fixed column width of 13 for Column C.
Column A (3) Column B(9) Column C(13)
MMA 12345 12345_MMA
MMO 987222 987222_MMO
Basically for this example in the export I am missing 4 spaces for the first row and 3 for the second row.
Thisis my current code, which also includes a new row for MD5 creation.
# Load input data
$PreSystem = [IO.File]::ReadAllText("C:\FILE.txt")
# Initiate md5-hashing
$md5 = new-object -TypeName System.Security.Cryptography.MD5CryptoServiceProvider
$utf8 = new-object -TypeName System.Text.UTF8Encoding
# Split input data by lines
$all = $PreSystem.split("`n")
# Loop over lines
for($i = 0; $i -lt $all.length-1; $i += 1) {
# Access distinct lines
$entry = "$($all[$i])"
# Get the different parameters
$market_code = $entry.substring(1,3)
$soc = $entry.substring(4,9)
# Hash the SOC element
$hash = [System.BitConverter]::ToString($md5.ComputeHash($utf8.GetBytes($soc)))
# Create desired format for each entry
$output = $hash.Replace("-","")+$soc.Replace(" ","") + "_" + $market_code + $all[$i]
# Write to file
"$output" | Out-File -Filepath C:\"C:\FILE.txt" -Append -encoding ASCII
}
Thanks in advance
You can create a custom table format using the tip explained here. Here is an example for Get-Process:
$a = #{Expression={$_.Name};Label="Process Name";width=25}, `
#{Expression={$_.ID};Label="Process ID";width=15}, `
#{Expression={$_.MainWindowTitle};Label="Window Title";width=40}
Get-Process | Format-Table $a
Basically, you build an expression through wich Format-Table will pipe
each row. Instead of taking care of the formating yourself for each row, you build a hash and pipe it through Format-Table.
It's still not quite clear to me what output you actually want to achieve, but maybe this will give you some idea.
One of the most convenient ways to get formatted string output is using the format operator (-f). You specify a format string with placeholders in curly brackets, and fill it with the values of an array:
PS C:\> '_{0}:{1}:{2}_' -f 'foo', 'bar', 'baz'
_foo:bar:baz_
Column widths can be specified in the format string like this:
PS C:\> '_{0,-5}:{1,7}:{2,-9}_' -f 'foo', 'bar', 'baz'
_foo : bar:baz _
As you can see, negative values align the column to the left, positive values align it to the right.
If there's a chance that a value is too long for the give column width you need to truncate it, e.g. with the Substring() method:
PS C:\> $s = 'barbarbar'
PS C:\> $len = [math]::Min(7, $s.Length)
PS C:\> '_{0,-5}:{1,7}:{2,-9}_' -f 'foo', $s.Substring(0, $len), 'baz'
_foo :barbarb:baz _
You can quickly have a fixed size left-aligned content string using the following code:
Write-Host "$myvariable $(" " * 60)".Substring(0,60)
this will give you a fixed width of 60 characters with the contents aligned to the left
One of the solutions is for each of the rows use this mechanism when concatenating:
$a = "MMA"
$b = "12345"
$str = "$($b)_$($a)"
if (($str.Length) -ge 13 ) {
Write-Host "$($str)"
} else {
$padStr = " " * (13 - ($str.Length))
Write-Host "$($str)$($padStr)"
}
So instead of Write-Host CmdLet you can use the appropriate CmdLet for your purpose.
Edit, after adding actual code. So the above logic would translate into:
$market_code = $entry.subString(1,3)
$soc = $entry.subString(4,9)
$str = $soc.Replace(" ", "") + "_" + $market_code
if (($str.Length) -ge 13 ) {
$output = $hash.Replace("-","") + $str + $all[$i]
} else {
$padStr = " " * (13 - ($str.Length))
$output = $hash.Replace("-","") + $str + $padStr + $all[$i]
}
You can do fixed size using next code:
$data = "Some text"
$size = 20
$str = [string]::new(' ',$size).ToCharArray()
$data.CopyTo(0,$str,0,$data.Length)
$str = $str -join ''