display the 1st variable in a tuple in PowerShell - arraylist

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.

Related

Command in expect for grep keyword

Expect script query:
In one of my expect script I have to pick keyword from output of send command and store in a file, could some one help me.
send "me\n"
output :
EM/X Nmis Ssh Session/2; Userid =
Impact = ; Scope = ; CustomerId = 0
Here I want to pick keyword : Nmis Ssh Session/2
and my target is to create new command in expect script is :
send "set Nmis Ssh Session/2 \n"
so this value : Nmis Ssh Session/2 should store in a variable. Could some one help me.
I'm not quite sure exactly what information is produced by which side, but maybe something like this will do:
expect -re {EM/X ([^;]+);}
set theVariable $expect_out(1,string)
The key is that we use the -re option to pass a regular expression to the expect command. That makes the text that matches what is in the parentheses (a sequence of non-semicolon characters) be stored in the variable expect_out(1,string) (there are many other things stored in the expect_out array; see the documentation). Copying it from there to a named variable for the purpose of storage and further manipulation is trivial.
I do not know if the RE is the right one; there's something of an art in choosing the right one, and it takes quite a lot of knowledge about what the possible output of the other side could be.

How to make a variable minus from a number. Batch File

Basically I want to use 2020 minus a variable to make another variable.
This is the code I have at the moment.
echo Type Your Year, then press enter.
set/p "myear=>"
echo Type the year of the person's stuff you want to copy, then press enter.
set/p "yearofv=>"
cls
set/p "myyear="2020-%myear%"
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
can anybody run make any suggestions as how to help.
Thanks
Sam Inc.
A quick search yielded the following relevant answer. Use "set /a".
Calculating the sum of two variables in a batch script
It would be easier to identify your question if your provided code example was legible.
You can make it legible by highlighting it and clicking on the brackets.
I'm sure I'll be able to provide what you need if I could see what you're working with.
Thanks for taking the time to make your example more legible.
I believe your confusion is located where I have placed a "rem". That may be where you want to perform your calculation (or some other variation similar).
I placed an example for performing the calculation, then placed a goto statement so that you can look at the results.
This might help you to get a grip of resolving your issue:
#echo off
echo Type Your Year, then press enter.
rem set/p "myear=>"
set /p myyear="Enter myyear: " %=%
echo Type the year of the person's stuff you want to copy, then press enter.
rem set/p "yearofv=>"
set /p yearofv="Year of v: " %=%
cls
echo This is the difference between 2020 and "myyear..."
set/a difference=2020-%myyear%
echo %difference%
goto skip
set/p year="2020-%yearofv%"
md "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Documents"
copy "c:\%year%\%user%\My Documents" "c:\%myyear%\%myuser%\My Documents\File Copier\%user%'s Data\Document
:skip
By the way, I placed an "#echo off" at the top to make it easier to read the flow when executing the script.
Also, you might consider removing the "cls" that you have until you have it debugged. While debugging you need to be able to view the history of what has happened.

Powershell 4.0 - plink and table-like data

I am running PS 4.0 and the following command in interaction with a Veritas Netbackup master server on a Unix host via plink:
PS C:\batch> $testtest = c:\batch\plink blah#blersniggity -pw "blurble" "/usr/openv/netbackup/bin/admincmd/nbpemreq -due -date 01/17/2014" | Format-Table -property Status
As you can see, I attempted a "Format-Table" call at the end of this.
The resulting value of the variable ($testtest) is a string that is laid out exactly like the table in the Unix console, with Status, Job Code, Servername, Policy... all that listed in order. But, it populates the variable in Powershell as just that: a vanilla string.
I want to use this in conjunction with a stored procedure on a SQL box, which would be TONS easier if I could format it into a table. How do I use Powershell to tabulate it exactly how it is extracted from the Unix prompt via Plink?
You'll need to parse it and create PS Objects to be able to use the format-* cmdlets. I do enough of it that I wrote this to help:
http://gallery.technet.microsoft.com/scriptcenter/New-PSObjectFromMatches-87d8ce87
You'll need to be able to isolate the data and write a regex to capture the bits you want.

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 do I manipulate data out of a variable in powershell

I have a power-shell script with which I am trying to back up a constantly changing number of SQL databases. Fortunately all of these databases are listed in a registry key. I am leveraging this in a for-each loop. The issue that I am having is that after grabbing the registry value that I want, when I try to pass it into my function to back up the databases there seems to be information in the variable that I can get rid of. If I output the contents of the variable to the screen by just calling the variable ($variable) is shows just fine. But if I write-host the variable to the screen the extra "content" that shows up when calling the function also shows up.
Here is the part of the script that generates the contents of the variable.
foreach ($childitem in get-childitem "HKLM:\SOFTWARE\Wow6432Node\Lanovation\Prism Deploy\Server Channels")
{$DBName = get-itemproperty Registry::$childitem | select "Channel Database Name"
write-host $DBname}
Here is what write-host displays :
#{Channel Database Name=Prism_Deploy_Sample_268CBD61_AC9E_4853_83DE_E161C72458DE}
but what I need is only this part :
Prism_Deploy_Sample_268CBD61_AC9E_4853_83DE_E161C72458DE
I have tried looking online at how to do this, and what I've found mentions things similar to $variable.split and then specifying my delimiters. But when I try this I get an error saying "Method invocation failed because [System.Management.Automation.PSCustomObject] doesn't contain a method named 'split'."
I'm at a loss as to where to go from where I'm at currently.
select-object will return an object that has the named properties that you "select". To get just value of that property, just access it by name:
write-host $DBname."Channel Database Name"
Sounds like it's returning a hash table row object.
Try
write-host $DBName.value
or, failing that, do a
$DBName | Get-member
When in doubt, get-member gives you a nice idea of what you are dealing with.
You should be able to write
foreach ($childitem in get-childitem "HKLM:\SOFTWARE\Wow6432Node\Lanovation\Prism Deploy\Server Channels")
{$DBName = get-itemproperty Registry::$childitem | select "Channel Database Name"
write-host $DBname.Name}
to get what you are looking for