Solved

dissecting a Powershell string from a script

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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article will help you understand what HashTables are and how to use them in PowerShell.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…
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…

680 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