syntax error

Glenn Maxwell 10,146 Reputation points
2021-11-08T20:47:04.92+00:00

Hi All

i am using the below syntax. i am connected to server01 and trying to execute the below syntax.
i.e i am connected to server01 and remoting it to server02 and executing.
when i directly run on server02 i dont see any issue but facing issue with remoting. i have also installed dns module on server01
I am using Account01 and when prompted for credentials i am giving it.
i am getting error Failed to get the zone information for mydomain.com mydc01

Windows Server 2019
Windows Server 2019
A Microsoft server operating system that supports enterprise-level management updated to data storage.
3,466 questions
Windows Server 2016
Windows Server 2016
A Microsoft server operating system that supports enterprise-level management updated to data storage.
2,374 questions
Windows DHCP
Windows DHCP
Windows: A family of Microsoft operating systems that run across personal computers, tablets, laptops, phones, internet of things devices, self-contained mixed reality headsets, large collaboration screens, and other devices.DHCP: Dynamic Host Configuration Protocol (DHCP). A communications protocol that lets network administrators manage centrally and automate the assignment of Internet Protocol (IP) addresses in an organization's network.
1,023 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,379 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
19,535 questions
0 comments No comments
{count} votes

Accepted answer
  1. Rich Matheisen 45,011 Reputation points
    2021-11-09T15:13:55.587+00:00

    I think you'll find that you're encountering the "Second Hop" problem. You can't use the credentials passed from local machine to SERVER02 when your Get-DnsServerResourceRecord tries to connect to MYDC01. The cmdlet uses WMI/CIM and it's probably being denied permission. The reason, I'm guessing, is probably an error "5" or "1722".

    # create session from local machine (machine #1) to SERVER02 (machine #2)
    $session = New-PSSession -ComputerName server02 -Credential Account01
    # Run Invoke-Command on SERVER02 (machine #2)
    Invoke-Command -Session $session -ScriptBlock {
        try {
            Write-Host $env:COMPUTERNAME;
            # Try connecting to MYDC01 (machine #3) from Server02 (machine #2)
            $dnsrecords = Get-DnsServerResourceRecord -ZoneName mydomain.com -ComputerName mydc01 -ErrorAction Stop | 
                Where-Object { $_.RecordType -eq "A" -Or $_.RecordType -eq "CNAME" } | 
                    ConvertTo-Json
            Write-Host $dnsrecords
            # Note: $dnsrecords never returned to SERVER02!
        }
        catch {
            $_  # return $Error[0] to Server02
        }
    }
    # remove session with SERVER02 (machine #2)
    Remove-PSSession -session $session
    
    0 comments No comments

6 additional answers

Sort by: Newest
  1. Rachel Gomez 166 Reputation points
    2022-08-23T06:23:04.917+00:00

    Syntax errors are mistakes in the source code, such as spelling and punctuation errors, incorrect labels, and so on, which cause an error message to be generated by the compiler. These appear in a separate error window, with the error type and line number indicated so that it can be corrected in the edit window.If a syntax error appears, check to make sure that the parentheses are matched up correctly. If one end is missing or lined up incorrectly, then type in the correction and check to make sure that the code can be compiled. Keeping the code as organized as possible also helps.

    Regards,
    Rachel Gomez

    0 comments No comments

  2. James Hamil 21,776 Reputation points Microsoft Employee
    2021-12-23T02:18:58.183+00:00

    Hi @Glenn Maxwell , did you see the follow up from Rich? Try using Invoke-Command mydc01 -credential (Get-Credential) -ScriptBlock .

    0 comments No comments

  3. Glenn Maxwell 10,146 Reputation points
    2021-11-09T16:40:06.18+00:00

    i am using domain admin account but still i am getting the below error

    $session = New-PSSession -ComputerName server02 -Credential account1
    Invoke-Command -Session $session -ScriptBlock {
    try {
    Write-Host $env:COMPUTERNAME;
    $dnsrecords = Get-DnsServerResourceRecord -ZoneName mydomain.com -ComputerName mydc01 -ErrorAction Stop | Where-Object { $.RecordType -eq "A" -Or $.RecordType -eq "CNAME" } | ConvertTo-Json
    Write-Host $dnsrecords
    }
    catch {
    $_
    }
    }
    Remove-PSSession -session $session
    server02
    Get-DnsServerResourceRecord : Failed to get the zone information for mydomain.com on server mydc01.
    At line:4 char:15

    • ... nsrecords = Get-DnsServerResourceRecord -ZoneName mydomain.com ...
    • ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    • CategoryInfo : PermissionDenied: (mydomain.com:root/Microsoft/...rResourceRecord) [Get-DnsServerResourceRecord], CimException
    • FullyQualifiedErrorId : WIN32 5,Get-DnsServerResourceRecord

  4. Glenn Maxwell 10,146 Reputation points
    2021-11-09T03:43:58.893+00:00

    please help in editing the syntax

    0 comments No comments