【问题标题】:Power shell class method call it is own method syntax errorPower shell类方法调用它是自己的方法语法错误
【发布时间】: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


【解决方案1】:

调用方法时,与调用函数时不同,参数需要在括号中传递并且是正确的类型。因为你的方法属于类,所以需要使用$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;
    }

【讨论】:

猜你喜欢
  • 2014-01-22
  • 2015-04-21
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-07-13
  • 2019-11-15
  • 2018-03-17
相关资源
最近更新 更多