Showing posts with label PowerShell User Group. Show all posts
Showing posts with label PowerShell User Group. Show all posts

11/16/2016

Using PATCH with PowerShell’s Invoke-RestMethod

 

A story about a bug, an inconsistency and a solution…

 

I recently did a demo of using PowerShell's Invoke-RestMethod to create, read, update and delete (CRUD) data to a REST service written using ASP.NET's WEBAPI project template and ODATA controllers. Everything worked pretty much as expected except for using the PATCH method to change an existing item.

My GET worked as expected:

  Invoke-RestMethod 'http://localhost:41613/odata/Courses' | select -ExpandProperty value

image_thumb[21]

 

My DELETE worked as expected:

  Invoke-RestMethod 'http://localhost:41613/odata/Courses(33)' -Method DELETE

 

My POST (create) worked as expected:

  $bodynew = @{ CourseCode='aa111'; Description='test'; Category='test'; Title='Test Course'}
  invoke-restmethod 'http://localhost:41613/odata/Courses' -Method POST -Body $bodynew

image_thumb[22]

 

My PATCH (and MERGE) failed!

  $bodyupdate = @{ Title='Updated Title!'}
  invoke-restmethod 'http://localhost:41613/odata/Courses(34)' -Method POST -Body $bodyupdate

image_thumb[23]

At least that gave me two hints… "no body" and "The inferred media type 'application/octet-stream' is not supported for this resource." The second one was probably the easiest to fix… tell it that I'm sending JSON.

   $headerJSON = @{ "content-type" = "application/json;odata=verbose"}

image_thumb[24]

