Power Shell类方法调用它是自己的方法语法错误



我有以下简单类。我试图将其自己的方法称为以下。但是我得到语法错误。

# 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:"哈希"一词不被认为是cmdlet的名称, 功能,脚本文件或可操作程序。检查拼写 名称,或者如果包含D的路径,请验证该路径是否正确并且 再试一次。在 C: tools backup3.ps1:20 char:23 $ hashed = hash $ this.apikey $ this.snapshotpath; ~~~~ categoryInfo:objectNotFound :(哈希:字符串)[],commandnotfoundException 完全QualifiedErrid:CommandnotFoundException

在Incorrigible1的评论后,我更新了代码。更新有关如何将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() ~~~~~~~~~~~~~~并非所有代码路径在方法中返回值。 categoryInfo:parserError:{:) [],parentContainsRorrecordException 完全QualifiedErrid: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;
    }

最新更新