Powershell : I want to restrict the "variables" a user puts into my textbox. from a list if possible? - variables

I have a textbox with 3 customfields where the user can put in whatever he wants, and its parsed as answer.
Now I want to restrict it to a wordlist like that for my first input :
Question 1 : How old are you ?
Answer (User input) : `1-99` (digits)
For my second question I need Answers combined of 2 Wordlists, like :
Question : Whats your mothers name ?
Assuming now that I have 2 List with possible names: All names of the world and all male names . So now I need to read out all names possible and subtract the other list.
(Allnames.txt - allmalenames.txt) = Answer2
So the user can only put in female names in the end, and no I don't have female list first ;)
With that information i can resolve question 3 alone I guess :)
My Code is basicially this input box from the powershell tips of week.
Custom input box
Any help is upvoted fast. If anything is unclear, let me know.

$answer2 = <user input string>
$allnames = <path to all names file>
$malenames = <path to male names file>
if (
( Select-String -Path $allnames -Pattern "^$answer2$" -Quiet ) -and -not
( Select-String -Path $malenames -Pattern "^$answer2$" -Quiet )
)
{ Do whatever needs done if it is a female name }
else { Do whatever needs done if it is not a female name }
returns $true, $answer2 is a valid female name. Otherwise, not.

Related

display the 1st variable in a tuple in PowerShell

I want to set the Netadapter "Roaming aggressiveness" value to "lowest" on all of our computers.
The wifi chips vary from one computer to another so that "Roaming aggressiveness" lowest value on on computer may display as "1.Lowest" vs another that displays as "1. Lowest" vs another with "1 - Lowest" and who knows what other variations.
Because of this, the below script does not work for ALL of our computers when trying to set them all to lowest roaming aggressiveness.
Set-NetadapterAdvancedProperty -Name "Wi-Fi" -DisplayName "Roaming aggressiveness" -DisplayValue "1.Lowest"
To get around this, I want to replace the "1.Lowest" instead to a variable that references the 1st DisplayValue without needing to know the actual name. I just know the 1st item is always the lowest option.
I am able to get the ValidDisplayValues into a variable called $RoamOptions:
$RoamOptions = Get-NetAdapterAdvancedProperty Wi-Fi -DisplayName *Roam* | ft ValidDisplayValues
The result of this on a sample computer is
ValidDisplayValues
------------------
{1. Lowest, 2. Medium-low, 3. Medium, 4. Medium-High...}
How do I create a variable $LowestOption that grabs the 1st item in the list so I can then reference it as follows so it would work for all of our computers?
Set-NetadapterAdvancedProperty -Name "Wi-Fi" -DisplayName "Roaming aggressiveness" -DisplayValue $LowestOption
Hope that makes sense.
Thank you
You need to get rid of the format-table and use this syntax.
PS> $x = (Get-NetAdapterAdvancedProperty "*Wi-Fi" -DisplayName *Roam*).ValidDisplayValues
PS> $x.count
5
PS> $x[0]
1. Lowest
PS>
Note: the * in front of Wi-Fi is because mine has been renamed. Might be good to use incase yours is likewise.

Whats the best approach to analyse a data source (csv) to then extract the deltas (changes) only to a destination source?

For my example, I'm looking to compare a source file with some changes made to the attributes in a table - lets say in the form of another source file.
What i want to achieve is
Sourcefile.csv
Newfile.csv
Deltafile.csv
(this will export only the changes deltas (rows) between the two files)
What i would like to achieve is that the row with the changes is exported as the delta, not just the column attribute.
All other rows that match do not need to be updated.
100,Renie,Stav,Renie.Stav#yopmail.com,Renie.Stav#gmail.com,CHANGE
101,Neila,Germann,CHANGE,Neila.Germann#gmail.com,developer
I've looked at Powershell, FC and SSIS incremental loading to see if this will work for my needs but a need some guidance in the right direction. Any help is greatly appreciated! :)
**Current Method **
Looking indepth at the powershell i tried in https://www.reddit.com/r/PowerShell/comments/cea8ax/compare_2_csv_files_and_export_the_rows_that_do/
Which is
# Compare work
$csv1 = Import-Csv -Path C:\Users\G23\Documents\Hackingfolder\source.csv
$csv2 = Import-Csv -Path C:\Users\G23\Documents\Hackingfolder\new.csv
$head = (Get-Content -Path C:\Users\G23\Documents\Hackingfolder\source.csv | Select-Object -First 1) -split ","
Compare-Object $csv1 $csv2 -Property $head -PassThru| Export-Csv C:\Users\G23\Documents\Hackingfolder\TheDiff.csv -NoTypeInformation
# Remove side indicator if you dont care to know where the diff came from
Compare-Object $csv1 $csv2 -Property $head | Select-Object -Property $head
Ignoring the side indicators i would get the rows that would not match, both of them. its not smart enough to know which one is the updated one. e.g I want the exported delta changes rows only.
PowerShell output
instead of the desired below changes only in csv
100,Renie,Stav,Renie.Stav#yopmail.com,Renie.Stav#gmail.com,CHANGE
101,Neila,Germann,CHANGE,Neila.Germann#gmail.com,developer
Thanks!

