none
Command returning different results from same command RRS feed

  • Question

  • I am trying to run a shutdown remote command. I am weirdly getting the following

    WHAT I EXPECT (and works)

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com" | %{write-host $_.DNSHostName}
    
    RESULT
    COMPUTER01.DOAMIN.com

    WHAT I AM GETTING (Not working)

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com" | %{shutdown /r /f /m \\$_.DNSHostName}
    
    RESULT
    CN=COMPUTER01,OU=Test,OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com.DNSHostName: The entered c
    omputer name is not valid or remote shutdown is not supported on the target computer. Check the name and then try again
    or contact your system administrator.(53)

    The shutdown command DOES work when run directly in the same window at the same time.


    BlankMonkey

    Wednesday, September 21, 2016 4:13 PM

Answers

  • You are still missing some information.

    With Get-AdComputer "Name" and "CN" are the same attribute.  DNSHostName is taken from the DNS attributes in AD as DNS with AD is integrated.

    If you must use DNS name then you have to do this:

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com" |      %{
              shutdown /r /f /m "\\$($_.DNSHostName)"
          }


    \_(ツ)_/

    • Marked as answer by BlankMonkey Wednesday, September 28, 2016 7:54 PM
    Monday, September 26, 2016 8:44 PM
  • A script is a one-liner.

    If you want the logging directly within the script, here is an example:


    $params = @{
      "Filter" = "*"
      "SearchBase" = "OU=Test,OU=Computer Maintenance,OU=## Computers,DC=fabrikam,DC=com"
    }
    $computerNames = Get-ADComputer @params | Select-Object -ExpandProperty Name
    & {
      foreach ( $computerName in $computerNames ) {
        shutdown \\$computerName /r /f /t 0
        "{0:yyyy-MM-dd hh:mm:ss} Shutdown command sent to $computerName" -f (Get-Date)
      }
    } | Tee-Object "C:\Temp\Results.txt"
    

    Of course, this is not as flexible as it has a fixed output file name. (You could make it a parameter.)

    The rest is up to you.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, September 27, 2016 5:54 PM
    Moderator

