Do not throw when getting Unix domain or user name
If the attempt to get the name fails, throwing an exception results in PowerShell crashing. Instead, we should simply return empty data. "unknown" is specifically not returned because it would be ambiguous; it is not impossible to have an actual user or domain named "unknown".
This commit is contained in:
parent
8b31f2c606
commit
816dac0a55
|
@ -312,20 +312,18 @@ namespace System.Management.Automation
|
||||||
|
|
||||||
internal static string NonWindowsGetDomainName()
|
internal static string NonWindowsGetDomainName()
|
||||||
{
|
{
|
||||||
string fullyQualifiedName = Unix.NativeMethods.GetFullyQualifiedName();
|
string name = Unix.NativeMethods.GetFullyQualifiedName();
|
||||||
if (string.IsNullOrEmpty(fullyQualifiedName))
|
if (!string.IsNullOrEmpty(name))
|
||||||
{
|
{
|
||||||
int lastError = Marshal.GetLastWin32Error();
|
// name is hostname.domainname, so extract domainname
|
||||||
throw new InvalidOperationException("Unix.NonWindowsGetDomainName error: " + lastError);
|
int index = name.IndexOf('.');
|
||||||
|
if (index >= 0)
|
||||||
|
{
|
||||||
|
return name.Substring(index + 1);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
// if the domain name could not be found, do not throw, just return empty
|
||||||
int index = fullyQualifiedName.IndexOf('.');
|
return string.Empty;
|
||||||
if (index >= 0)
|
|
||||||
{
|
|
||||||
return fullyQualifiedName.Substring(index + 1);
|
|
||||||
}
|
|
||||||
|
|
||||||
return "";
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static string NonWindowsGetUserName()
|
internal static string NonWindowsGetUserName()
|
||||||
|
@ -336,13 +334,7 @@ namespace System.Management.Automation
|
||||||
// Hostname in this context seems to be the FQDN
|
// Hostname in this context seems to be the FQDN
|
||||||
internal static string NonWindowsGetHostName()
|
internal static string NonWindowsGetHostName()
|
||||||
{
|
{
|
||||||
string hostName = Unix.NativeMethods.GetFullyQualifiedName();
|
return Unix.NativeMethods.GetFullyQualifiedName() ?? string.Empty;
|
||||||
if (string.IsNullOrEmpty(hostName))
|
|
||||||
{
|
|
||||||
int lastError = Marshal.GetLastWin32Error();
|
|
||||||
throw new InvalidOperationException("Unix.NonWindowsHostName error: " + lastError);
|
|
||||||
}
|
|
||||||
return hostName;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static bool NonWindowsIsFile(string path)
|
internal static bool NonWindowsIsFile(string path)
|
||||||
|
@ -377,13 +369,8 @@ namespace System.Management.Automation
|
||||||
if (string.IsNullOrEmpty(s_userName))
|
if (string.IsNullOrEmpty(s_userName))
|
||||||
{
|
{
|
||||||
s_userName = NativeMethods.GetUserName();
|
s_userName = NativeMethods.GetUserName();
|
||||||
if (string.IsNullOrEmpty(s_userName))
|
|
||||||
{
|
|
||||||
int lastError = Marshal.GetLastWin32Error();
|
|
||||||
throw new InvalidOperationException("Unix.UserName error: " + lastError);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
return s_userName;
|
return s_userName ?? string.Empty;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue