r/PowerShell • • Apr 29 '19

A Glorious Day

Post image
462 Upvotes

65 comments sorted by

4

u/Ravin66 Apr 29 '19

It would probably be better to have your if check above buying the coffee otherwise you're wasting a lot of coffee.

3

u/RaevenFairchilde Apr 29 '19

You can never have too much coffee... ;) but valid point!

5

u/RaevenFairchilde Apr 29 '19

I dont know who came up with this, but loved coming across this framed picture

3

u/ExEvolution May 01 '19
Get-Coffee -Roast Dark -Creamer $(If ($HasHadBreakfast -eq $true){$false}Else{$true})

I don't like calling it black and then having to specify creamer separately LMAO

2

u/xxxsirkillalot Apr 30 '19

Statically assigning variables instead of using parameters. $creamer will definitely be $true

Always write code like the person who touches it after you is a serial killer and knows where you live. You just told the serial killer he can't have creamer. Why did you do this to yourself? (kidding, but also not... kinda, maybe)

1

u/RaevenFairchilde Apr 30 '19

Hahahaha that's great

103

u/Dogoodwork Apr 29 '19

%USERNAME%

yuck

$env:USERNAME

21

u/theblindness Apr 29 '19 edited Apr 29 '19

I don't think %variables% are even supported since % is an alias to ForEach-Object. In this case, it looks like User is probably a string, so instead of terminating with a big red error message, it might just silently pass the string "%USERNAME%" without expanding the environmental variable or any warning that the input was wrong.

36

u/bsnotreallyworking Apr 29 '19
Handle-Item -Like 'Boss'

You're handling your boss?

6

u/The1Shiner Apr 29 '19

First thing I thought.

8

u/[deleted] Apr 30 '19

Best way to get promoted.

5

u/DragonDrew Apr 29 '19

I think it means they are going to outsource the work to a junior and then go out for a liquid lunch.

23

u/[deleted] Apr 29 '19

Those brackets!!!

6

u/moebaca Apr 29 '19

Yeah I didn't want to say anything but those brackets need some serious love.

2

u/ReverendDS Apr 29 '19

Can you explain?

I'm not doubting/questioning, just not understanding.

8

u/[deleted] Apr 29 '19

I think they want to see:

IF($foo){

$bar

}

instead of

IF($foo)

{

$bar

}

13

u/ciacco22 Apr 29 '19

I know I’m in the minority, and it isn’t really in the style of PowerShell, but I prefer my curly braces to line up, like the OP. I think it’s a holdover from my C++ classes in college (20 years ago).

There are others out in the wild. And we always get excited when we find one another.

1

u/chen1201 Apr 29 '19

haha that's how I setup my brackets...am I doing it wrong?

12

u/[deleted] Apr 30 '19

Yes. But it won’t hurt anything or anyone and if you’re consistent through your script it will be easy to read so continue on. Just know I hate you for doing it different than me.

2

u/missingMBR Apr 30 '19

It's a lesser evil to inconsistent indentation or no indentation at all.

2

u/chen1201 Apr 30 '19

Ah I see, I only do it this way because that's how I learned it when taking Java courses back in school. Is there a reason why Powershell users use this method instead? Or is this mostly just preference?

2

u/[deleted] Apr 30 '19

No idea. Just how i done saw it so it's how i did it. I stand by the fact that it really doesn't matter as long as its consistent and reasonably easy to find the brackets and their homies.

1

u/Lee_Dailey [grin] Apr 30 '19

howdy chen1201,

some of the places where you can use a scriptblock REQUIRE the opening { to be right after the thing it belongs to. for instance, this works ...

'af', '12' |
    ForEach-Object {
        $_
        }

... but this fails ...

'af', '12' |
    ForEach-Object 
        {
        $_
        }

... and asks for input. [grin]

i prefer something like this ...

foreach ($Item in 1..10)
    {
    [math]::Pow($Item, 2)
    }

... but most others prefer something like the following ...

foreach ($Item in 1..10)
{
    [math]::Pow($Item, 2)
}

... or like this ...

foreach ($Item in 1..10) {
    [math]::Pow($Item, 2)
}

i far prefer the very , very, very in-your-face style where everything subordinate to the control block is indented one level. [grin]

take care,
lee

9

u/ciacco22 Apr 29 '19

The majority of PowerShell scripts have the left curly brace on the same line as the conditional statement and the right curly brace aligned at the same horizontal position of the start of the conditional.

1

u/[deleted] Apr 29 '19

I agree with you; I also dont care if others dont like it :P

0

u/[deleted] Apr 29 '19

[deleted]

2

u/[deleted] Apr 29 '19

Damn, formatting messed up on my reply.

6

u/[deleted] Apr 30 '19

Always keep the opening curlybracket directly under the statement 😁

20

u/FalconChucker Apr 29 '19

You might want a $Temperature check before you -force your coffee.

13

u/majtom Apr 30 '19

I audibly laughed at the -force on Cusume-Coffee line

4

u/Illbatting Apr 30 '19

Yeah I mean, come on... -Force, really? -Throttle makes more sense to me personally. :)

