私は次の簡単なクラスがあります。私は次のように独自のメソッドを呼び出そうとしています。しかし、私は構文エラーを取得します。パワーシェルクラスのメソッドは、それ自身のメソッドの構文エラーを呼び出します
# 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;
}
}
}
パワーシェルスクリプトの新機能です。私はすることで学びます。それで私は正しい構文を知らないかもしれません。どうすればHashとBase64UrlEncodeメソッドを呼び出すことができるか教えてください。現在、次のエラーが表示されます。また、どのように私のクラスのメンバーとして$のHttpClientを持つことができます -
Hash : The term 'Hash' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was include d, verify that the path is correct and try again. At C:\tools\backup3.ps1:20 char:23 + $hashed = Hash $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;
}
}
At C:\tool\backup3.ps1:60 char:38 + [System.Net.Http.Httpclient] GetHttpClient() + ~~~~~~~~~~~~~ Not all code path returns value within method. + CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException + FullyQualifiedErrorId : MethodHasCodePathNotReturn
'$ this.Hash($ this.apiKey、$ this.snapshotPath) ' – PetSerAl
はい、動作しています。クラスメンバーとして$ httpClientをどのように持つことができますか教えてください。 – masiboo