Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

dissecting a Powershell string from a script

Posted on 2013-01-12
3
Medium Priority
?
259 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
[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
3 Comments
 
LVL 40

Assisted Solution

by:als315
als315 earned 1000 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 41

Accepted Solution

by:
footech earned 1000 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

Looking for the Wi-Fi vendor that's right for you?

We know how difficult it can be to evaluate Wi-Fi vendors, so we created this helpful Wi-Fi Buyer's Guide to help you find the Wi-Fi vendor that's right for your business! Download the guide and get started on our checklist today!

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
In previous parts of this Nano Server deployment series, we learned how to create, deploy and configure Nano Server as a Hyper-V host. In this part, we will look for a clustering option. We will create a Hyper-V cluster of 3 Nano Server host nodes w…
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
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…

704 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