Solved

easy powershell question

Posted on 2014-02-05
6
312 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
6 Comments
 
LVL 41

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 70

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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This article will help you understand what HashTables are and how to use them in PowerShell.
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…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

738 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