Friday, 8 December 2017

Get-LocalGroupMember PowerShell cmdlet issues - An unspecified error occurred: error code = 1789

We've recently been working on the new local account management PowerShell cmdlets and found an issue with the Get-LocalGroupMember cmdlet

If a user group has a member that cannot be resolved normally most APIs will return just the SID of the account, and certainly the Computer Management MMC shows the user as the SID and as an unknown account.


Unfortunately the  Get-LocalGroupDetails PowerShell cmdlet fails completely with the following error



 Get-LocalGroupDetails. An unspecified error occurred: error code = 1789

If you use an -ErrorAction SilentlyContinue parameter the cmdlet will return with no members which is obviously misleading as there may be other, valid accounts in the group.



Thursday, 7 December 2017

How to relock a Bitlocker drive after you've unlocked it with PowerShell

If you're using Windows Server 2012 or above you can use PowerShell to relock a bitlocker drive that you have already unlocked

Lock-BitLocker E:

However if the drive is in use you may see this error

Lock-BitLocker : Access is denied. (Exception from HRESULT: 0x80070005
(E_ACCESSDENIED))
At line:1 char:1
+ Lock-BitLocker E:
+ ~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (:) [Write-Error], UnauthorizedAcc
   essException
    + FullyQualifiedErrorId : System.UnauthorizedAccessException,Lock-BitLocke
   r
 

This is simple to resolve - simply add the -ForceDismount parameter
Lock-BitLocker E: -ForceDismount  



Tuesday, 5 December 2017

Running start in a batch file or .cmd file displays a command prompt instead of running the application.

I've recently run into an issue whereby I'm trying to start several Visual Studio environments as another user with the runas command for remote testing.

So I created a batch file with the runas command which then launches a second batch file that starts 4 instances of Visual Studio.

@echo off
cls
start "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"

However instead 4 command prompt windows are loaded.

When you run the same command live inside a command prompt it works.

This is a case of not reading the documentation! The first parameter of the start command is the window title, so you need two double quotes, or just enter any title.

@echo off
cls
start "" "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "" "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "" "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"
start "" "C:\Program Files (x86)\Microsoft Visual Studio\2017\Professional\Common7\IDE\devenv.exe"

Friday, 1 December 2017

System.IO.File.ReadAllText method and Default encoding with Nano server

You may find that running the following command on Nano will fail
[System.IO.File]::ReadAllText($path, [System.Text.Encoding]::Default)

Exception calling "ReadAllText" with "2" argument(s): "Value cannot be null.

This is because the Default encoding is not longer in the list of valid encoding types


To resolve this issue either call ReadAllText with no parameter, or a different encoding parameter. Or better still use the Get-Content cmdlet which takes the pain away from this encoding.

Thursday, 30 November 2017

XIA Configuration Server reviewed on the 4SYSOPs web site

We now have a review of XIA Configuration Server live on the 4SYSOPs web site

https://4sysops.com/archives/xia-configuration-easy-network-inventory-and-documentation-solution/


4sysops is an online community for IT professionals. Experienced IT pros cover the latest technologies in system administration, cloud computing and DevOps.

Thursday, 23 November 2017

PowerShell Get-ItemProperty cmdlet returns garbled registry values with artifacts, regedit shows ellipses ("...") after the registry value.

We've recently stumbled upon a problem with reading registry entries using PowerShell where the Get-ItemProperty or Get-Item cmdlet returns garbled registry values with artifacts.

You'll notice that Regedit shows ellipses ("...") after the registry value.

For example here's the ODBC settings for the Oracle ODBC driver.



If you double click the registry value however it shows correctly.

When you try get the value using PowerShell things get very messy
Get-ItemProperty -LiteralPath "Registry::HKEY_LOCAL_MACHINE\SOFTWARE\ODB
C\ODBCINST.INI\Oracle in client" -Name Driver


If you read the registry value using WMI using the standard WMI registry provider, this shows the string correctly.

What is happening here is that the string in the registry is stored as a null-terminated string, and after the null terminator a lot of binary data has been dumped that we're not meant to see. Obviously this data should really be stored in another REG_BINARY key, however this has not been done.

You can clean the string by calling substring on the index of the first null character. 



if ($driver.Driver.Contains($null))
{
    $driver.Driver.Substring(0, $driver.Driver.IndexOf($null))
}



Problem solved.






C#.NET Winforms PropertyGrid reports "Properties Window", "Index was outside the bounds of the array".

You may find when using the PropertyGrid in a C#.NET Winforms application that the PropertyGrid displays an alert with the title "Properties Window" and the message "Index was outside the bounds of the array" when you are editing a collection.



This is a somewhat misleading error as this can occur if you are modifying a Collection<T> where the object



/// <summary>
/// Represents an ODBC data source property.
/// </summary>
[Serializable]
[XmlRoot("Property")]
public abstract class OdbcDataSourceProperty : GlobalSerializableBase, IComparable<OdbcDataSourceProperty>
{


Remove the abstract modifier and the problem will go away.