Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

easy powershell question

Posted on 2014-02-05
6
Medium Priority
?
315 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 43

Assisted Solution

by:Vasil Michev (MVP)
Vasil Michev (MVP) earned 800 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 800 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 71

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

Technology Partners: 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

A brief introduction to what I consider to be the best editor for PowerShell.
The Nano Server Image Builder helps you create a custom Nano Server image and bootable USB media with the aid of a graphical interface. Based on the inputs you provide, it generates images for deployment and creates reusable PowerShell scripts that …
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…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

636 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