SQL replace row with matching value

I'm pulling data out from AD using CSVDE. I have a row named "Managers", which gives distinguished name when pulling data. I somehow replaced that value with employee's ID.
I executed and got
CN=Manager1,OU=Container,DC=DomainController
Need to replace whole thing with Manager1's employee ID. I can either do it in PowerShell or VB, if necessary.
Using Powershell and the ActiveDirectory module:
Import-CSV <csvfile> |
foreach {
$_.Manager = (Get-Aduser $_.Manager -Properties EmployeeID).EmployeeID
} |
Export-CSV <csvfile> -NoTypeInformation

powershell assigning output of a ps1 script to a variable

Let me start with I am very new to powershell and programming for that matter. I have a powershell script that takes some arguments and that outputs a value.
The result of the script is going to be something like 9/10 where 9 would be the number active out of the total amount of nodes. I want to assign the output to a variable so I can then call another script based on the value.
This is what I have tried, but it does not work:
$active = (./MyScript.ps1 lb uid **** site)
I have also tried the following which seems to assign the variable an empty string
$active = (./MyScript.ps1 lb uid **** site | out-string)
In both cases they run and give me the value immediately instead of assigning it to the variable. When I call the variable, I get no data.
I would embrace PowerShell's object-oriented nature and rather than output a string like "9/10", create an object with properties like NumActiveNodes and TotalNodes e.g. in your script output like so:
new-object psobject -Property #{NumActiveNodes = 9; TotalNodes = 10}
Of course, substitute in the dynamic values for num active and total nodes. Note that uncaptured objects will automatically appear on your script's output. Then, if this is your scripts only output, you can do this:
$obj = .\MyScript.ps1
$obj.NumActiveNodes
9
$obj.TotalNodes
10
It will make it nicer for those consuming the output of your script. In fact the output is somewhat self-documenting e.g.:
C:\PS> .\MyScript.ps1
NumActiveNodes TotalNodes
-------------- ----------
9 10
P.S. When did StackOverflow start sucking so badly at formatting PowerShell script?
If you don't want to change the script ( and assuming only that $avail_count/$total_count line is written by the script), you can do:
$var= powershell .\MyScript.ps1
Or just drop the write-host and have just $avail_count/$total_count
and then do:
$var = .\MyScript.ps1
you could just do a $global:foobar in your script and it will persist after the script is closed
I know, the question is a bit older, but it might help someone to find the right answer.
I had the similar problem with executing PS script with another PS script and saving the output into variable, here are 2 VERY good answers:
Mathias
mklement0
Hope it helps!
Please up-vote them if so, because they are really good!

How to search with multiple keys in a hash table in powershell

I'm writing a Powershell script and in it I'm using a hash table to store information about database checks. The table has 5 keys (host, check, last execution time, last rep, status) and I want to search in my table for values where:
$s = $table where $host -eq $hostname -and check -eq $check
Does anyone have any idea how this is done? And if it makes any difference, the script cannot rely on .NET framework higher than 2.0
I´m new to Powershell and scripting in general so this might be very obvious but I still can't seem to find an answer on Google. Also if someone knows a good reference page for Powershell scripting I would really appreciate a link.
Gísli
EDIT: Don't see how it matters but here is a function I use to create a hash table:
function read_saved_state{
$state = #{}
$logpos = #{}
$last_log_rotate = 0
foreach($s in Get-Content $saved_state_file){
$x = $s.split('|')
if($x[0] -eq 'check'){
$state.host = $x[1]
$state.check = $x[2]
$state.lastexec = $x[3]
$state.lastrep = $x[4]
$state.status = $x[5]
}
elseif($x[0] -eq 'lastrotate'){
$last_log_rotate = $x[1]
}
elseif($x[0] -eq 'log'){
$logpos.lastpos = $x[3]
}
}
$saved_state_file has one line for each check run and can also have a line for last log rotate and last log position. There can be as many as 12 checks for one host.
I'm trying to extract a particular check, run at a particular host, and changing the lastexec_time, last_rep and status.
return $state,$logpos,$last_log_rotate
}
Assuming you have an array or list of hashtables (not entirely clear from the question), your syntax is pretty close:
$s = $tables | where {($_.host -eq $hostname) -and ($_.check -eq $check)}