2017-12-29 48 views
1

私は次の簡単なクラスがあります。私は次のように独自のメソッドを呼び出そうとしています。しかし、私は構文エラーを取得します。パワーシェルクラスのメソッドは、それ自身のメソッドの構文エラーを呼び出します

# 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

+0

'$ this.Hash($ this.apiKey、$ this.snapshotPath) ' – PetSerAl

+0

はい、動作しています。クラスメンバーとして$ httpClientをどのように持つことができますか教えてください。 – masiboo

答えて

0

を、あなたは関数を呼び出すときとは違って、引数は括弧内に渡される必要があり、正しい種類のこと。メソッドがクラスに属しているため、$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; 
    } 
+0

はい、動作しています。クラスメンバーとして$ httpClientをどのように持つことができますか教えてください。 – masiboo

+0

@masiboo変数をメソッド外に宣言するだけです。 – TheIncorrigible1

関連する問題