Solved

easy powershell question

Posted on 2014-02-05
6
303 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 38

Assisted Solution

by:Vasil Michev (MVP)
Vasil Michev (MVP) earned 200 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 67

Author Comment

by:sirbounty
Comment Utility
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
Comment Utility
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
Comment Utility
nevermind - it was a type-o.
0

Featured Post

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

743 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

18 Experts available now in Live!

Get 1:1 Help Now