function User-Search($input)
{
Write-Host "Searching for user: $input"
pause
}
function Show-Menu
{
param (
[string]$Title = 'MainMenu'
)
cls
Write-Host "================ $Title ================"
Write-Host " "
Write-Host "Specify computer / username"
Write-Host " "
Write-Host "Q: Press 'Q' to quit."
Write-Host " "
}
do
{
Show-Menu
$input = Read-Host "Search"
User-Search -input $input
}
until ($input -eq 'q')
Outputs: "Searching for user:", it's empty.
There must be some small mistake i am doing, probably easy for you guys :)
$INPUT is an automatic variable:
Contains an enumerator that enumerates all input that is passed to a
function. The $input variable is available only to functions and
script blocks (which are unnamed functions).
So just use another variable, e. g. $user instead of $input
Related
I have a large Powershell script that checks multiple variables on VMs. The script consists of about 80 different functions that are named question1, question2, question3...
At first none of the functions needed parameters, so this code worked.
$number_of_questions = 1..75
foreach($num in $number_of_questions){
Invoke-Expression question$num
}
It iterates thru every question
But now i need to add parameters for when i run the functions. And that doesn't work. And i cant find a way to get it to work with arguments
HereĀ“s a testversion of what im trying to do.
function test1($text){
Write-host "Not argument"
Write-host $text
}
function test2($text){
Write-host "Not argument"
Write-host $text
}
function test3($text){
Write-host "Not argument"
Write-host $text
}
function test4($text){
Write-host "Not argument"
Write-host $text
}
function test5($text){
Write-host "Not argument"
Write-host $text
}
$num = 1..5
foreach($number in $num){
Invoke-Expression test$number -text "Argument"
}
Does anyone have a solution for running multiple functions with sequenced names that uses parameters.
Just replace:
Invoke-Expression test$number -text "Argument"
with:
Invoke-Expression "test$number -text `"Argument`""
to make it work.
I would really appreciate it if somebody could point out what I am doing wrong in passing parameters from a function back to the mainline code. I have a variable which has been successfully extracted in a function, but I cannot seem to pass that back to the mainline code
This is the code I am using:
function get-field ($field, $heading) {
$fieldPos = $script:source.AllElements.InnerText.IndexOf($heading) +1
$field = $script:source.AllElements.InnerText[$fieldPos]
# If states "Not Available", or contains a heading, process as if not found.
if ($field -eq "Not Available ") {$fieldPos = 0}
if ($field -eq $heading) {$fieldPos = 0}
# Check that a valid entry was received
if ($fieldPos -eq 0) {
Write-Host "Warning:" $heading "was not found"
} else {
$field = $field.Trim()
}
return $field
}
get-field $email "Name"
get-field $address "Address"
I have verified that within the function, the $field and $heading parameters contain the correct information, so why aren't the $email and $address fields being populated?
You're not doing it totally wrong.
Have a look at this example:
function get-field ($field, $heading) {
return "$field - $heading"
}
$address = get-field "AddressFiled" "AddressHeading"
$address
to catch the returned value in a variable for further use, you should call the function like in the above example.
Parameters in PowerShell are normally used for passing values into a function. The output of a function must be assigned to a variable in the statement that invokes the function. Also, it's bad design to use global variables inside a function, because that makes debugging significantly more difficult.
Your code should look somewhat like this:
function Get-Field ($data, $heading) {
$fieldPos = $data.IndexOf($heading) + 1
$field = $data[$fieldPos].Trim()
# If states "Not Available", or contains a heading, process as if not found.
if ($field -eq 'Not Available' -or $field -eq $heading) {
Write-Host "Warning: ${heading} was not found"
}
$field
}
$email = Get-Field $script:source.AllElements.InnerText 'Name'
$address = Get-Field $script:source.AllElements.InnerText 'Address'
You can have out parameters if you want to, but they're rather uncommon in PowerShell, probably because they're not as straight-forward to use as one would like.
function Get-Field ([ref]$field, $data, $heading) {
$fieldPos = $data.IndexOf($heading) + 1
$field.Value = $data[$fieldPos].Trim()
# If states "Not Available", or contains a heading, process as if not found.
if ($field -eq 'Not Available' -or $field -eq $heading) {
Write-Host "Warning: ${heading} was not found"
}
}
$email = $null
Get-Field ([ref]$email) $script:source.AllElements.InnerText 'Name'
$address = $null
Get-Field ([ref]$address) $script:source.AllElements.InnerText 'Address'
stuck at this one trying to store function output in a variable:
function AD-prompt($Text)
{
do
{
$in = read-host -prompt "$Text"
}
while($in -eq "")
}
calling the function with
$type = AD-prompt "Sample Text"
does not store anything in $type - only when i remove the entire do-while loop it works. it seems the function output is empty as the read-host output is stored in the $in variable, but i have no idea how to solve this - i didnt find another way to loop the read-host aswell sadly.
You need to return $in from your function by outputting it. You can do this by putting it on a line on its own after your loop:
function AD-prompt($Text)
{
do
{
$in = read-host -prompt "$Text"
}
while($in -eq "")
$in
}
I am attempting to write a function to compress files using 7zip, but I am having issues passing multiple parameters to the function.
$In = "C:\test\gateways_25357_20140407000204.pcap"
$Out = "C:\test\gateways_25357_20140407000204.zip"
function CompressFile([string]$Output,[string]$Input) {
Write-Host $Output
write-host $Input
$7zipPath = "C:\Program Files\7-Zip\7z.exe"
$Arguments = "a","-tzip",$Output,$Input
& $7zipPath $Arguments
}
CompressFile $Out $In
My results of this code is the compressing of the files in the working directory of this script and the output goes to the correct location c:\test.
What exactly am I doing wrong here with passing in the $Input parameter?
$Input is a powershell automatic variable, try changing the name.
see
$In = "C:\test\gateways_25357_20140407000204.pcap"
$Out = "C:\test\gateways_25357_20140407000204.zip"
function CompressFile([string]$Outputz, [String]$Inputz) {
Write-Host $Outputz
write-host $Inputz
}
Write-Host $Out
write-host $In
CompressFile $Out $In
http://technet.microsoft.com/en-us/library/hh847768.aspx
Trying to create a PowerShell function that will output a single-line of text using multiple sets of fore and back colors. I have a switch that defines the color sets.
The function has one param that defines the switch value and another param that, if I can get this working, defines the next color set using the same switch:
function Write-Custom
{
param($Say,$ThenSay,$Level,$ExtraLevel)
switch([array]$level)
{
none {$c = 'Black','White'}
name {$c = 'Cyan','DarkBlue'}
good {$c = 'White','DarkGreen'}
note {$c = 'Gray','White'}
info {$c = 'White','DarkGray'}
warn {$c = 'Yellow','Black'}
fail {$c = 'Black','Red'}
}
$s = " $Say"
$ts = " $ThenSay "
Write-Host $s -ForegroundColor $c[0] -BackgroundColor $c[1] -NoNewLine
Clear-Variable Level
$Level = $ExtraLevel
Write-Host $ts -ForegroundColor $c[0] -BackgroundColor $c[1]
}
Write-Custom -Say 'hi there' -Level 'name' -ThenSay 'stranger ' -ExtraLevel 'warn'
Can't seem to clear and re-define the $level variable. Seems the output ' hi there ' should have a foreground/background of cyan/darkblue, with the ' stranger ' part being yellow/black....but the whole string comes out cyan/darkblue.
Do I need to create a more elaborate switch?
You need to invoke the switch each time to get a different color set. One way to do this is to put a function inside your function e.g.:
function Write-Custom
{
param($Say,$ThenSay,$Level,$ExtraLevel)
function GetColors([string]$level)
{
switch([array]$level)
{
none {'Black','White'}
name {'Cyan','DarkBlue'}
good {'White','DarkGreen'}
note {'Gray','White'}
info {'White','DarkGray'}
warn {'Yellow','Black'}
fail {'Black','Red'}
default { throw "Unrecognized level $level" }
}
}
$c = GetColors($Level)
Write-Host " $Say" -ForegroundColor $c[0] -BackgroundColor $c[1]
$c = GetColors($ExtraLevel)
Write-Host " $ThenSay " -ForegroundColor $c[0] -BackgroundColor $c[1]
}