vmware / script-runtime-service-for-vsphere

The Repository contains Script Runtime Service for vSphere. A service for managing server-side PowerCLI instances to run commands and scripts against VCenter servers.
Other
35 stars 7 forks source link

[Feature Request] Create a PS module for using SRS #53

Open lucdekens opened 3 years ago

lucdekens commented 3 years ago

To facilitate the use of SRS for a casual PowerCLI/PowerShell user, it might be handy to have a PowerShell module that encapsulates the REST API calls. This avoids that the user has to do the tedious Invoke-RestMethod calls all the time.

PS: if you are looking for volunteers to start such a module, you can count me in.

dmilov commented 3 years ago

Good one. We can create an SRS Client PS Module and update the API pester tests to use it. I'll prepare the scaffold, feel free to put all the content there.

dmilov commented 3 years ago

Added the module manifest

https://github.com/vmware/script-runtime-service-for-vsphere/tree/master/client-sdk/powershell

@lucdekens Feel free to add content in there. https://github.com/vmware/script-runtime-service-for-vsphere/blob/master/test/api-integration-tests/tests/SRSAPIHelpers.ps1 could help.

lucdekens commented 3 years ago

Thanks for the pointer to SRSAPIHelpers.ps1.

Question: shall the module support PSv5.1?

If yes, we have to foresee an alternative for the SkipCertificateCheck switch when invoked from PSv5.1. Something similar to what I did in my Invoke-SrsMethod function in my A Hitchhikers Guide To SRS 1.0.0

dmilov commented 3 years ago

Let's start with PS 7 initially, later we can add support for 5.1

dmilov commented 3 years ago

As a SkipCertificateCheck solution for PS 5.1 I can suggest a wrapper function like

function Invoke-RestMethodEx {
param(
# InvokeRestMethodParams,

[switch]
$SkipCertificateCheck

)

    if (-not("SkipCertificateCheck" -as [type])) {
        Add-Type -TypeDefinition `
                ("using System;" + `
                "using System.Net;" + `
                "using System.Net.Security;" + `
                "using System.Security.Cryptography.X509Certificates;" + `
                "public static class SkipCertificateCheck {" + `
                    "public static bool ReturnTrue(object sender," + `
                        "X509Certificate certificate," + `
                        "X509Chain chain," + `
                        "SslPolicyErrors sslPolicyErrors) { return true; }" + `
                    "public static RemoteCertificateValidationCallback GetDelegate() {" + `
                        "return new RemoteCertificateValidationCallback(SkipCertificateCheck.ReturnTrue);" + `
                    "}" + `
                "}")
    }

    if ($SkipCertificateCheck) {
        [System.Net.ServicePointManager]::ServerCertificateValidationCallback += [SkipCertificateCheck]::GetDelegate()
    }

    Invoke-RestMethod https://10.23.82.191/api/about

   if ($SkipCertificateCheck) {
     # back to previous value, for the sake of example I assume it was $null
       [System.Net.ServicePointManager]::ServerCertificateValidationCallback = $null
   }

}