<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: credentials with powershell in ONTAP Rest API Discussions</title>
    <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169229#M230</link>
    <description>&lt;P&gt;I'm new to PS and REST so this old post is still relevant to me. I've used a synthesis of Matt's code and combined it with how I wanted to authenticate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$Cluster = "xxx.xxx.xxx.xxx"
$CredentialPath = "C:\Users\Username\Documents\PSCredentials\rest.ps1.credential"
$Credential = Import-Clixml -Path $CredentialPath
$Auth    = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName + ':' + $Credential.GetNetworkCredential().Password))
$Headers = @{"Authorization" = "Basic $auth"}
[String]$Uri = "https://$Cluster/api/storage/volumes?fields=**&amp;amp;return_records=true&amp;amp;return_timeout=15"
$Response = Invoke-RestMethod -method GET -uri $Uri -header $Headers -ErrorAction Stop&lt;/LI-CODE&gt;&lt;P&gt;However I receive the following error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS
secure channel.
At line:1 char:13
+ $Response = Invoke-RestMethod -method GET -uri $Uri -header $Headers  ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand&lt;/LI-CODE&gt;&lt;P&gt;Any thoughts please? If I run the following first, running the invoke command works:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Connect-NcController -Name $Cluster -Credential $Credential&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Mon, 16 Aug 2021 13:22:29 GMT</pubDate>
    <dc:creator>tyrone_owen_1</dc:creator>
    <dc:date>2021-08-16T13:22:29Z</dc:date>
    <item>
      <title>credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156757#M45</link>
      <description>&lt;P&gt;Hello all,&lt;/P&gt;
