Ich habe die folgende einfache Klasse. Ich versuche, seine eigene Methode wie folgt zu nennen. Aber ich bekomme den Syntaxfehler.Power Shell-Klasse-Methode Aufruf es ist eigene Methode Syntaxfehler
# 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;
}
}
}
Ich bin neu zu Power Shell-Skript. Ich lerne gerade. Deshalb weiß ich vielleicht nicht die richtige Syntax. Bitte lassen Sie mich wissen, wie kann ich Hash und Base64UrlEncode Methode aufrufen. Derzeit erhalte ich den folgenden Fehler. Auch, wie kann $ Httpclient als mein Klassenmitglied hat: -
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
Nach TheIncorrigible1 Kommentar, ich meinen Code aktualisiert. Aktualisiert Frage, wie als Mitglied haben Httpclient und senden Sie es:
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
Ja, es funktioniert. Können Sie mir sagen, wie ich $ httpClient als mein Klassenmitglied haben kann? – masiboo