Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 8 additions & 19 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/ConsoleHost.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2141,9 +2141,7 @@ private void ReportException(Exception e, Executor exec)

// NTRAID#Windows OS Bugs-1143621-2005/04/08-sburns

IContainsErrorRecord icer = e as IContainsErrorRecord;

if (icer != null)
if (e is IContainsErrorRecord icer)
{
error = icer.ErrorRecord;
}
Expand Down Expand Up @@ -2200,8 +2198,7 @@ private void ReportExceptionFallback(Exception e, string header)

// See if the exception has an error record attached to it...
ErrorRecord er = null;
IContainsErrorRecord icer = e as IContainsErrorRecord;
if (icer != null)
if (e is IContainsErrorRecord icer)
er = icer.ErrorRecord;

if (e is PSRemotingTransportException)
Expand Down Expand Up @@ -2736,8 +2733,7 @@ e is RemoteException ||

internal void BlockCommandOutput()
{
RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline;
if (rCmdPipeline != null)
if (_parent.runningCmd is RemotePipeline rCmdPipeline)
{
rCmdPipeline.DrainIncomingData();
rCmdPipeline.SuspendIncomingData();
Expand All @@ -2750,8 +2746,7 @@ internal void BlockCommandOutput()

internal void ResumeCommandOutput()
{
RemotePipeline rCmdPipeline = _parent.runningCmd as RemotePipeline;
if (rCmdPipeline != null)
if (_parent.runningCmd is RemotePipeline rCmdPipeline)
{
rCmdPipeline.ResumeIncomingData();
}
Expand Down Expand Up @@ -2841,8 +2836,7 @@ private static bool IsIncompleteParseException(Exception e)
}

// If it is remote exception ferret out the real exception.
RemoteException remoteException = e as RemoteException;
if (remoteException == null || remoteException.ErrorRecord == null)
if (e is not RemoteException remoteException || remoteException.ErrorRecord == null)
{
return false;
}
Expand Down Expand Up @@ -2923,8 +2917,7 @@ private string EvaluatePrompt()
// Check for the pushed runspace scenario.
if (_isRunspacePushed)
{
RemoteRunspace remoteRunspace = _parent.Runspace as RemoteRunspace;
if (remoteRunspace != null)
if (_parent.Runspace is RemoteRunspace remoteRunspace)
{
promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession);
}
Expand Down Expand Up @@ -2958,13 +2951,9 @@ private string EvaluateDebugPrompt()

PSObject prompt = output.ReadAndRemoveAt0();
string promptString = (prompt != null) ? (prompt.BaseObject as string) : null;
if (promptString != null)
if (promptString != null && _parent.Runspace is RemoteRunspace remoteRunspace)
{
RemoteRunspace remoteRunspace = _parent.Runspace as RemoteRunspace;
if (remoteRunspace != null)
{
promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession);
}
promptString = HostUtilities.GetRemotePrompt(remoteRunspace, promptString, _parent._inPushedConfiguredSession);
}

return promptString;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -354,8 +354,7 @@ public override
}
catch (HostException e)
{
Win32Exception win32exception = e.InnerException as Win32Exception;
if (win32exception != null &&
if (e.InnerException is Win32Exception win32exception &&
win32exception.NativeErrorCode == 0x57)
{
throw PSTraceSource.NewArgumentOutOfRangeException("value", value,
Expand Down
9 changes: 3 additions & 6 deletions src/Microsoft.PowerShell.ConsoleHost/host/msh/Executor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,7 @@ private void ErrorObjectStreamHandler(object sender, EventArgs e)
private void AsyncPipelineFailureHandler(Exception ex)
{
ErrorRecord er = null;
IContainsErrorRecord cer = ex as IContainsErrorRecord;
if (cer != null)
if (ex is IContainsErrorRecord cer)
{
er = cer.ErrorRecord;
// Exception inside the error record is ParentContainsErrorRecordException which
Expand Down Expand Up @@ -483,8 +482,7 @@ internal string ExecuteCommandAndGetResultAsString(string command, out Exception
// And convert the base object into a string. We can't use the proxied
// ToString() on the PSObject because there is no default runspace
// available.
PSObject msho = streamResults[0] as PSObject;
if (msho != null)
if (streamResults[0] is PSObject msho)
result = msho.BaseObject.ToString();
else
result = streamResults[0].ToString();
Expand Down Expand Up @@ -585,8 +583,7 @@ private void Cancel()

internal void BlockCommandOutput()
{
RemotePipeline remotePipeline = _pipeline as RemotePipeline;
if (remotePipeline != null)
if (_pipeline is RemotePipeline remotePipeline)
{
// Waits until queued data is handled.
remotePipeline.DrainIncomingData();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -294,8 +294,7 @@ to fill the passed in ETW data descriptor.
{
dataDescriptor->Reserved = 0;

string sRet = data as string;
if (sRet != null)
if (data is string sRet)
{
dataDescriptor->Size = (uint)((sRet.Length + 1) * 2);
return sRet;
Expand Down
6 changes: 2 additions & 4 deletions src/Microsoft.PowerShell.Security/security/AclCommands.cs
Original file line number Diff line number Diff line change
Expand Up @@ -291,8 +291,7 @@ public static AuthorizationRuleCollection GetAccess(PSObject instance)
}

// Get DACL
CommonObjectSecurity cos = sd as CommonObjectSecurity;
if (cos != null)
if (sd is CommonObjectSecurity cos)
{
return cos.GetAccessRules(true, true, typeof(NTAccount));
}
Expand Down Expand Up @@ -326,8 +325,7 @@ public static AuthorizationRuleCollection GetAudit(PSObject instance)
PSTraceSource.NewArgumentException(nameof(instance));
}

CommonObjectSecurity cos = sd as CommonObjectSecurity;
if (cos != null)
if (sd is CommonObjectSecurity cos)
{
return cos.GetAuditRules(true, true, typeof(NTAccount));
}
Expand Down
129 changes: 52 additions & 77 deletions src/Microsoft.PowerShell.Security/security/CertificateProvider.cs
Original file line number Diff line number Diff line change
Expand Up @@ -724,16 +724,11 @@ protected override void RemoveItem(
ThrowInvalidOperation(errorId, message);
}

if (DynamicParameters != null)
if (DynamicParameters != null && DynamicParameters is ProviderRemoveItemDynamicParameters dp)
{
ProviderRemoveItemDynamicParameters dp =
DynamicParameters as ProviderRemoveItemDynamicParameters;
if (dp != null)
if (dp.DeleteKey)
{
if (dp.DeleteKey)
{
fDeleteKey = true;
}
fDeleteKey = true;
}
}

Expand Down Expand Up @@ -888,9 +883,8 @@ protected override void MoveItem(
object store = GetItemAtPath(destination, false, out isDestContainer);

X509Certificate2 certificate = cert as X509Certificate2;
X509NativeStore certstore = store as X509NativeStore;

if (certstore != null)
if (store is X509NativeStore certstore)
{
certstore.Open(true);

Expand Down Expand Up @@ -1068,23 +1062,18 @@ protected override bool HasChildItems(string path)

if ((item != null) && isContainer)
{
X509StoreLocation storeLocation = item as X509StoreLocation;
if (storeLocation != null)
if (item is X509StoreLocation storeLocation)
{
result = storeLocation.StoreNames.Count > 0;
}
else
else if (item is X509NativeStore store)
{
X509NativeStore store = item as X509NativeStore;
if (store != null)
store.Open(IncludeArchivedCerts());
IntPtr certContext = store.GetFirstCert();
if (certContext != IntPtr.Zero)
{
store.Open(IncludeArchivedCerts());
IntPtr certContext = store.GetFirstCert();
if (certContext != IntPtr.Zero)
{
store.FreeCert(certContext);
result = true;
}
store.FreeCert(certContext);
result = true;
}
}
}
Expand Down Expand Up @@ -1259,20 +1248,15 @@ protected override void GetItem(string path)
return;
}

X509StoreLocation storeLocation = item as X509StoreLocation;
if (storeLocation != null) // store location
if (item is X509StoreLocation storeLocation) // store location
{
WriteItemObject(item, path, isContainer);
}
else // store
else if (item is X509NativeStore store) // store
{
X509NativeStore store = item as X509NativeStore;
if (store != null)
{
// create X509Store
X509Store outStore = new(store.StoreName, store.Location.Location);
WriteItemObject(outStore, path, isContainer);
}
// create X509Store
X509Store outStore = new(store.StoreName, store.Location.Location);
WriteItemObject(outStore, path, isContainer);
}
}
}
Expand Down Expand Up @@ -2644,51 +2628,46 @@ private CertificateFilterInfo GetFilter()
{
CertificateFilterInfo filter = null;

if (DynamicParameters != null)
if (DynamicParameters != null && DynamicParameters is CertificateProviderDynamicParameters dp)
{
CertificateProviderDynamicParameters dp =
DynamicParameters as CertificateProviderDynamicParameters;
if (dp != null)
if (dp.CodeSigningCert)
{
if (dp.CodeSigningCert)
{
filter = new CertificateFilterInfo();
filter.Purpose = CertificatePurpose.CodeSigning;
}
filter = new CertificateFilterInfo();
filter.Purpose = CertificatePurpose.CodeSigning;
}

if (dp.DocumentEncryptionCert)
{
filter ??= new CertificateFilterInfo();
filter.Purpose = CertificatePurpose.DocumentEncryption;
}
if (dp.DocumentEncryptionCert)
{
filter ??= new CertificateFilterInfo();
filter.Purpose = CertificatePurpose.DocumentEncryption;
}

if (dp.DnsName != null)
{
filter ??= new CertificateFilterInfo();
filter.DnsName = new WildcardPattern(dp.DnsName, WildcardOptions.IgnoreCase);
}
if (dp.DnsName != null)
{
filter ??= new CertificateFilterInfo();
filter.DnsName = new WildcardPattern(dp.DnsName, WildcardOptions.IgnoreCase);
}

if (dp.Eku != null)
if (dp.Eku != null)
{
filter ??= new CertificateFilterInfo();
filter.Eku = new List<WildcardPattern>();
foreach (var pattern in dp.Eku)
{
filter ??= new CertificateFilterInfo();
filter.Eku = new List<WildcardPattern>();
foreach (var pattern in dp.Eku)
{
filter.Eku.Add(new WildcardPattern(pattern, WildcardOptions.IgnoreCase));
}
filter.Eku.Add(new WildcardPattern(pattern, WildcardOptions.IgnoreCase));
}
}

if (dp.ExpiringInDays >= 0)
{
filter ??= new CertificateFilterInfo();
filter.Expiring = DateTime.Now.AddDays(dp.ExpiringInDays);
}
if (dp.ExpiringInDays >= 0)
{
filter ??= new CertificateFilterInfo();
filter.Expiring = DateTime.Now.AddDays(dp.ExpiringInDays);
}

if (dp.SSLServerAuthentication)
{
filter ??= new CertificateFilterInfo();
filter.SSLServerAuthentication = true;
}
if (dp.SSLServerAuthentication)
{
filter ??= new CertificateFilterInfo();
filter.SSLServerAuthentication = true;
}
}

Expand Down Expand Up @@ -3311,17 +3290,13 @@ public EnhancedKeyUsageProperty(X509Certificate2 cert)
foreach (X509Extension extension in cert.Extensions)
{
// Filter to the OID for EKU
if (extension.Oid.Value == "2.5.29.37")
if (extension.Oid.Value == "2.5.29.37" && extension is X509EnhancedKeyUsageExtension ext)
{
X509EnhancedKeyUsageExtension ext = extension as X509EnhancedKeyUsageExtension;
if (ext != null)
OidCollection oids = ext.EnhancedKeyUsages;
foreach (Oid oid in oids)
{
OidCollection oids = ext.EnhancedKeyUsages;
foreach (Oid oid in oids)
{
EnhancedKeyUsageRepresentation ekuString = new(oid.FriendlyName, oid.Value);
_ekuList.Add(ekuString);
}
EnhancedKeyUsageRepresentation ekuString = new(oid.FriendlyName, oid.Value);
_ekuList.Add(ekuString);
}
}
}
Expand Down