PowerShell: вызов метода завершился неудачно, потому что ... не содержит метода с именем - PullRequest
0 голосов
/ 16 июня 2020

Работа с OOP и PowerShell. У меня есть класс, определенный с помощью кучи методов, что можно увидеть, когда я запускаю Get-Member против него:

enter image description here

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
}

Однако когда я отлаживаю сценарий, он здесь не работает:

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()
        }
    }
}

enter image description here

Что мне не хватает? Я повторно открыл Visual Studio Code, у меня есть оператор using для файла модуля, и когда я импортирую модуль, я использую -Force. Мы будем благодарны за любую помощь.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...