Marc Rufer bio photo

Marc Rufer

Software engineer
@d-fens GmbH

Twitter LinkedIn Github Stackoverflow
RSS Feed

Get NetworkCredential from PSCredential splits Username into Username and Domain

Marc Rufer 13 Apr 2017 .NET C# PowerShell

During development of a PowerShell binary module I had to do deal with two different credential types (System.Management.Automation.PSCredential and System.Net.NetworkCredential) and came across a behaviour I wanted to write down.

The PowerShell binary module I developed contains several Cmdlets. One of them defines a parameter of type System.Management.Automation.PSCredential. Inside the Cmdlet we need to have an object of type System.Net.NetworkCredential. Getting such an object is easy as the System.Management.Automation.PSCredential provides an instance method called GetNetworkCredential(), which returns an object of type System.Net.NetworkCredential. This is the point I came across a behaviour I didn’t expect. I expected GetNetworkCredential() method to return a new NetworkCredential object with exactly the same the UserName and Password as the corresponding System.Management.Automation.PSCredential object. However in case the UserName property of the System.Management.Automation.PSCredential object contains a domain part the UserName property of the System.Net.NetworkCredential returned by GetNetworkCredential() does not contain the domain part anymore. The domain part gets cut and assigned to the Domain property of the System.Net.NetworkCredential as shown in the following code sample.

PS C:\>$usernameWithDomain = 'my-domain\arbitraryUser';
PS C:\>$password = 'P@ssw0rd';

PS C:\>$psCredential = [System.Management.Automation.PSCredential]::new($username, (ConvertTo-SecureString -AsPlainText -String $password -Force));
PS C:\>$psCredential.UserName;

PS C:\>$networkCredentialFromPSCredential = $psCredential.GetNetworkCredential();
PS C:\>$networkCredentialFromPSCredential.UserName;
PS C:\>$networkCredentialFromPSCredential.Domain;

The reason behind this behaviour is, that System.Net.NetworkCredential, in contrast to System.Management.Automation.PSCredential, has a Domain property, which will be filled up with the domain part of the UserName.

comments powered by Disqus


HOWTO Access Azure File Share in PowerShell Script executed by a Scheduled Task BUG "Task Scheduler service is not available" Error, if specifying Network Connection Condition HOWTO Sync OneDrive on Server even if Windows User not logged in HOWTO Access Microsoft Access Database with PowerShell Get NetworkCredential from PSCredential splits Username into Username and Domain HOWTO Create LocalDB File (.mdf) manually in Visual Studio 2015 HOWTO Set Cookie Header on DefaultRequestHeaders of HttpClient HOWTO Set up PowerShell Remote Session Configuration HOWTO Analyze PowerShell Scripts with PSScriptAnalyzer HOWTO Sign PDF with SuisseID NoBrainer PUT/PATCH/MERGE with OData Service Client Apply Commit from one Repository to another GitHub Repository creation with PowerShell HOWTO Sign PowerShell Script with SuisseID Swissunihockey game schedule PDF generator HOWTO Maven Release on JetBrains TeamCity Limitations when running Activiti in H2 Embedded Mode HOWTO Pylint Integration in IntelliJ 14 HOWTO Install Windows 10 IoT Core on Raspberry PI 2 from a VM First steps with Dropwizard Allman code style for IntelliJ First steps with Flyway IntelliJ - Setup custom inspection profiles Configuration properties meta-data support in IntelliJ AMQP Integration with spring integration OCA Java SE 7 Programmer I Certification Guide Swissunihockey key matcher Swisscom SMS-API-Client Some hints concerning logging with Log4j IFTTT A brilliant service Run deltacloud on a VirtualBox-VM Project specific maven settings in IntelliJ AngularJS Javascript for beginners Liferay in Action