Solved

dissecting a Powershell string from a script

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

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

Independent Software Vendors: 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

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 …
Recently we ran in to an issue while running some SQL jobs where we were trying to process the cubes.  We got an error saying failure stating 'NT SERVICE\SQLSERVERAGENT does not have access to Analysis Services. So this is a way to automate that wit…
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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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