• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

Power Shell to VBA code conversion

aks131

New Member
Need help converting this powersheel in VBA code. I am new to VBA , please advise/guide.
Need is to Authenticate cert placed in Windows Cert Store and make rest API Call. Here’s a high-level approach:
  • Open the certificate store
  • Search for the certificate with the specified subject name (“PS Cloud Authentication, O=APISafe, OU=Development, C=Canada”).
  • Retrieve the certificate and store it in a variable.
  • Use the certificate for authentication.


Then make a secure connection to the rest API endpoint
$baseUrl = https://APISafe/api/public/v3/;


#The Application API Key generated in APISafeInsight
$apiKey = "This is where the API key will be once it is given to you.";


#Username of APISafeInsight user granted permission to the API Key
$runAsUser = "TESTAPI";


$clientCertificateType = "BICertificate";

Here is the Power Shell code that I need to convert in VBA code

#Step 1: Authenticate securily to API Server
$certStore = "CurrentUser"; # Alternative: LocalMachine
$subFieldName = "CN";
$issuedTo = "PS Cloud Authentication, O=APISafe, OU=Development, C=Canada";




#Step 2: "Finding client certificate..";




$certs = Get-ChildItem -Path "cert:\${certStore}\My" -EKU "Client Authentication";
$cert = $certs | Where-Object { $_.Subject -eq "${subFieldName}=${issuedTo}" };




#Write-Output $cert
$cert;


[System.Security.Cryptography.X509Certificates.X509Certificate2]$script:authCert = $cert
Psafe-SignAppIn




#Secure Connection
$baseUrl = https://APISafe/api/public/v3/;


#The Application API Key generated in APISafeInsight
$apiKey = "This is where the API key will be once it is given to you.";


#Username of APISafeInsight user granted permission to the API Key
$runAsUser = "TESTAPI";


$clientCertificateType = "BICertificate";
 
Aks, I've been exposed to a little PS but I can't claim to really know it. If you can tell me what the above code does—exactly what it does—I can help coach you to write a VBA program to do something similar.
 
Thanks Bob. I am in a bit urgent need , googled all over not finding any info, please see if you can assist wits this
I am trying to make an API Call from VBA code and for that first step is to access a particular certificate detail from Windows laptop and authenticate.

Step 1:
a : Open Cert Store on the Windows Machine
b : Use below details to locate a particular cert

These are some variables used in powershell to find
$certStore = "CurrentUser"; ‘ where certs are stored for current user running VBA , script picks by itself w/o any hardcoding ’
$subFieldName = "CN" ‘ This will be value that Cert will have , hardcoded value to compare cert’
$issuedTo = "IXTVPN.BIZ, OU=XS, O=RS, L=Engwod, S=Coldo, C=US"; ‘ This will be value that Cert will have , hardcoded value to compare cert’

c : Store this cert details in a variable
 
So you're saying the first action of the PS script is to pull up the details on a certificate? Hm, maybe I can't help you after all; I've no idea how to do that. "Cert store on the Win machine"; that'd be in the registry, right? I see the registry root is described at https://learn.microsoft.com/en-us/w...l-machine-and-current-user-certificate-stores, which oughta give a start.

Ah, look at https://stackoverflow.com/questions/35936735/find-registry-key-using-excel-vba. To read a registry key in VBA, this seems to say you define a Wscript.Shell object and use the RegReadKey method; that sends back information on the key. No, wait, at https://ss64.com/vb/regread.html it says the proper method is RegRead. Take a look at these and see whether you can get that working; then we'll go on to the next step.
 
Back
Top