Solved

easy powershell question

Posted on 2014-02-05
6
305 Views
Last Modified: 2014-02-05
Something stupid that I overlooked I'm sure, but
(a) I'm not getting my verbose messages here - script is prefixed with cmdletbinding, so I don't know why this isn't working
(b) I'm not getting my final info values in my $info array

[Cmdletbinding()]
param()

function addtoinfo ($msg) {
  $info += $msg
  write-warning $msg
}

function checkit {
  write-verbose "testing"
  addtoinfo "my info"
  write-verbose "more testing"
  addtoinfo "more info"
}

$info = @()
checkit -verbose

write-host "finished"

write-host $info

Open in new window

0
Comment
Question by:sirbounty
6 Comments
 
LVL 39

Assisted Solution

by:Vasil Michev (MVP)
Vasil Michev (MVP) earned 200 total points
ID: 39836137
You have added the [Cmdletbinding()] to the script, not the function. It will work if you run the script and pass the "-verbose" parameter.

Or change it to this:

function checkit {
    [Cmdletbinding()]
    param()

  write-verbose "testing"
  addtoinfo "my info"
  write-verbose "more testing"
  addtoinfo "more info"
}

Open in new window

0
 
LVL 40

Accepted Solution

by:
Subsun earned 200 total points
ID: 39836169
Variables inside functions don't live outside the the function, you need to define scope..

$script:info += $msg

Regarding verbose, I don't see any issue with with code.. What you get if you run the code with -verbose switch?

.\script.ps1 -Verbose

If it is not displaying anything check the $VerbosePreference value..
0
 
LVL 68

Expert Comment

by:Qlemo
ID: 39836203
Or you call the script with the dot operator:
  . .\theScript.ps1
this will allow to get all changes applied to the current shell session. That also means those functions are now declared for the session lifetime, and not dismissed after the script ran.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 67

Author Comment

by:sirbounty
ID: 39836299
Okay, that kind of sorts both issues, but my last write of $info is
my info my other info
instead of

my info
my other info
0
 
LVL 40

Expert Comment

by:Subsun
ID: 39836339
That's because you are using Write-host to display the array.. Try with Echo (Write-Output)
0
 
LVL 67

Author Closing Comment

by:sirbounty
ID: 39836340
nevermind - it was a type-o.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will help you understand what HashTables are and how to use them in PowerShell.
A brief introduction to what I consider to be the best editor for PowerShell.
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

914 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now