All replies

  • Do not use Write-Host. It does not output anything - it only displays output in the console.

    Also, you don't need shutdown.exe. You can use the Restart-Computer cmdlet directly.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 21, 2016 5:01 PM
    Moderator
  • I can't use the restart-computer, it does not work.  I get an RPC error.  Probably remote PS tools not installed or something.  I did not chase it because the shutdown command does work.

    The write-host is only to validate the output. You can see from the first example, that the output is as I would expect it to be, and FQDN.  But the same command, piped to shutdown instead is instead getting a distinguished name, or the CN= format.


    BlankMonkey

    Wednesday, September 21, 2016 5:26 PM
  • shutdown /m \\netbiosname /r /f

    Do not use DNS name.


    \_(ツ)_/

    Wednesday, September 21, 2016 6:36 PM

  • $params = @{
      "Filter" = "*"
      "SearchBase" = "OU=Test,OU=Computer Maintenance,OU=## Computers,DC=fabrikam,DC=com"
    }
    Get-ADComputer @params | ForEach-Object {
      Restart-Computer $_.Name -WhatIf
    }
    


    -- Bill Stewart [Bill_Stewart]





    Wednesday, September 21, 2016 6:42 PM
    Moderator
  • I would like that, but NetBIOS is actively disabled on the network.  I tried .Name but the value is truncated so the name of COMPUTER01 is COMPUTER (8 chars), so even the DNS suffix order will not work.

    BlankMonkey

    Wednesday, September 21, 2016 9:15 PM
  • NetBIOS is not really disabled.  It is routed through TCP.  The NetBIOS protocol has not been used since W2K.

    test this

    ping comuter01

    It will work.


    \_(ツ)_/

    Wednesday, September 21, 2016 9:28 PM
  • Whenever a command requires the '\\' in front of the name it will use the NetBIOS name. \\ can be used with FQDN but not all commands will handle it correctly.

    Use "Restart-Computer -force" as it eliminates all of your issues.


    \_(ツ)_/



    • Edited by jrv Wednesday, September 21, 2016 9:31 PM
    Wednesday, September 21, 2016 9:30 PM
  • I would like that, but NetBIOS is actively disabled on the network.  I tried .Name but the value is truncated so the name of COMPUTER01 is COMPUTER (8 chars)

    I don't know what you mean by "NetBIOS is actively disabled". Do you mean you don't use WINS servers?

    In any case the Name property returned from the output of Get-ADComputer is certainly not truncated to 8 characters. I use that property all the time, and I have lots of machines that have names longer than that.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 21, 2016 9:36 PM
    Moderator
  • TY for the replies, I am sorry about the delay, life happens to us all I am sure.

    From the discussion, I think there are some things here for me to learn, and I look forward to your further input.  To simplify, I am seeing 3 different points; NetBIOS resolution, Error code, and Command Results.

    NetBIOS

    So I have been told and shown that NetBIOS is turned off on the network, and this is done by going to

    • NCPA.cpl > IPv4 > Properties > Advanced > WINS (Tab) > "Disable NetBIOS over TCP/IP" (Radio button).

       I have confirmed that this is true, and there is no NetBIOS going on as I understand it.  BROWMON.exe is not showing my computer.  In fact, all the computers and servers on this network are all MS and have had NetBIOS disabled.

       I believe the behavior of doing the \\COMPUTER01 does work, but it is because of this DNS setting (similar to the lmhosts file)

    • NCPA.cpl > IPv4 > Properties > Advanced > DNS (Tab) > "Append there DNS suffixes (in order):"

       I have tested this to be true, as I have an empty DNS and WINS off, and \\COMPUTER01 fails, but if I populate the DNS with the DNS Suffix, \\Computer01 will now work.  This would indicate that the NetBIOS is disabled, and that the \\COMPUTER01 (Not FQDN) is happening due to DNS config.  Am I missing something with NetBIOS???

    ERROR CODE

       I am not using the restart-computer because the command is failing.  The error is "RPC is not available." (more detailed then that but that is the jist of it.  The system is brand new build)  There is no firewall and it is on the same network segment.  I run the command with domain admin rights.  Perhaps I have to turn on the RSAT tools for it to work.  But the simple is that restart-computer does not work, and shutdown does, if I can just get Powershell to send it the correct computer name.

    COMMAND RESULTS

       So the error I am getting is that the name is not being passed as I would expect it to be.  Using the write-host command I am trying to validate I am getting the actual FQDN of the object, as listed in the properties command, but what I am getting is the distinguished name (correct terms??)  The actual properties are shown here;

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=#### Computers,DC=DOMAIN,DC=com"
    
    
    DistinguishedName : CN=COMPUTER01,OU=Test,OU=Computer Maintenance,OU=#### Computers,DC=DOMAIN,DC=com
    DNSHostName       : COMPUTER01.DOMAIN.com
    Enabled           : True
    Name              : COMPUTER      <------THIS IS MISSING THE 01
    ObjectClass       : computer
    ObjectGUID        : ########-2dc1-4158-a303-############
    SamAccountName    : COMPUTER01$
    SID               : S-1-5-21-50792---------------------------
    UserPrincipalName :
    So you can understand that I am confused why the write-host command returns the correct value, but the shutdown returns something different.

     


    BlankMonkey

    Monday, September 26, 2016 8:19 PM

  • $params = @{
      "Filter" = "*"
      "SearchBase" = "OU=Test,OU=Computer Maintenance,OU=## Computers,DC=fabrikam,DC=com"
    }
    Get-ADComputer @params | ForEach-Object {
      shutdown \\$($_.Name) /r /f /t 0
    }
    


    -- Bill Stewart [Bill_Stewart]


    Monday, September 26, 2016 8:40 PM
    Moderator
  • You are still missing some information.

    With Get-AdComputer "Name" and "CN" are the same attribute.  DNSHostName is taken from the DNS attributes in AD as DNS with AD is integrated.

    If you must use DNS name then you have to do this:

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com" |      %{
              shutdown /r /f /m "\\$($_.DNSHostName)"
          }


    \_(ツ)_/

    • Marked as answer by BlankMonkey Wednesday, September 28, 2016 7:54 PM
    Monday, September 26, 2016 8:44 PM
  • This failed, the shutdown command was not recognizing the switches or something, the error was not clear, only returning the help for the shutdown command.  Still, I suspect it is the .Name that is not recognized, as it is being truncated (see above).

    BlankMonkey

    Tuesday, September 27, 2016 2:25 PM
  • VICTORY!

    Can you please tell me why this worked???  What is the extra $ doing?? Why without it did it return the DistinguishedName formatting?


    BlankMonkey

    Tuesday, September 27, 2016 2:27 PM
  • So, one final part to this.  Being a good sys admin I want to write a simple log of what has happened, but I am having one final problem, why is this not working?

    Get-ADComputer -Filter * -SearchBase "OU=Test, OU=Computer Maintenance,OU=## Computers,DC=DOMAIN,DC=com" |      %{
              shutdown /r /f /m "\\$($_.DNSHostName)"
          } |$a=get-date ; write-output "$a , $($_.DNSHostName) , reboot sent" >>e:\download\results.txt


    BlankMonkey

    Tuesday, September 27, 2016 3:04 PM
  • OK. Let's simplify a bit:


    $params = @{
      "Filter" = "*"
      "SearchBase" = "OU=Test,OU=Computer Maintenance,OU=## Computers,DC=fabrikam,DC=com"
    }
    $computerNames = Get-ADComputer @params | Select-Object -ExpandProperty Name
    foreach ( $computerName in $computerNames ) {
      shutdown \\$computerName /r /f /t 0
      "Shutdown command sent to $computerName"
    }
    

    Put the above code in a .ps1 (script) file, e.g., "C:\Scripts\RebootServers.ps1". Then you can run the script and redirect its output to a file; e.g.:


    PS C:\> C:\Scripts\RebootServers.ps1 | Out-File C:\Scripts\RebootLog.log
    


    -- Bill Stewart [Bill_Stewart]

    Tuesday, September 27, 2016 3:36 PM
    Moderator
  • TY :)

    I am trying to get it down to a one liner because I have to push it into a cron program from a coworker, but I can probably push the script as well, just a little more work.  But there is the problem that when the script (shutdown command) works, it doesn't return anything, so I have to time stamp it and add a name that it ran


    BlankMonkey

    Tuesday, September 27, 2016 4:38 PM
  • A script is a one-liner.

    If you want the logging directly within the script, here is an example:


    $params = @{
      "Filter" = "*"
      "SearchBase" = "OU=Test,OU=Computer Maintenance,OU=## Computers,DC=fabrikam,DC=com"
    }
    $computerNames = Get-ADComputer @params | Select-Object -ExpandProperty Name
    & {
      foreach ( $computerName in $computerNames ) {
        shutdown \\$computerName /r /f /t 0
        "{0:yyyy-MM-dd hh:mm:ss} Shutdown command sent to $computerName" -f (Get-Date)
      }
    } | Tee-Object "C:\Temp\Results.txt"
    

    Of course, this is not as flexible as it has a fixed output file name. (You could make it a parameter.)

    The rest is up to you.


    -- Bill Stewart [Bill_Stewart]


    Tuesday, September 27, 2016 5:54 PM
    Moderator
  • Any more thoughts on this?


    BlankMonkey

    Wednesday, September 28, 2016 5:04 PM
  • Any more thoughts on what?

    My previous response contains a complete script you can use.

    You can run a script with a PowerShell command line:

    powershell.exe -File C:\Scripts\MyFile.ps1

    That's a one-line command that runs a script.


    -- Bill Stewart [Bill_Stewart]

    Wednesday, September 28, 2016 7:34 PM
    Moderator
  • TY, I may use this, I was referring to the $($_ syntax question.  I know it works, but why, and why did mine not?

    "

    VICTORY!

    Can you please tell me why this worked???  What is the extra $ doing?? Why without it did it return the DistinguishedName formatting?

    "


    BlankMonkey

    Wednesday, September 28, 2016 7:53 PM