Using Invoke-Command to get Website Info

This topic contains 15 replies, has 3 voices, and was last updated by Profile photo of Derek Derek 4 months ago.

  • Author
    Posts
  • #62310
    Profile photo of Derek
    Derek
    Participant

    Invoke-Command -ComputerName SERVERNAME -ScriptBlock {Import-Module 'WebAdministration'; Get-ChildItem -Path IIS:\Sites}

    When I use this command on all Win2012 R2 servers and 90% of Win2008 R2 I get information back from remote servers. But on a couple of Win2008 R2 I don't get an error I just get the PS> prompt with no info. What needs to be turned on these Win2008 R2 to get the information. Enable-Remoting -Force is turned on. I did a WinRM Quickconfig but nothing. I've compared the Win2008 R2 servers that work with the ones that don't work and can't seem to find the difference.

    Thanks

  • #62311
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well... we need to maybe troubleshoot. You've run a lot of stuff but I'm not sure if it's helping or hurting.

    On one of the not-working machines, can you just

    Invoke-Command -ComputerName WHATEVER -Script { Dir c:\ }

    And get results?

    • #62313
      Profile photo of Derek
      Derek
      Participant

      Yes, I got a directory of info on the remote server.

  • #62316
    Profile photo of Don Jones
    Don Jones
    Keymaster

    OK, great. So Remoting is fine. All that Enable-PSRemoting and WSMan Quickconfig was for naught ;).

    The problem, then, is with WebAdministration. Are you able to log on to one of those servers' consoles and run:

    Import-Module 'WebAdministration'
    Get-ChildItem -Path IIS:\Sites

    And get anything? Whilst making a note of the user account you use to do this?

    • #62317
      Profile photo of Derek
      Derek
      Participant

      Import-Module worked with no errors and Get-Childitems returned what I was looking for using the Invoke-Command.

  • #62319
    Profile photo of Don Jones
    Don Jones
    Keymaster

    EXCELLENT. Now we're making progress. The components all work, just not together. I'm assuming that one of the following two are true, but I want you to confirm:

    1. When you run Invoke-Command, you're logged onto your computer as the same user that you just used to log onto the servers' consoles, and your computer and the servers are all in the same domain or in trusting domains

    OR

    2. When you run Invoke-Command, you specify -Credential and provide the same username that you just used to log onto the servers' consoles.

    Is one or both of these true?

    • #62322
      Profile photo of Derek
      Derek
      Participant

      1. Yes
      2. I don't provide -Credentials because my credentials are Admin credentials and I don't use -Credentials for the 40 servers that work okay.

  • #62325
    Profile photo of Don Jones
    Don Jones
    Keymaster

    I know you don't do this for the ones that are working, but these ones aren't working, so we take some extra steps to try and pinpoint the problem. One potential problem is credential delegation not working for some reason; were you to try #2 and explicitly provide a credential, it'd eliminate that as a possibility. We've already determined that Remoting is fine, and that the WebAdministration module works. So we have to start branching out and testing other things.

    Additionally, try using Enter-PSSession to connect to a non-working server, load WebAdministration and try your IIS: drive. Do you get results, or still just a blank prompt, or maybe an error message? You can do this with or without -Credential.

    • #62328
      Profile photo of Derek
      Derek
      Participant

      Can you give me the Enter-PSSession commands / load WebAdministration command you want me to enter so I can do exactly like you want.

  • #62331
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Yup!

    Enter-PSSession -ComputerName WHATEVER
    Import-Module 'WebAdministration'
    Get-ChildItem -Path IIS:\Sites
    Exit
    
    • #62332
      Profile photo of Derek
      Derek
      Participant

      Everything worked like a charm and I got results.

  • #62334
    Profile photo of Don Jones
    Don Jones
    Keymaster

    Well. That is odd.

    Invoke-Command -ComputerName WHATEVER -Script { Import-Module 'WebAdministration'; Start-Sleep 5000; Get-ChildItem -Path IIS:\Sites }
    

    How about that? See, Enter-PSSession and Invoke-Command do the exact same thing, in the same way. Enter-PSSession wasn't supposed to work. The only thing I can think of is some kind of timing in WebAdministration loading, so I'm having you try Start-Sleep.

    • #62335
      Profile photo of Derek
      Derek
      Participant

      No error... just came back with a Prompt again.

    • #62343
      Profile photo of Iron
      Iron
      Participant

      Kind of sheepish here, because I'm new, but could you run the following code?

      Invoke-Commmand -ComputerName Computer -ScriptBlock {([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")}
      

      If that returns false, you may be running into a weird UAC issue. Might try running your console as administrator and giving the code above another try. Should return true if you have elevated rights (which is needed to import the WebAdministration Module).

    • #62500
      Profile photo of Derek
      Derek
      Participant

      I got a TRUE results... as mentioned on another reply .....

      I solved half the problem. It must be a firewall issue. I RDP into another server and ran the invoke-command to all the servers that showed just a prompt and it worked fine. I got the results. My PC from home via VPN must be blocking results from these servers.

      Derek

    • #62499
      Profile photo of Derek
      Derek
      Participant

      I solved half the problem. It must be a firewall issue. I RDP into another server and ran the invoke-command to all the servers that showed just a prompt and it worked fine. I got the results. My PC from home via VPN must be blocking results from these servers.

      Derek

You must be logged in to reply to this topic.