&lt;P&gt;I want to script REST API using powershell. How do I get the credentials (token) and pass it to the commands?&lt;/P&gt;
&lt;P&gt;something similar to this:&lt;/P&gt;
&lt;P&gt;[the below doesn't work, but I think I am close]&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;$user = Read-Host “Enter username”
$pass = Read-Host -assecurestring “Enter password” | ConvertTo-SecureString -asPlainText -Force

$credential = New-Object System.Management.Automation.PSCredential($user,$pass)
$token = Invoke-WebRequest -Credential $credential -Method Post -Uri "https://192.168.0.52/XXXXXXXXXXXXXX"


Invoke-RestMethod -method GET -uri '"https://192.168.0.52/api/storage/volumes?name=vol1&amp;amp;fields=*&amp;amp;return_records=true&amp;amp;return_timeout=15" ' -header @{"accept" = "application/json"; "authorization" = "Basic $token"}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;any input would be appreciated&lt;/P&gt;</description>
      <pubDate>Wed, 04 Jun 2025 11:06:48 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156757#M45</guid>
      <dc:creator>kahuna</dc:creator>
      <dc:date>2025-06-04T11:06:48Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156786#M47</link>
      <description>&lt;P&gt;It's "quirky" that's for sure - but I've generally handled it this way and seems to work across PowerShell versions.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&lt;FONT size="2"&gt;&lt;EM&gt;...this example is with the ONTAP Select Deploy Utility but should work with ONTAP I would think...&lt;/EM&gt;&lt;/FONT&gt;&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;Function ConvertTo-PlainText( [security.securestring]$secure ) 
{
	$marshal = [Runtime.InteropServices.Marshal] 
	$marshal::PtrToStringAuto( $marshal::SecureStringToBSTR($secure) ) 
}
Write-Host " Enter Deploy Password: " -NoNewline
$pwd = Read-Host -AsSecureString
$deploy_password = ConvertTo-PlainText $pwd
$pw = ConvertTo-SecureString -String "$deploy_password" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "admin", $pw
$url = "https://{ip}/api/v3/clusters?name=cluster1"
Invoke-RestMethod -Method 'GET' -Uri $url -Credential $cred -SkipCertificateCheck&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I get inconsistent behavior if I try to send the secure password directly to the New-Object call - you'd think they would be the same but perhaps not (?!?).&amp;nbsp;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;The "-SkipCertificateCheck" switch is awesome btw - too bad it's only in PowerShell version 6/7&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":beaming_face_with_smiling_eyes:"&gt;😁&lt;/span&gt;&lt;/P&gt;</description>
      <pubDate>Mon, 08 Jun 2020 20:09:05 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156786#M47</guid>
      <dc:creator>JohnChampion</dc:creator>
      <dc:date>2020-06-08T20:09:05Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156987#M56</link>
      <description>&lt;P&gt;After some testing I realized this actually didn't work correctly in PowerShell Core 7.0 (oops!)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Here's the new code (note the previous "marshal" function is not used and the change to the Read-Host cmdlet):&lt;/P&gt;
&lt;LI-CODE lang="markup"&gt;$pwd = Read-Host " Enter Deploy Password" -AsSecureString
$deploy_password = ConvertFrom-SecureString $pwd -AsPlainText
$pw = ConvertTo-SecureString -String "$deploy_password" -AsPlainText -Force
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList "admin", $pw
$url = "https://{ip}/api/v3/clusters?name=cluster1"
$result = Invoke-RestMethod -Method 'GET' -Uri $url -Credential $cred -SkipCertificateCheck
&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;I still couldn't pass the secure password directly (?!?)&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 02:57:49 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156987#M56</guid>
      <dc:creator>JohnChampion</dc:creator>
      <dc:date>2020-06-15T02:57:49Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156988#M57</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;You can pass a [&lt;SPAN class="pl-k"&gt;System.Management.Automation.PSCredential] object to your script or function. IE prompt for credentials, then within your code use the credential object to create the header for authentication.&amp;nbsp; Here is a very basic example based on your code to query a volume on a cluster using a credential object.&lt;/SPAN&gt;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;Param(
  [Parameter(Mandatory = $True, HelpMessage = "The Cluster Name or IP Address")]
  [ValidateNotNullOrEmpty()]
  [String]$Cluster,
  [Parameter(Mandatory = $True, HelpMessage = "The Cluster Name or IP Address")]
  [ValidateNotNullOrEmpty()]
  [String]$VolumeName,
  [Parameter(Mandatory = $True, HelpMessage = "The Credential to authenticate to AIQUM")]
  [ValidateNotNullOrEmpty()]
  [System.Management.Automation.PSCredential]$Credential
)
#'------------------------------------------------------------------------------
#'Set the authentication header.
#'------------------------------------------------------------------------------
$auth    = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName + ':' + $Credential.GetNetworkCredential().Password))
$headers = @{"Authorization" = "Basic $auth"}
#'------------------------------------------------------------------------------
#'Enumerate the Volume.
#'------------------------------------------------------------------------------
[String]$uri = "https://$Cluster/api/storage/volumes?name=$VolumeName&amp;amp;fields=*&amp;amp;return_records=true&amp;amp;return_timeout=15"
Try{
  $response = Invoke-RestMethod -method GET -uri $uri -header $headers -ErrorAction Stop
  Write-Host "Enumerated volume ""$VolumeName"" on cluster ""$Cluster"" using URI ""$uri"""
}Catch{
  Write-Warning -Message $("Failed enumerating volume ""$VolumeName"" on cluster ""$Cluster"" using URI ""$uri"". Error " + $_.Exception.Message + ". Status Code " + $_.Exception.Response.StatusCode.value__)
  Break;
}
$response.records
#'------------------------------------------------------------------------------&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;EG if you save that as "GetVolume.ps1" then prompt for a credential object using:&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;PS C:\&amp;gt; $credential = Get-Credential&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Then run the script as follows (IE you can pass the cluster name, volume and credential object as input parameters to your script (or function in library). This way you can avoid having to hard code usernames or passwords in code.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;LI-CODE lang="csharp"&gt;PS C:\Scripts\PowerShell\Projects\ONTAP&amp;gt; .\GetVolume.ps1 -Cluster cluster2.demo.netapp.com -VolumeName volume_001 -Credential $credential
Enumerated volume "volume_001" on cluster "cluster2.demo.netapp.com" using URI "https://cluster2.demo.netapp.com/api/storage/volumes?name=volume_001&amp;amp;fields=*&amp;amp;return_records=true&amp;amp;return_timeout=15"


uuid            : 9838bd47-ab86-11ea-85a7-005056b01307
comment         :
create_time     : 2020-06-11T01:55:15+00:00
language        : c.utf_8
name            : volume_001
size            : 1161850880
state           : online
style           : flexvol
tiering         : @{policy=none}
type            : rw
aggregates      : {@{name=aggr1_cluster2_01; uuid=e329b91b-0633-4186-8c8d-aa4c7191da16}}
clone           : @{is_flexclone=False}
nas             : @{export_policy=}
snapshot_policy : @{name=default}
svm             : @{name=svm21; uuid=503ac2b8-acfe-11e9-8271-005056b03109; _links=}
space           : @{size=1161850880; available=21479424; used=1082281984}
snapmirror      : @{is_protected=False}
metric          : @{timestamp=2020-06-15T04:26:15Z; duration=PT15S; status=ok; latency=; iops=; throughput=}
_links          : @{self=}&lt;/LI-CODE&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;If you don't want to be prompted for credentials you could make the credential parameter non-mandatory and checking for cached credentials using the PSTK (Get-NcCredential) so you don't have to pass the credential parameter if they have already been cached.&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Loads of examples of using the $Credential parameter as an input into a function in the module libraries i've uploaded to github for&amp;nbsp;&lt;A title="AIQUM" href="https://github.com/matthew-beattie/AIQUM/blob/master/AIQUM.psm1" target="_blank" rel="noopener"&gt;AIQUM&lt;/A&gt; and&amp;nbsp;&lt;A title="VSC" href="https://github.com/matthew-beattie/VSC/blob/master/VSC.psm1" target="_blank" rel="noopener"&gt;VSC&lt;/A&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;Hope that helps. Let me know if you have any questions&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;/Matt&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 15 Jun 2020 04:40:33 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/156988#M57</guid>
      <dc:creator>mbeattie</dc:creator>
      <dc:date>2020-06-15T04:40:33Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169229#M230</link>
      <description>&lt;P&gt;I'm new to PS and REST so this old post is still relevant to me. I've used a synthesis of Matt's code and combined it with how I wanted to authenticate:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$Cluster = "xxx.xxx.xxx.xxx"
