Solved

dissecting a Powershell string from a script

Posted on 2013-01-12
3
254 Views
Last Modified: 2013-01-15
Greetings,

I received a script where the first two lines have me a bit befuddled. Can you help me break apart how it works?

function IsAlive ([string] $computer = $(throw "Computer name missing"))
#returns $true if snamed computer reachable. $false otherwise
0
Comment
Question by:johndarby
3 Comments
 
LVL 39

Assisted Solution

by:als315
als315 earned 250 total points
ID: 38770377
You can read about throw keyword here:
http://technet.microsoft.com/en-us/library/dd819510.aspx
In this case if computer name is missing, immediately will be returned error "Computer name missing"
Second line is comment (#)
0
 
LVL 39

Accepted Solution

by:
footech earned 250 total points
ID: 38770687
Just a little more explanation on why it works like als315 described...
The function IsAlive accepts the parameter called $computer.  $computer is cast as a string type, and when the function is called $computer has a default value of $(throw "Computer name missing")).  The default value will be used when no other value has been given to it.
So if the function was called with
IsAlive
throw generates a terminating error.  But if the function was called with
IsAlive wkstn1
then $computer now equals the string "wkstn1" and the function continues.
0
 
LVL 1

Author Closing Comment

by:johndarby
ID: 38779149
Thank you, both!
0

Featured Post

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.

Question has a verified solution.

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

The article will show you how you can maintain a simple logfile of all Startup and Shutdown events on Windows servers and desktops with PowerShell. The script can be easily adapted into doing more like gracefully silencing/updating your monitoring s…
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…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

861 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

25 Experts available now in Live!

Get 1:1 Help Now