Also 4 cups is a good start but far from done. :P

4

u/Yevrag35 Apr 30 '19
do
{
    Enjoy-Day
}
while ($isDrinking)

7

u/WAN_S0L0 Apr 29 '19

Shouldn't there be an Out-Null after the consume coffee?

4

u/TheIncorrigible1 Apr 29 '19

Don't use Out-Null.

1

u/WAN_S0L0 Apr 29 '19

Why not?

0

u/[deleted] Apr 29 '19

[deleted]

2

u/WAN_S0L0 Apr 29 '19

For someone who isn't a programmer, I have always used out-null to just not display the output of the command.

Is there a better way to do this?

3

u/TheIncorrigible1 Apr 29 '19

Literally any of the three ways I posted in my comment. They all function the same, better.

1

u/WAN_S0L0 Apr 29 '19

I don't understand why you're saying they are better though.

For me all I do is dump the pipeline output, this happens so quickly I don't even notice it in the scripts I run.

So if out-null takes 1ms and these take .8ms, what is the benefit and what makes them faster and.or better?

1

u/signofzeta Apr 30 '19

Thanks! I’ll still use Out-Null where I’m putting readability over performance; as the only guy who knows PowerShell at my job, I try to make my code readable in hopes they’ll see the light.

1

u/Mkep May 01 '19

But comments....

6

u/Ta11ow Apr 30 '19

The arbitrary capitalization, aliases, and naming of functions is really giving me grief here.

Also, while /u/Lee_Dailey probably loves it, I still can't accept Whitesmith's as a proper indentation/bracing style.

3

u/Lee_Dailey [grin] Apr 30 '19

howdy Ta11ow,

whitesmiths is the only truly sensible way ... but us sensible folks are aware that some others have a slightly understandable rationale for keeping the opening { on the same line as the control block. [grin]

take care,
lee

2

u/DrSinistar May 01 '19

But... why? I find it so hard to read! It's contrary to every language I have ever worked with.

2

u/Lee_Dailey [grin] May 01 '19

howdy DrSinistar,

for the reasons given in the style guide comparison over at wikipedia, here ... [grin]

Indentation style - Wikipedia
— https://en.wikipedia.org/wiki/Indentation_style#Whitesmiths_style

take care,
lee

3

u/Yevrag35 Apr 30 '19

At least I'm not the only one :P

2

u/DrSinistar Apr 30 '19

Yeah these indented braces are causing me physical pain.

4

u/ShadySeptapus Apr 29 '19

ALL GLORY TO THE HYPNOTOAD!

2

u/The-Dark-Jedi Apr 29 '19

So far it looks only like #Glorious Morning rather than #Glorious Day. Looking forward to the finished product. ;-)

2

u/ryanknapper Apr 30 '19

The "$CUPS_CONSUMED -gt 4" comes after Consume-Coffee. OP will still be drinking cups of coffee, but logging an error with each one after four.

Although, the way this is written no more than one cup of coffee will ever be consumed.

2

u/[deleted] Apr 30 '19

$cred = Get-Credential

2

u/baycityvince May 01 '19

Line 2, Greet-Person cmdlet really should be updated to accept pipeline input. 😁

3

u/puggo12 Apr 29 '19

I love this

2

u/TheBlackArrows Apr 29 '19

The fact that someone framed it, that makes it all the better.

1

u/MaxHedrome Apr 30 '19

Can I get an

$grade += 1

if ($grade -gt 3) { write-warning: “Billy Passed” }

1

u/LeSpatula Apr 30 '19

Ist greet an approved verb?

2

u/signofzeta Apr 30 '19

No. Use Invoke-Greeting, Start-Computer, and Get-Coffee | Start-CoffeeConsumptionJob

1

u/coderkid723 Apr 30 '19

This is fantastic, I was gonna come up with a poweshell pun, but haven’t executed the coffee loop yet this morning!

1

u/missingMBR Apr 30 '19

Any reason why Authenticate doesn't follow the verb-noun format?

1

u/deadflamingo Apr 30 '19

Maybe consuming coffee should be in a try catch block and throw an error instead of a warning about aborting.