Solved

easy powershell question

Posted on 2014-02-05
6
311 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 40

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 69

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
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

Suggested Solutions

Microsoft Windows Server Update Service (WSUS) is free for everyone, but it lacks of some desirable features like send an e-mail to the administrator with the status of all computers on the WSUS server. This article is based on my PowerShell script …
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

821 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