Google

2013-09-27

Issue with PowerShell 4 AD Module on Windows 8.1

Just a quick note that I am seeing some issue with the new PowerShell 4.0 ActiveDirectory Module on Windows 8.1 when retrieving user properties.

PS> get-aduser adil -properties *

get-aduser : One or more properties are invalid.

Parameter name: msDS-AssignedAuthNPolicy

At line:1 char:1

+ get-aduser adil -properties *

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : InvalidArgument: (hindia01:ADUser) [Get-ADUser], ArgumentException

    + FullyQualifiedErrorId : ActiveDirectoryCmdlet:System.ArgumentException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

The property 'msDS-AssignedAuthNPolicy' it is complaining about seems to be a new one implemented on Windows Server 2012 R2, according to MSDN: http://msdn.microsoft.com/en-us/library/dn366549.aspx
You can specify the properties you want manually, except that one of course. Hopefully, Microsoft will fix it by the time Windows 8.1 GA on October 18, 2013.

Update 2014-01-09:
I kinda forgot about this issue but it's still not fixed. You can workaround this issue by piping the user object to Get-ADObject cmdlet:

Get-ADUser Adil | Get-ADObject -properties *



By the way, Get-ADComputer cmdlet has the same issue and same workaround can be applied to it.
There is also a bug filed for it here: https://connect.microsoft.com/PowerShell/feedback/details/806452/windows-8-1-powershell-4-0-get-adcomputer-properties-bug#

Update 2014-03-15
Fixed: http://support.microsoft.com/kb/2923122

Update 2014-03-17
Unfortunately the fix does not seem to work for me. I installed the update rollup March 2014 via WSUS so I already had i for a few days and file information is listed here: http://support.microsoft.com/kb/2928680

I have these files (see the screenshot below) yet I still get the same error as before.


2013-09-02

PowerShell try catch gotcha

I check out StackOverflow's PowerShell section time to time, and one issue seems to trip people a lot: Try/Catch block.

Here is how Try/Catch is block is constructed:


Try {
## Some code here which is likely to fail
}
Catch {
## What to do if try block has a "terminating" error
}

Here is an example:


try { get-content c:\a.txt;"file exists" }catch{"that file does not exist"}

If you run the code above; it will throw an error on the Get-Content if 'c:\a.txt' file does not exist. And in theory we should be able to catch that but that is not exactly what happens:


PS Z:\> try { get-content c:\a.txt;"file exists" }catch{"that file does not exist"}
get-content : Cannot find path 'C:\a.txt' because it does not exist.
At line:1 char:7
+ try { get-content c:\a.txt;"file exists" }catch{"that file does not exist"}
+       ~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\a.txt:String) [Get-Content], ItemNotFoundException
    + FullyQualifiedErrorId : PathNotFound,Microsoft.PowerShell.Commands.GetContentCommand

file exist
PS Z:\>

Did you notice that "file exist" line being printed? Umm, what happened?

Well, remember I said "terminating" errors are caught. the first line is an error but not a terminating error. And therefore, the next line is executed.

Anything we can do to stop this? Yes, use -ErrorAction after the command or $ErrorActionPreference.


PS Z:\> try { get-content c:\a.txt -ErrorAction stop; "file exists" }catch{"that file does not exist"}
that file does not exist
PS Z:\>

With ErrorAction set to STOP, the error on the first line becomes a terminating error and hence get caught in the CATCH {} block.

Similarly, we can set the $ErrorActionPreference variable before the line top "STOP"; and tell our code to treat any error as terminating one.


PS Z:\> $ErrorActionPreference="STOP"; try { get-content c:\a.txt; "file exists" }catch{"that file does not exist"}
that file does not exist
PS Z:\>

If not specified, $ErrorActionPreference is set to "Continue".


PS Z:\> $ErrorActionPreference
Continue
PS Z:\>

Note that it is not "SilentlyContinue", which would execute the next statement without complaining about the error.


PS Z:\> try { get-content c:\a.txt -ErrorAction SilentlyContinue; "file exists" }catch{"that file does not exist"}
file exist

This is equivalent to "On Error Resume Next" in vbScript.