我有以下简单的课程。我正在尝试将自己的方法称为以下方法。但是我得到了语法错误。
# Add the necessary .NET assembly
Add-Type -AssemblyName System.Net.Http
# Create the HttpClient client
# I wanted to have it as a class member. But I get error AssemblyName not found.
$httpClient = New-Object -TypeName System.Net.Http.Httpclient;
class myClass
{
[Byte] Hash([String]$apiKey, [String]$path)
{
$hmacsha = New-Object System.Security.Cryptography.HMACSHA512;
$hmacsha.key = $apiKey;
$hashed = $hmacsha.ComputeHash([system.Text.Encoding]::UTF8.GetBytes($path));
return $hashed;
}
[Byte] Base64UrlEncode($data)
{
$encoded = [System.Convert]::ToBase64String($data);
$encoded = $encoded.Split('-')[0];
$encoded = $encoded.Replace('+', '-');
$encoded = $encoded.Replace('*', '_');
return $encoded;
}
setupHttpClient()
{
# Create the HttpClient client
#$this.httpClient = New-Object -TypeName System.Net.Http.Httpclient;
if($global:httpClient)
{
# Set base address
$global:httpClient.BaseAddress = $this.baseAddress;
# Hash data
$hashed = Hash $this.apiKey $this.snapshotPath; # syntax error
# Encode data
$encoded = Base64UrlEncode $hashed; # syntax error
# Setup HttpClient client for the secure call
$this.httpClient.DefaultRequestHeaders.Authorization = New-Object -TypeName System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $encoded);
$this.httpClient;
}
}
}
我是Power shell脚本的新手。我正在边做边学。这就是为什么我可能不知道正确的语法。请让我知道如何调用Hash和Base64UrlEncode方法。目前,我收到以下错误。另外,如何将$ httpClient作为我的班级成员: -
哈希:术语“哈希”不被识别为cmdlet,函数,脚本文件或可操作程序的名称。检查名称的拼写,或者如果路径包含d,请验证路径是否正确,然后重试。在C:\ tools \ backup3.ps1:20 char:23 + $ hashed = Hash $ this.apiKey $ this.snapshotPath; + ~~~~ + CategoryInfo:ObjectNotFound :( Hash:String)[],CommandNotFoundException + FullyQualifiedErrorId:CommandNotFoundException
在The Incorrigible 1的评论之后,我更新了我的代码。更新了有关如何将httpClient作为成员并返回它的问题:
class DataBackup
{
[System.Net.Http.Httpclient]$httpClient = $null;
DataBackup()
{
$this.httpClient = New-Object -TypeName System.Net.Http.Httpclient;
}
[System.Net.Http.Httpclient] GetHttpClient() # I got systax error here
{
# Create the HttpClient client
#$this.httpClient = New-Object -TypeName System.Net.Http.Httpclient;
if($this.httpClient)
{
# Set base address
$this.httpClient.BaseAddress = $this.baseAddress;
# Hash data
$hashed = $this.Hash($this.apiKey, $this.snapshotPath);
#$hashed = Hash $this.apiKey $this.snapshotPath;
# Encode data
$encoded = $this.Base64UrlEncode($hashed);
# Setup HttpClient client for the secure call
$this.httpClient.DefaultRequestHeaders.Authorization = New-Object -TypeName System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $encoded);
$this.httpClient;
}
}
在C:\ tool \ backup3.ps1:60 char:38 + [System.Net.Http.Httpclient] GetHttpClient()+ ~~~~~~~~~~~~~并非所有代码路径都返回方法中的值。 + CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException + FullyQualifiedErrorId:MethodHasCodePathNotReturn
调用方法时,与调用函数时不同,参数需要在括号中传递并且是正确的类型。因为您的方法属于该类,所以您需要使用$this
自动变量。
$this.Hash($this.apiKey, $this.snapshotPath)
如果写成一个函数:
Get-Hash $apiKey $snapshotPath
你编写以下内容的方式,我不确定你为什么要使用类和函数。然而,这是导致错误的原因。
if($global:httpClient)
{
# Set base address
$global:httpClient.BaseAddress = $this.baseAddress;
# Hash data
$hashed = Hash $this.apiKey $this.snapshotPath; # syntax error
# Encode data
$encoded = Base64UrlEncode $hashed; # syntax error
# Setup HttpClient client for the secure call
$this.httpClient.DefaultRequestHeaders.Authorization = New-Object -TypeName System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", $encoded);
$this.httpClient;
}