$CredentialPath = "C:\Users\Username\Documents\PSCredentials\rest.ps1.credential"
$Credential = Import-Clixml -Path $CredentialPath
$Auth    = [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($Credential.UserName + ':' + $Credential.GetNetworkCredential().Password))
$Headers = @{"Authorization" = "Basic $auth"}
[String]$Uri = "https://$Cluster/api/storage/volumes?fields=**&amp;amp;return_records=true&amp;amp;return_timeout=15"
$Response = Invoke-RestMethod -method GET -uri $Uri -header $Headers -ErrorAction Stop&lt;/LI-CODE&gt;&lt;P&gt;However I receive the following error:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Invoke-RestMethod : The underlying connection was closed: Could not establish trust relationship for the SSL/TLS
secure channel.
At line:1 char:13
+ $Response = Invoke-RestMethod -method GET -uri $Uri -header $Headers  ...
+             ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-RestMethod], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeRestMethodCommand&lt;/LI-CODE&gt;&lt;P&gt;Any thoughts please? If I run the following first, running the invoke command works:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;Connect-NcController -Name $Cluster -Credential $Credential&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 13:22:29 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169229#M230</guid>
      <dc:creator>tyrone_owen_1</dc:creator>
      <dc:date>2021-08-16T13:22:29Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169232#M231</link>
      <description>&lt;P&gt;That error is related to the&amp;nbsp; ONTAP certificate and the Invoke-RestMethod requires a signed cert.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you're using PowerShell v7 you can add '-SkipCertificateCheck' to the end of the cmdlet and it will allow the self-signed cert from ONTAP for the https connection.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;$Response = Invoke-RestMethod -method GET -uri $Uri -header $Headers -ErrorAction Stop -SkipCertificateCheck&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you're using PowerShell v5 it's a bit trickier.&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;# Allow untrusted (self signed) certificate
Add-Type @"
    using System.Net;
    using System.Security.Cryptography.X509Certificates;
    
    public class IDontCarePolicy : ICertificatePolicy {
        public IDontCarePolicy() {}
        public bool CheckValidationResult(
            ServicePoint sPoint, X509Certificate cert,
            WebRequest wRequest, int certProb) {
            return true;
        }
    }
"@
[System.Net.ServicePointManager]::CertificatePolicy = New-Object IDontCarePolicy&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;This is only applied to the current session/console.&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 15:01:12 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169232#M231</guid>
      <dc:creator>JohnChampion</dc:creator>
      <dc:date>2021-08-16T15:01:12Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169233#M232</link>
      <description>&lt;P&gt;Thanks John. I'm currently using PS v5.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Would you mind giving me a brief explainer of what is happening in the v5 resolution please? The v7 resolution, even I can understand&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 15:06:28 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169233#M232</guid>
      <dc:creator>tyrone_owen_1</dc:creator>
      <dc:date>2021-08-16T15:06:28Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169236#M233</link>
      <description>&lt;P&gt;It's basically telling that specific console session to trust a non-CA issued certificate (ie. self-signed).&amp;nbsp; It's a common workaround for PSv5 - give it a "google" and I'm sure you'll find a more in-depth answer than mine.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Also, I'm fairly certain it's actually -Headers (with an 's').&amp;nbsp; Maybe that's the issue.&amp;nbsp;&lt;span class="lia-unicode-emoji" title=":thinking_face:"&gt;🤔&lt;/span&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 15:34:20 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169236#M233</guid>
      <dc:creator>JohnChampion</dc:creator>
      <dc:date>2021-08-16T15:34:20Z</dc:date>
    </item>
    <item>
      <title>Re: credentials with powershell</title>
      <link>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169238#M234</link>
      <description>&lt;P&gt;Regarding the 's', good spot - though it was the v5 solution that worked - thank you&lt;/P&gt;</description>
      <pubDate>Mon, 16 Aug 2021 15:39:49 GMT</pubDate>
      <guid>https://community.netapp.com/t5/ONTAP-Rest-API-Discussions/credentials-with-powershell/m-p/169238#M234</guid>
      <dc:creator>tyrone_owen_1</dc:creator>
      <dc:date>2021-08-16T15:39:49Z</dc:date>
    </item>
  </channel>
</rss>

