【发布时间】:2017-12-29 20:18:58
【问题描述】:
我有以下简单的课程。我试图调用它自己的方法如下。但我得到语法错误。
# 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 作为我的班级成员:-
Hash : 术语“Hash”未被识别为 cmdlet 的名称, 函数、脚本文件或可运行的程序。检查拼写 名称,或者如果路径包含 d,请验证路径是否正确,并且 再试一次。在 C:\工具\backup3.ps1:20 字符:23 + $hashed = 哈希 $this.apiKey $this.snapshotPath; + ~~~~ + CategoryInfo : ObjectNotFound: (Hash:String) [], CommandNotFoundException + FullyQualifiedErrorId : CommandNotFoundException
在 TheIncorrigible1 发表评论后,我更新了我的代码。更新了关于如何让 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.Hash($this.apiKey, $this.snapshotPath) -
是的,它正在工作。你能告诉我如何让 $httpClient 作为我的班级成员吗?
标签: powershell class syntax-error