Trying to get Certificate Information in Powershell - powershell

I recently posted some code here : Powershell hanging in script
And someone recommended using Invoke-WebRequest instead of the way I was getting the cert information to make it clean and short.
I was able to get the Invoke-WebRequest to work but can't figure out how to get the cert information.
I found this link : Can Powershell Give Me Information on the Server's Certificate Used By Invoke-WebRequest?
But this does not seem to be working for me. What am I doing wrong?
$url = "https://google.com"
$req = Invoke-WebRequest -Uri $url -Proxy $proxy -ProxyUseDefaultCredentials
$servicepoint = [System.Net.ServicePointManager]::FindServicePoint($url)
$servicepoint.Certificate.GetExpirationDateString()
$servicepoint.Certificate.Subject
$servicepoint.Certificate.Issuer
You cannot call a method on a null-valued expression.
At line:5 char:1
+ $servicepoint.Certificate.GetExpirationDateString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull
I also tried to do it exactly as the test and not use the $url variable and instead type it in, and received the same error.
$req = Invoke-WebRequest -Uri https://google.com -Proxy $proxy -ProxyUseDefaultCredentials
$servicepoint = [System.Net.ServicePointManager]::FindServicePoint("https://google.com")
$servicepoint.Certificate.GetExpirationDateString()
$servicepoint.Certificate.Subject
$servicepoint.Certificate.Issuer
You cannot call a method on a null-valued expression.
At line:4 char:1
+ $servicepoint.Certificate.GetExpirationDateString()
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : InvokeMethodOnNull

the error says that line 4 is null.
i am doing the same .
for subject \ cert name i am calling like so.
$certName = $servicePoint.Certificate.GetName()
i hope it helps.

Related

Invoke web request throwing an error : Invoke-WebRequest : {"errors":[]}

I am using a powershell script to get details from a website via invoke-webrequest. By using the credentials i have connecting to the website using invoke-restmethod to get the token.
After getting the token i used the token to retrieve the details that i need by using invoke-webrequest. But then it is throwing the error this way.The URL is workingfine.
"Invoke-WebRequest : {"errors":[]}"
How can i get to know what is the problem with this request.
API Query:
Invoke-WebRequest -Headers #{"X-Token" = "$token"} -Method Get -Uri "$URL"
error message:
Invoke-WebRequest : {"errors":[]} At C:\temp\RTPA\rtpa_vault3.ps1:24 char:24
+ ... ltdetails = Invoke-WebRequest -Headers #{"X-Token" = "$tok ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

Basic Authentication for Artifactory from PowerShell is throwing 403

I am trying to authenticate to JFROG from powershell i tried it with my token and its giving 403 forbidden
PS C:\Myproject> $myHeaders = #{'X-JFrog-Art-Api' = 'AKCp8sdFTEKF1Y5MDgM3M8RK6bRkKWoX43jWranZvS2U2DE82KFE7243F'}
PS C:\Myproject> Invoke-WebRequest -Uri "https://org.jfrog.io/" -Method Get -Headers $myHeaders
Invoke-WebRequest : 403 Forbidden
nginx
At line:1 char:1
+ Invoke-WebRequest -Uri "https://fiprod.jfrog.io/" -Method Get -Header ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Any idea on how to authenticate to Artifactory via a token using PowerShell ?

Can't Run Skype Commands when authenticating programatically on Powershell

I am running into an strange issue building a Powershell script. I want to programatically connecto to Microsoft Teams and execute some skype for businesss online commands.
When I simply do on a terminal
Connect-MicrosoftTeam and get prompted and import my credentials, it all works perfectly, but i need to this programatically.
I did an app registration with the following permissions
And I am authenticating against Azure AD and Graph:
#Connect to Graph
$Body = #{
Grant_Type = "client_credentials"
Scope = "https://graph.microsoft.com/.default"
client_Id = $ApplicationID
Client_Secret = $AccessSecret
}
$ConnectGraph = Invoke-RestMethod -Uri "https://login.microsoftonline.com/$TenatDomainName/oauth2/v2.0/token" -Method POST -Body $Body
$token = $ConnectGraph.access_token
#Connect to AD
$uri = "https://login.microsoftonline.com/${tenant_id}/oauth2/token"
$body = #{grant_type='refresh_token';resource='https://graph.windows.net';client_id=$clientId;refresh_token=$refresh_token}
$result = Invoke-RestMethod -Method Post -Uri $uri -Body $body
$accessToken = $result.access_token
Then I run
Connect-MicrosoftTeams -AadAccessToken $accessToken -MsAccessToken $token -AccountId $account
And it doesn't throw an error, works properly. and i gen run commands like Get-Team and returns the info correctly. However when I try to run an skype command like
Get-CsOnlineVoiceUser
I get the error
Get-CsOnlineSession : Run Connect-MicrosoftTeams before running cmdlets.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:63 char:22
+ $remoteSession = & (Get-CsOnlineSessionCommand)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Get-CsOnlineSession], UnauthorizedAccessException
+ FullyQualifiedErrorId : UnauthorizedAccessException,Microsoft.Teams.ConfigApi.Cmdlets.GetCsOnlineSession
Invoke-Command : Cannot validate argument on parameter 'Session'. The argument is null or empty. Provide an argument that is not null or empty, and then try the command again.
At C:\Program Files\WindowsPowerShell\Modules\MicrosoftTeams\2.3.1\net472\SfBORemotePowershellModule.psm1:10006 char:38
+ ... -Session (Get-PSImplicitRemotingSession -CommandName 'Get-CsOnline ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidData: (:) [Invoke-Command], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.PowerShell.Commands.InvokeCommandCommand
Am I missing something? I am using the 2.3.1 Teams Module.
Thanks!
This is a bit tricky to do, but can be achieved.
I recommend reading this blog post for exact instructions and code examples:
https://paulobrien.co.nz/2020/06/29/s4b-online-powershell-modern-auth/
Should get you started.

PowerShell Invoke-RestMethod throws WebCmdletWebResponseException

I am trying the following command
Invoke-RestMethod -Uri 'http://serverIP/my/application/URL' -Method 'GET' -WebSession $CurrentSession
I tried it with Invoke-WebRequest as well But I am getting this error for both of them
Invoke-WebRequest : An unexpected error has occurred while attempting to serialize a Date datatype.
At line:1 char:1
+ Invoke-WebRequest -Uri 'http://serverIP/my/application/UR ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
I am quite new at using the Invoke-RestMethod
Can someone please help me with this error?

Error: System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand

While going through https://github.com/Azure/azure-iot-remote-monitoring at Local deployment and while debugging got an error:
Error:
At G:\Azure-IoT\2.Customize a preconfigured solution\Trail_1\azure-iot-remote-monitoring\Common\Deployment\DeploymentLib.ps1:704 char:23
+ $result = Invoke-RestMethod -Method "GET" -Uri $uri -Headers #{"Auth ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-RestMethod], ArgumentException
+ FullyQualifiedErrorId : System.ArgumentException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand
I am using Azure power shell 1.6.0 Version.
Can anyone tell me what's going wrong ?