Help me hate PowerShell a little less, please.

  • I found an interesting and useful PoSh script at the following URL:

    http://woshub.com/get-adcomputer-getting-active-directory-computers-info-via-powershell/

    Here's the script that I got directly from that article and tried to execute through a Cmd window and first typed PowerShell to get into it.
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name, OperatingSystem | Format-Table -AutoSize

    When I run that fine bit o' code, here's the response I got...

    Get-ADComputer : The term 'Get-ADComputer' is not recognized as the name of a cmdlet, function, script file, or operable program.
    Check the spelling of the name, or if a path was included, verify that the path is correct and try again.
    At line:1 char:1
    + Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } - ...
    + ~~~~~~~~~~~~~~
        + CategoryInfo          : ObjectNotFound: (Get-ADComputer:String) [], CommandNotFoundException
        + FullyQualifiedErrorId : CommandNotFoundException

    Ok... maybe I have to load something first.  Let's try that... so I run the following per the article...

    Import-Module activedirectory

    Wonderful... here's what I get...

    Import-Module : The specified module 'activedirectory' was not loaded because no valid module file was found in any module directory.
    At line:1 char:1
    + Import-Module activedirectory
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : ResourceUnavailable: (activedirectory:String) [Import-Module], FileNotFoundException
        + FullyQualifiedErrorId : Modules_ModuleNotFound,Microsoft.PowerShell.Commands.ImportModuleCommand

    Ok... even though I'm running Windows Server 12 and I'm RDC'd directly into the box with admin privs and started the Cmd window with "Run as Administrator", maybe my Windows guys haven't got the right version of Powershell loaded.  Lemme find out... and I run the following command.

    $PSVersionTable.PSVersion

    Well wadaya know!  Something in Powershell actually worked!  It looks to me like I have Version 5.

    Major  Minor  Build  Revision
    -----  -----  -----  --------
    5      1      14409  1012

    To make matters worse, it says the following in the article...

    Tip. In PowerShell 3.0 (installed in Windows Server 2012) or later, this module is imported by default.

    I run into this a lot.  You find something really cool that can easily be done with PowerShell  and it doesn't work at all never mind anything close to what was advertised.  What is wrong with the code in the article or what is it that I'm doing wrong?  How do I get the original command from above to work or have I been sniped by a fake article?

    Thanks for any help on this one.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • ActiveDirectory is NOT part of of the standard install of PowerShell, instead you need to install it. It might well be that ActiveDirectory is imported by default, if installed, but it's does not install with the OS. There's an article on how here: https://technet.microsoft.com/en-us/library/dd378937(v=ws.10).aspx

    I had the same problem a little while ago, where I needed to query the AD Groups a user was part of, and then run a log on script. This needed to run on the Staff Desktops, rather than the server, so in addition to that, I would have had to check if the ActiveDirectory Module was installed, install it if not, and then run the rest. Considering this needed to run (very) quickly in the background to configure an app, I wasn't interested in writing that much so I just made use of net group and read each line from the output looking for the specific AD group names.

    I admit, it does frustrate me how few modules come "natively installed" with PowerShell. They aren't to hard to install but if you want those on client machines, rather than a specific server, then it can be a real pain while you wait for those machines to notice the new Group policy, and installs it. Especially if those machines then also require a restart afterwards.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • I'm not sure this a shortcoming of PowerShell.  The standard installation of Windows doesn't include the Active Directory management tools.  If you add that particular feature, you get the Active Directory Users and Groups GUI, and the PowerShell module.  At least that's how I remember it.

    John

  • John Mitchell-245523 - Wednesday, August 2, 2017 2:38 AM

    I'm not sure this a shortcoming of PowerShell.  The standard installation of Windows doesn't include the Active Directory management tools.  If you add that particular feature, you get the Active Directory Users and Groups GUI, and the PowerShell module.  At least that's how I remember it.

    John

    Yeah, that's the case.

    I agree, it's not necessarily a shortfall of Powershell itself, but I do feel it's a shortfall for Microsoft. If you're running a Professional/Server version of Windows, it's almost definitely going to be in an AD. Given the work that Microsoft want people to use PoSh, you'd think that some of the "useful" (for lack of a better word) tools might come readily available.

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thom A - Wednesday, August 2, 2017 2:19 AM

    ActiveDirectory is NOT part of of the standard install of PowerShell, instead you need to install it. It might well be that ActiveDirectory is imported by default, if installed, but it's does not install with the OS. There's an article on how here: https://technet.microsoft.com/en-us/library/dd378937(v=ws.10).aspx

    I had the same problem a little while ago, where I needed to query the AD Groups a user was part of, and then run a log on script. This needed to run on the Staff Desktops, rather than the server, so in addition to that, I would have had to check if the ActiveDirectory Module was installed, install it if not, and then run the rest. Considering this needed to run (very) quickly in the background to configure an app, I wasn't interested in writing that much so I just made use of net group and read each line from the output looking for the specific AD group names.

    I admit, it does frustrate me how few modules come "natively installed" with PowerShell. They aren't to hard to install but if you want those on client machines, rather than a specific server, then it can be a real pain while you wait for those machines to notice the new Group policy, and installs it. Especially if those machines then also require a restart afterwards.

    Thanks, Tom.  Here's the problem I have even with that article... it says (and I quote, bold emphasis is mine)...

    The Active Directory module is installed with the following features by default:

    • Windows PowerShell
    • The Microsoft .NET Framework 3.5.1 or 4.5

    For the Active Directory module to function correctly, Windows PowerShell and the .NET Framework 3.5.1 or 4.5 must be installed.


    Both of those features are loaded on the server and yet the Active Directory module is not available, which only leads to addition frustration on my part.  :crazy:  Any idea how I can figure out why the module didn't auto-magically install under these conditions?  I ask because, if I knew, it would solve a pot wad of similar "not available" problems.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Wednesday, August 2, 2017 8:31 AM

    Thom A - Wednesday, August 2, 2017 2:19 AM

    ActiveDirectory is NOT part of of the standard install of PowerShell, instead you need to install it. It might well be that ActiveDirectory is imported by default, if installed, but it's does not install with the OS. There's an article on how here: https://technet.microsoft.com/en-us/library/dd378937(v=ws.10).aspx

    I had the same problem a little while ago, where I needed to query the AD Groups a user was part of, and then run a log on script. This needed to run on the Staff Desktops, rather than the server, so in addition to that, I would have had to check if the ActiveDirectory Module was installed, install it if not, and then run the rest. Considering this needed to run (very) quickly in the background to configure an app, I wasn't interested in writing that much so I just made use of net group and read each line from the output looking for the specific AD group names.

    I admit, it does frustrate me how few modules come "natively installed" with PowerShell. They aren't to hard to install but if you want those on client machines, rather than a specific server, then it can be a real pain while you wait for those machines to notice the new Group policy, and installs it. Especially if those machines then also require a restart afterwards.

    Thanks, Tom.  Here's the problem I have even with that article... it says (and I quote, bold emphasis is mine)...

    The Active Directory module is installed with the following features by default:

    • Windows PowerShell
    • The Microsoft .NET Framework 3.5.1 or 4.5

    For the Active Directory module to function correctly, Windows PowerShell and the .NET Framework 3.5.1 or 4.5 must be installed.


    Both of those features are loaded on the server and yet the Active Directory module is not available, which only leads to addition frustration on my part.  :crazy:  Any idea how I can figure out why the module didn't auto-magically install under these conditions?  I ask because, if I knew, it would solve a pot wad of similar "not available" problems.

    Ah... reading down further, I see...

    If you want to use the Active Directory module to manage an Active Directory domain, an AD LDS instance or configuration set, or an Active Directory Database Mounting Tool instance, the Active Directory Web Services (ADWS) service must be installed on at least one domain controller in this domain or on one server that hosts your AD LDS instance. For more information about ADWS, see What's New in AD DS: Active Directory Web Services.

    If that has anything to do with the problems I'm having, then it's official... I hate Powershell... a lot.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Wednesday, August 2, 2017 8:31 AM

    Both of those features are loaded on the server and yet the Active Directory module is not available, which only leads to addition frustration on my part.  :crazy:  Any idea how I can figure out why the module didn't install under these conditions?  I ask because, if I knew, it would solve a pot wad of similar "not available" problems.

    OK, did a little more further reading on the subject. The module is "installed" but not enabled. Thus, you also have to enable it, so you can add the module and use it (although the first command looks like an installation process still):

    Add-WindowsFeature RSAT-AD-PowerShell
    Import-Module ActiveDirectory
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name, OperatingSystem | Format-Table -AutoSize

    (I admit, I'm not helping your love for PoSh here. But I don't have much for it either, I much more prefer working in bash).

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • On my 2016 server it was not installed by default.
    I had to install Active Directory Module for Windows Powershell feature via Server Manager
    I do not remember having to enabled it
    When I call an AD cmdlet it loads automatically.

    Far away is close at hand in the images of elsewhere.
    Anon.

  • Thom A - Wednesday, August 2, 2017 8:43 AM

    Jeff Moden - Wednesday, August 2, 2017 8:31 AM

    Both of those features are loaded on the server and yet the Active Directory module is not available, which only leads to addition frustration on my part.  :crazy:  Any idea how I can figure out why the module didn't install under these conditions?  I ask because, if I knew, it would solve a pot wad of similar "not available" problems.

    OK, did a little more further reading on the subject. The module is "installed" but not enabled. Thus, you also have to enable it, so you can add the module and use it (although the first command looks like an installation process still):

    Add-WindowsFeature RSAT-AD-PowerShell
    Import-Module ActiveDirectory
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name, OperatingSystem | Format-Table -AutoSize

    (I admit, I'm not helping your love for PoSh here. But I don't have much for it either, I much more prefer working in bash).

    Thanks for the help, Thom.  I'll give that a try.  You're correct, though.  I'm thinking of referring to it as "PowerSmell" and changing the abbreviation to "POS". 😉  It won't help the situation but at least I'll get some personal satisfaction there. 😉

    Shifting gears a bit, how on Earth did you figure out that you need to use the Add-WindowsFeature Cmdlet and what the "operand" should say?

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • David Burrows - Wednesday, August 2, 2017 9:06 AM

    On my 2016 server it was not installed by default.
    I had to install Active Directory Module for Windows Powershell feature via Server Manager
    I do not remember having to enabled it
    When I call an AD cmdlet it loads automatically.

    That's some good info, David.  We're upgrading from 2012 Expensive Edition to 2016 in a month or two.

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • Jeff Moden - Wednesday, August 2, 2017 9:14 AM

    Thom A - Wednesday, August 2, 2017 8:43 AM

    Jeff Moden - Wednesday, August 2, 2017 8:31 AM

    Both of those features are loaded on the server and yet the Active Directory module is not available, which only leads to addition frustration on my part.  :crazy:  Any idea how I can figure out why the module didn't install under these conditions?  I ask because, if I knew, it would solve a pot wad of similar "not available" problems.

    OK, did a little more further reading on the subject. The module is "installed" but not enabled. Thus, you also have to enable it, so you can add the module and use it (although the first command looks like an installation process still):

    Add-WindowsFeature RSAT-AD-PowerShell
    Import-Module ActiveDirectory
    Get-ADComputer -Filter { OperatingSystem -Like '*Windows Server*' } -Properties OperatingSystem | Select Name, OperatingSystem | Format-Table -AutoSize

    (I admit, I'm not helping your love for PoSh here. But I don't have much for it either, I much more prefer working in bash).

    Shifting gears a bit, how on Earth did you figure out that you need to use the Add-WindowsFeature Cmdlet and what the "operand" should say?

    Google.co.uk 😉 and the search term "Enable ActiveDirectory Module". Second link gave me the answer (but that was first link I tried, as I normally go to the 2nd result first, then the 1st). I knew that the module needed to be enabled/installed, just didn't know the syntax. You can do it through the GUI but for completion thought I'd take the for PoSh route. 🙂

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

  • Thanks, Thom.  Santa said he's going to give me a new brain this Christmas.  I've asked him if he could push that up a bit because, apparently, mine isn't working so hot lately. :blush:

    --Jeff Moden


    RBAR is pronounced "ree-bar" and is a "Modenism" for Row-By-Agonizing-Row.
    First step towards the paradigm shift of writing Set Based code:
    ________Stop thinking about what you want to do to a ROW... think, instead, of what you want to do to a COLUMN.
    "Change is inevitable... change for the better is not".
    "Dear Lord... I'm a DBA so please give me patience because, if you give me strength, I'm going to need bail money too!"

    Helpful Links:
    How to post code problems
    How to Post Performance Problems
    Create a Tally Function (fnTally)

  • I am having also this issue. How do I use a PowerShell script built using the Windows Active Directory module as an Active Roles Server Script Module? I am trying this I am getting an error that "GEt-ADGroupMember" is not recognized. I think this is because it does not recognize the cmdlet without the ActiveDirectory module.

     

  • jackaustin wrote:

    I am having also this issue. How do I use a PowerShell script built using the Windows Active Directory module as an Active Roles Server Script Module? I am trying this I am getting an error that "GEt-ADGroupMember" is not recognized. I think this is because it does not recognize the cmdlet without the ActiveDirectory module.

    Yes, you need to install the feature first, and then you need to import the module into the current PowerShell session. I demonstrate the steps here for Powershell 5.0 or prior (note you only need to install the feature once). It might work on Powershell 6.0+ but I have not tested it (I have access to commands like Get-ADGroupMember in Powershell 7.1.3 so I assume it does).

    Thom~

    Excuse my typos and sometimes awful grammar. My fingers work faster than my brain does.
    Larnu.uk

Viewing 14 posts - 1 through 14 (of 14 total)

You must be logged in to reply to this topic. Login to reply