This got past the Invoke-RestMethod error when using PowerShell 4, but got a new error when using PowerShell 3! "Invoke-RestMethod : The 'content-type' header must be modified using the appropriate property or method." (it's a bug!) But now my ASP.NET WebApi application threw an error, "NullReferenceException". No data was received! (Remember the "no data" error?) So, maybe it was not in the expected format. When using the jQuery AJAX method you serialize your object into JSON before sending. Maybe that would work here:

  $bodyupdateAsJSON = @{ Title='Updated Title!'} | ConvertTo-Json
  Invoke-RestMethod 'http://localhost:41613/odata/Courses(34)' -Method PATCH -Body $bodyupdateAsJSON
      -Headers $headerJSON

image_thumb[25]

  Bingo!

 

The Question then is…

Why can I pass a -Body for a create (POST) without any additional work, but when I pass a -Body with an update (PATCH or MERGE), I have to pass the data as JSON and add a header to state that I'm sending JSON?

 

Will POST work with those two changes?

Actually it will! Invoke-RestMethod will do a POST using default PowerShell objects for -Body as long has you don't add a -Header that specifies JSON as the format. Invoke-RestMethod will also do a POST using JSON data as long as you do supply the right -Header. It's probably a best practice to be consistent and explicitly use JSON for both.

image_thumb[26]

 

Live and learn…

 

 

Details:

Tested with PowerShell 3.0 and 4.0. PowerShell 3.0 fails when trying to set a header for JSON with "Invoke-RestMethod : The 'content-type' header must be modified using the appropriate property or method." (It's a bug!)

The .NET application:

  • Visual Studio 2015 ASP.NET Web API project with ODATA controllers
  • .NET Framework 4.5.2
  • Entity Framework 6.0
  • SQL Server in Azure
  • Test project hosted in Azure.

.

5/16/2016

Ed Wilson, the Microsoft Scripting Guy, Cincinnati June 15th!

 

Ed Wilson, the Microsoft Scripting Guy, will be presenting at the Cincinnati PowerShell User Group meeting on June 15th at MAX Technical Training.

Topic: Configuration Management with Azure Automation DSC - Cloud & On-Prem, Windows & Linux

Info and regestration here: http://www.meetup.com/TechLife-Cincinnati/events/230743256/

See you there!

11/17/2015

PowerShell Get-Date vs. [DateTime]

 

The Cincinnati PowerShell User Group (@CincyPowershell) opens each meeting with a review of a cmdlet. Tonight is was Get-Date's turn. Many of the examples were done with both Get-Date and [datetime], with one example showing that only [datetime] supports the IsLeapYear() method. This got me to thinking… (sometimes a painful thing…) and the light bulb lit up over my head. Get-Date returns an instance of the System.DateTime class, i.e. an object, while [datetime] is a reference to the class and provides easy access to the class's static methods.

 

System.DateTime

To be precise, [datetime] is really [System.DateTime], but System is the default name space and PowerShell is not case sensitive (most of the time). And to be precise, Get-Date with no options is the same as[System.DateTime]::Now.

What's with the two colons? They are PowerShell's way to call a static method on a class. So going back to the IsLeapYear() question, it is a static method of the DateTime class. To access it we write [System.DateTime]::IsLeapYear(2016).

 

Get-Date

Get-Date is a cmdlet with many options, but the default option is to return the current date and time. I.e. [System.DateTime]::Now.

The class that an object was created from is also called it's type. So what's the "type" of a date?

image

 

Instances / Objects

If Get-Date returns an instance, how could I do that with [DateTime]? Two ways, use one of the static methods like .Now or .Today, or create a new object using the New-Object cmdlet.

Using the static methods:

$d = [System.DateTime]::Now    (or just [DateTime].Now)

$d = [System.DateTime]::Today  (or just [DateTime].Today)

Using New-Object:

$d = New-Object System.DateTime    (or just DateTime)

The unique aspect of using New-Object with DateTime is that you get back an object with default values for year, month, day, etc. and not today's date. The actual result is "Monday, January 01, 0001 12:00:00 AM".

image

If you want to create a particular date or time, use one of the class's constructors. (See the DateTime Structure link at the end of the article for details.)

image

There's a nice write up about classes, instances, statics here from Don Jones.

 

And just for fun… and only in PowerShell 5.0

Who needs [System.DateTime]?  Just call the static methods off of any DateTime object! The following all produce the same result, keeping in mind that Get-Date adds the additional overhead of retrieving the current date, and then ignoring it.

(Get-Date)::IsLeapYear(2016)

$d = Get-Date
$d::IsLeapYear(2016)

$d = [System.DateTime]::Now
$d::IsLeapYear(2016)

 

What about PowerShell 3.0?

The examples for 5.0 do not work with 3.0. We have to ask for the "class", i.e. it's Type.

(Get-Date).GetType()::IsLeapYear(2016)

$d = Get-Date
$d.GetType()::IsLeapYear(2016)

$d = [System.DateTime]::Now
$d.GetType()::IsLeapYear(2016)

 

What about PowerShell 4.0?

Beats me! (Don't have a copy in front of me right now.)

 

Discover the Static Methods and Properties of an Object

Get-Member has a –Static parameter to display the static methods and properties of an object:

Get-Date | Get-Member -Static

image

 

More Fun With Static Methods

Most .Net classes are not exposed in PowerShell as cmdlets.

For example, you might need a new GUID…

[System.Guid]::NewGuid()   will return a new GUID object.

[System.Guid]::NewGuid().toString()   will return a new GUID as a string.

image

How about some Math?

Need the value of PI?

[System.,Math]::PI  (or just [Math]::PI)  It's about 3.14159265358979 according to PowerShell and .Net.

How about the square root of 2?

[Math]::Sqrt(2)  (Let's see… I think it's about 1.4142135623731.)

And of course, the Sine of 45 degrees:

[Math]::sin(0.785398163397448)  (in Radians of course) Oh, about 0.707106781186547.

And doing a little converting from degrees to Radians:

[Math]::sin( [Math]::pi/180*45 )

For more of what the Math class can do, see the Math link at the end of the article. Or, wait for it, use PowerShell:

[Math] | Get-Member  -Static

 

Bottom line… if the C# guys can do it, then so can you!

 

So now you know… 2016 is a leap year!  And at least a dozen different ways!

 

(and a quick 5 minute article took over an hour…)

 

References:

Using Static Classes and Methods
https://technet.microsoft.com/en-us/library/dd347632.aspx

How to Create an Object in PowerShell
http://blogs.msdn.com/b/powershell/archive/2009/03/11/how-to-create-an-object-in-powershell.aspx

DateTime Structure (All of the constructors, methods and properties of DateTime)
https://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

A nice write up on class, object, instance and static by Don Jones:
http://powershell.org/wp/2014/09/08/powershell-v5-class-support/

System.Math Class
https://msdn.microsoft.com/en-us/library/system.math(v=vs.110).aspx

 

.

8/18/2015

Cincinnati PowerShell User Group Meeting

 

Register here: (free!) http://www.meetup.com/TechLife-Cincinnati/events/224705451/

Cincinnati PowerShell Users group – August Meeting

When: August 27th
Where: Max Technical Training
Mason, OH

PS C:\> Get-PSUG | where {$_.City -eq Cincinnati}

The Cincinnati PowerShell user group is back and under new management!  Last meeting was great with Ed Wilson as our presenter, I hope you all got as fired up about scripting as we did!

Come join us as we dive into….

Version Control?  That's for Developers, not Admins!!

Does your version control look like this?!?!?

We'll cover different ways to approach source control with PowerShell, why we as System Administrators need it, and why the idea of "infrastructure-as-code" is getting so much traction these days.

 

Sponsor!

SAPIEN Technologies will be sponsoring the Cincinnati PowerShell User Group meeting and will be supplying the pizza!

.

6/01/2015

Cincinnati PowerShell User Group Meeting this week!


Info. and RSVP here: http://meetu.ps/2JkRsx


The PowerShell User Group is back! (But the future is up to you!)


     PS C:\> Get-Speaker | Plan-Meeting | ShowUp-AndLearn!


Sponsor:  SAPIEN Technologies
is supplying the food!
Ed Wilson "The Scripting Guy" from Microsoft will be here to help us (re)kickoff the PowerShell User Group. If you would like to see future meetings, place your vote by showing up for this meeting!
Wednesday, June 3rd 6:30 PM at MAX Technical Training. (Food and networking starts at 6:00!)



Speaker: Ed Wilson "The Scripting Guy"
Title: Garbage in, Garbage out: Data grooming with Windows PowerShell
Everyone has heard the old adage, "garbage in, garbage out" when talking about databases, or other online data storage / retrieval systems. But do you know that Windows PowerShell can help you with your problem? In this session, Microsoft Scripting Guy Ed Wilson talks about using Windows PowerShell to perform data grooming. He shows how cleaning up names, street addresses, cities, states, and even zip codes by using basic string manipulation techniques. By focusing directly on the data transformation itself, he extracts principles that can be used regards of the database, or other data storage system. After focusing on the individual components of the process, he puts the whole thing into a single script for transforming the sample data. This session is heavy with live demonstration.

Bio
Ed Wilson is the Microsoft Scripting Guy, and writes the daily Hey Scripting Guy blog. He is the co-founder of PowerShell Saturday, and the Charlotte PowerShell User Group. He is a frequent speaker at conferences, user groups and other places where groups of computer people may be found. He has written books about every version of Windows PowerShell, as well as other books related to automation and operating systems.  His most recent book is Windows PowerShell Best Practices.


.

4/14/2014

Cincinnati PowerShell User Group meeting announcement

 

From Rob Dreyer, the organizer:

Our first meeting in 2014 will be extra special. Throughout 2014, Dr. Ferdinand Rios, the CEO of SAPIEN Technologies, Inc. is touring around the world. Last week, I received word that on Thursday April 17th he will be coming to MAX! I nearly fell out of my seat when I heard.

Please register here: http://www.meetup.com/TechLife-Cincinnati/events/154731982/

See you there!

  • Thursday, April 17, 2014
    6:00 PM to 8:30 PM
     

How to keep track of the PowerShell user group:

3/04/2014

Cincinnati PowerShell User Group 4/17/14 – SAPIEN Technologies

 

I just got the email below from Rob Dreyer of the Cincinnati PowerShell User Group. Cool stuff!

Mark your calendar! April 17th 2014 at MAX Technical Training in Mason, Ohio.

For more information and to register for the event (free!): http://www.meetup.com/TechLife-Cincinnati/events/154731982/ 


Hello Everyone,

I have some amazing news – our first meeting in 2014 will be extra special. Throughout 2014, Dr. Ferdinand Rios, the CEO of SAPIEN Technologies, Inc. is touring around the world. Last week, I received word that on Thursday April 17th he will be coming to MAX! I nearly fell out of my seat when I heard.

 

clip_image002clip_image004clip_image005clip_image007clip_image009

And, if you haven’t tried PrimalScript or PowerShell Studio, now is the time!

clip_image010

Check out the links below for more details. Thanks, Ferdinand!

Free Software Upgrades: http://www.sapien.com/blog/2014/02/13/2014-versions-out-march-15-buy-now-and-save/

Free PowerShell Training: http://www.sapien.com/blog/2013/12/23/sapiens-full-powershell-video-catalog-now-on-you-tube/

Let’s plan to give him a warm welcome. We’ll be hosting the event at MAX, but I will also provide remote access using GoToMeeting for those who cannot attend. I’ll send meeting information out as we get closer to the event. Hope to see you soon!

1/23/2013

Cincinnati PowerShell User Group Meeting with Keith Mayer (Round 2)

 

Thursday 1/24/2013 6:00 at MAX Technical Training

Before our December meeting came to a close, Keith mentioned a follow-up presentation. I am pleased to announce he will be joining us again on Thursday January 24th! He will continue his presentation with the second half of PowerShell management and virtualization – this time with a focus on managing Windows Azure virtual machines via PowerShell.

Registration and other details here: http://www.meetup.com/TechLife-Cincinnati/events/98558502/

Registration is optional, but helps us with planning.

.

10/17/2012

Cincinnati PowerShell Events

 

Cincinnati PowerShell User Group

The next meeting of the Cincinnati PowerShell User Group is Thursday October 18th at MAX Technical Training. I'll be speaking on "How to use PowerShell to use, filter and merge data from multiple systems". I'll be showing how to work across three systems, AD, SharePoint and SQL to collect data about a subset of employees and post the results to a SharePoint list.

Go here to resister (not required to attend) http://www.meetup.com/TechLife-Cincinnati/events/84008152/

 

SharePoint Saturday Cincinnati

It's a SharePoint event, but there will be PowerShell content there! At least four sessions include PowerShell.

Go here for info and to register: http://www.sharepointsaturday.org/cincinnati
October 27th, 2010

.

8/26/2012

Final Reminder! First Ever Cincinnati PowerShell User Group Meeting 8/28!

 

Tuesday, August 28, 2012, 6:00 PM to 8:00 PM (add it to your calendar NOW!)

It's free! Go here and register NOW!
http://www.meetup.com/TechLife-Cincinnati/events/75092962/
(Registration is not required, but will help us plan for the event, order pizza and spread the word.)

Speaker is scheduled, SWAG is being acquired, people are signing up.

Ed Wilson, the Microsoft Scripting Guy and a well-known scripting expert, will be the kick off speaker for the new Cincinnati PowerShell user group. His topic will be "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation".

Goodies are on the way from Microsoft Press, O'Reilly, APress and others! Free books, discounts, and other stuff, all to be given away as door prizes. (But I know you are coming to hear Ed speak, not to win door prizes!)

Come prepared to network, talk PowerShell and even volunteer to support this new user group!

Spread the word!

Let your coworkers know. Blog and Tweet about it. (#cincypowershell should work)

Ed's Topic "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation"

There are four different ways to manage a remote Windows 8 workstation. The first is to use WMI remoting, the second is to use the computername cmdlets, the third is to use WinRm and Windows PowerShell native remoting, the last way is to use the CIM cmdlets. Each approach has advantages and disadvantages for the network administrator. In this session, I will examine each approach, and provide a checklist of criteria to aid the enterprise network administrator in choosing the appropriate technology for a variety of real world scenarios. This presentation combines live demo’s and interactive discussion to heighten learning.

About the presenter:

Ed Wilson is the Microsoft Scripting Guy and a well-known scripting expert. He writes the daily Hey Scripting Guy! blog. He has also spoken multiple times at TechEd as well as at the Microsoft internal Tech Ready and Geek Ready conferences. He has also spoken at the first SQL Rally conference in Orlando, as well as at numerous SQL Saturday events. He is a Microsoft-certified trainer who has delivered a popular Windows PowerShell workshop to Microsoft Premier Customers worldwide. He has written 9 books including 6 on Windows scripting that were published by Microsoft Press. He has also contributed to nearly a dozen other books. His Windows PowerShell 2.0 Best Practices book for Microsoft Press is currently a best seller. Ed holds more than 20 industry certifications, including Microsoft Certified Systems Engineer (MCSE), the Microsoft Certified Data Base Administrator (MCDBA) and Certified Information Systems Security Professional (CISSP). Prior to coming to work for Microsoft, he was a senior consultant for a Microsoft Gold Certified Partner where he specialized in Active Directory design and Exchange implementation. In his spare time, he enjoys woodworking, underwater photography, and scuba diving.

.

8/17/2012

Final Reminder! First Ever Cincinnati PowerShell User Group Meeting 8/28!

 

Tuesday, August 28, 2012, 6:00 PM to 8:00 PM (add it to your calendar NOW!)

It's free! Go here and register NOW!
http://www.meetup.com/TechLife-Cincinnati/events/75092962/
(Registration is not required, but will help us plan for the event, order pizza and spread the word.)

Speaker is scheduled, SWAG is being acquired, people are signing up.

Ed Wilson, the Microsoft Scripting Guy and a well-known scripting expert, will be the kick off speaker for the new Cincinnati PowerShell user group. His topic will be "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation".

Goodies are on the way from Microsoft Press, O'Reilly, APress and others! Free books, discounts, and other stuff, all to be given away as door prizes. (But I know you are coming to hear Ed speak, not to win door prizes!)

Come prepared to network, talk PowerShell and even volunteer to support this new user group!

 

Spread the word!

Let your coworkers know. Blog and Tweet about it. (#cincypowershell should work)

 

Ed's Topic "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation"

There are four different ways to manage a remote Windows 8 workstation. The first is to use WMI remoting, the second is to use the computername cmdlets, the third is to use WinRm and Windows PowerShell native remoting, the last way is to use the CIM cmdlets. Each approach has advantages and disadvantages for the network administrator. In this session, I will examine each approach, and provide a checklist of criteria to aid the enterprise network administrator in choosing the appropriate technology for a variety of real world scenarios. This presentation combines live demo’s and interactive discussion to heighten learning.

About the presenter:

Ed Wilson is the Microsoft Scripting Guy and a well-known scripting expert. He writes the daily Hey Scripting Guy! blog. He has also spoken multiple times at TechEd as well as at the Microsoft internal Tech Ready and Geek Ready conferences. He has also spoken at the first SQL Rally conference in Orlando, as well as at numerous SQL Saturday events. He is a Microsoft-certified trainer who has delivered a popular Windows PowerShell workshop to Microsoft Premier Customers worldwide. He has written 9 books including 6 on Windows scripting that were published by Microsoft Press. He has also contributed to nearly a dozen other books. His Windows PowerShell 2.0 Best Practices book for Microsoft Press is currently a best seller. Ed holds more than 20 industry certifications, including Microsoft Certified Systems Engineer (MCSE), the Microsoft Certified Data Base Administrator (MCDBA) and Certified Information Systems Security Professional (CISSP). Prior to coming to work for Microsoft, he was a senior consultant for a Microsoft Gold Certified Partner where he specialized in Active Directory design and Exchange implementation. In his spare time, he enjoys woodworking, underwater photography, and scuba diving.

.

7/26/2012

New Cincinnati PowerShell User Group!

 

Tuesday, August 28, 2012, 6:00 PM to 8:00 PM (add it to your calendar NOW!)

Ed Wilson, the Microsoft Scripting Guy and a well-known scripting expert, will be the kick off speaker for the new Cincinnati PowerShell user group. His topic will be "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation".

Come prepared to network, talk PowerShell and even volunteer to support this new user group!

It's free! Go here and register!
http://www.meetup.com/TechLife-Cincinnati/events/75092962/

Registration is not required, but will help us plan for the event, order pizza and spread the word!

 

Ed's Topic "Using Windows PowerShell 3.0 to manage the remote Windows 8 workstation"

There are four different ways to manage a remote Windows 8 workstation. The first is to use WMI remoting, the second is to use the computername cmdlets, the third is to use WinRm and Windows PowerShell native remoting, the last way is to use the CIM cmdlets. Each approach has advantages and disadvantages for the network administrator. In this session, I will examine each approach, and provide a checklist of criteria to aid the enterprise network administrator in choosing the appropriate technology for a variety of real world scenarios. This presentation combines live demo’s and interactive discussion to heighten learning.

 

About the presenter:

Ed Wilson is the Microsoft Scripting Guy and a well-known scripting expert. He writes the daily Hey Scripting Guy! blog. He has also spoken multiple times at TechEd as well as at the Microsoft internal Tech Ready and Geek Ready conferences. He has also spoken at the first SQL Rally conference in Orlando, as well as at numerous SQL Saturday events. He is a Microsoft-certified trainer who has delivered a popular Windows PowerShell workshop to Microsoft Premier Customers worldwide. He has written 9 books including 6 on Windows scripting that were published by Microsoft Press. He has also contributed to nearly a dozen other books. His Windows PowerShell 2.0 Best Practices book for Microsoft Press is currently a best seller. Ed holds more than 20 industry certifications, including Microsoft Certified Systems Engineer (MCSE), the Microsoft Certified Data Base Administrator (MCDBA) and Certified Information Systems Security Professional (CISSP). Prior to coming to work for Microsoft, he was a senior consultant for a Microsoft Gold Certified Partner where he specialized in Active Directory design and Exchange implementation. In his spare time, he enjoys woodworking, underwater photography, and scuba diving.

.

2/18/2012

PowerShell Saturday! -- Interested in a PowerShell user group in Cincinnati?

 

PowerShell Saturday in Columbus

The first ever PowerShell Saturday will be held in Columbus, OH on March 10th. If you have not already registered, then it’s too late! It’s a sell out! But you might want to check the link below to see if they open up any more tickets.

    Tickets: http://www.eventbrite.com/event/2860436643/eorg

    I got my ticket!

 

PowerShell in Cincinnati?

So… would you be interested in either a Cincinnati area PowerShell user group and / or a Cincinnati PowerShell Saturday? Post a response below and let me know. (All posts are moderated, so if you don’t want your post public, just say so.)

 

Mike

 

.

Note to spammers!

Spammers, don't waste your time... all posts are moderated. If your comment includes unrelated links, is advertising, or just pure spam, it will never be seen.