Solved

dissecting a Powershell string from a script

Posted on 2013-01-12
3
253 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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Synchronize a new Active Directory domain with an existing Office 365 tenant
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

747 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

11 Experts available now in Live!

Get 1:1 Help Now