title: PowerShell: Method invocation failed because ... does not contain a method named
body: <p>Working with OOP and PowerShell. I have a class defined with a bunch of methods, as can be seen when I run Get-Member against it:</p>
  
  <p><a href="https://i.stack.imgur.com/SpN8w.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/SpN8w.png" alt="enter image description here"></a></p>
  
  <pre><code>Computer($c, $service){
      $this.Service = $service
      $this.Name = $c
  }
  
  [string] StartService() {
      Set-Service -InputObject $this.ServiceState -Status 'Running'
      $this.ServiceState.WaitForStatus('Running')
      return $this.ServiceState.Status
  }
  
  [string] EnableService() {
      Set-Service -InputObject $this.ServiceState -Status 'Manual'
      $this.ServiceState.WaitForStatus('Manual')
      return $this.ServiceState.Status
  }
  
  [string] StopService() {
      Set-Service -InputObject $this.ServiceState -Status 'Stopped'
      $this.ServiceState.WaitForStatus('Stopped')
      return $this.ServiceState.Status
  }
  
  [string] ServiceState() {
      $this.ServiceState = Get-Service -ComputerName $this.Name -Name $this.Service
      switch ($this.ServiceState.Status) {
          'Disabled' {
              if ($this.EnableService() -eq 'Manual')
              {
                  if ($this.StartService() -eq 'Running')
                  {
                      return $this.ServiceState.Status
                  } else {
                      return "Unable to start the service"
                  }
              } else {
                  return "Unable to enable the service"
              }
          }
          'Stopped' {
                  if ($this.StartService() -eq 'Running') {
                      return $this.ServiceState.Status
                  } else {
                      return "Unable to start the service"
                  }
          }       
          Default {
              return $this.ServiceState.Status
          }
      }
      return $this.ServiceState.Status
  }
  </code></pre>
  
  <p>However, when I debug the script, it fails here:</p>
  
  <pre><code>Using Module "./Computer.psm1" 
  Import-Module "./Computer.psm1" -Force
  $service = 'RemoteRegistry'
  $computers = Get-ADComputer -Filter * -SearchBase "OU=OU HERE,OU=ANOTHER OU,OU=MORE OUs,DC=DOMAIN NAME,DC=com" | Select-Object -ExpandProperty Name
  $obj = @{}
  foreach($computer in $computers)
  {
      $CompObj = [Computer]::New($computer,$service)
  if (Test-Connection -ComputerName $CompObj.Name -Count 1 -ErrorAction SilentlyContinue ){
          if ($CompObj.ServiceState() -eq 'Running') 
          {
              $InstalledSoftwareKey="SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall"
              $InstalledSoftware=[microsoft.win32.registrykey]::OpenRemoteBaseKey('LocalMachine',$computer)
              $RegistryKey=$InstalledSoftware.OpenSubKey($InstalledSoftwareKey) 
              $SubKeys=$RegistryKey.GetSubKeyNames()
              Foreach ($key in $SubKeys){
                  $thisKey=$InstalledSoftwareKey+"\\"+$key
                  $thisSubKey=$InstalledSoftware.OpenSubKey($thisKey)
                  if ($thisSubkey.GetValue("DisplayName") -like "Microsoft Office*")
                  {
                      if ($obj.ContainsKey($thisSubkey.GetValue("DisplayName")))  {
                          $name = $thisSubKey.GetValue("DisplayName")
                          $obj.$name++
                      } else {
                          $obj.Add($thisSubkey.GetValue("DisplayName"), 1)
                      }
                  }
              }
          } else {
              Write-Output "Unable to start the remote registry service on $computer"
          }
          if ($CompObj.ServiceState -eq 'Running') {
              $CompObj.StopService()
          }
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/IYF3U.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/IYF3U.png" alt="enter image description here"></a></p>
  
  <p>What am I missing? I've re-opened Visual Studio Code, I have a using statement for the module file and when I import the module I'm using -Force. Any assistance will be greatly appreciated.</p>