@@ -1333,7 +1333,6 @@ public static bool TryReadRequiredResourceFile(
13331333 out resourceInfo ,
13341334 out error ) ;
13351335 }
1336-
13371336 private static bool TryReadPSDataFile (
13381337 string filePath ,
13391338 string [ ] allowedVariables ,
@@ -1354,84 +1353,70 @@ private static bool TryReadPSDataFile(
13541353
13551354 // Parallel.ForEach calls into this method.
13561355 // Each thread needs its own runspace created to provide a separate environment for operations to run independently.
1357- // The runspace must be disposed when done and DefaultRunspace must be set/restored per-thread
1358- // to prevent cross-contamination when thread-pool threads are reused across tests.
13591356 Runspace runspace = RunspaceFactory . CreateRunspace ( ) ;
13601357 runspace . Open ( ) ;
13611358 runspace . SessionStateProxy . LanguageMode = PSLanguageMode . ConstrainedLanguage ;
13621359
1363- // Save and set the default runspace for the current thread to prevent
1364- // stale DefaultRunspace from a prior operation on this reused thread-pool thread.
1365- Runspace previousDefaultRunspace = Runspace . DefaultRunspace ;
1366- try
1360+ // Set the created runspace as the default for the current thread
1361+ //Runspace. DefaultRunspace = runspace;
1362+
1363+ using ( System . Management . Automation . PowerShell pwsh = System . Management . Automation . PowerShell . Create ( ) )
13671364 {
1368- Runspace . DefaultRunspace = runspace ;
1365+ pwsh . Runspace = runspace ;
13691366
1370- using ( System . Management . Automation . PowerShell pwsh = System . Management . Automation . PowerShell . Create ( ) )
1371- {
1372- pwsh . Runspace = runspace ;
1367+ var cmd = new Command (
1368+ command : contents ,
1369+ isScript : true ,
1370+ useLocalScope : true ) ;
1371+ cmd . MergeMyResults (
1372+ myResult : PipelineResultTypes . Error | PipelineResultTypes . Warning | PipelineResultTypes . Verbose | PipelineResultTypes . Debug | PipelineResultTypes . Information ,
1373+ toResult : PipelineResultTypes . Output ) ;
1374+ pwsh . Commands . AddCommand ( cmd ) ;
13731375
1374- var cmd = new Command (
1375- command : contents ,
1376- isScript : true ,
1377- useLocalScope : true ) ;
1378- cmd . MergeMyResults (
1379- myResult : PipelineResultTypes . Error | PipelineResultTypes . Warning | PipelineResultTypes . Verbose | PipelineResultTypes . Debug | PipelineResultTypes . Information ,
1380- toResult : PipelineResultTypes . Output ) ;
1381- pwsh . Commands . AddCommand ( cmd ) ;
13821376
1383- try
1384- {
1385- // Invoke the pipeline and retrieve the results
1386- var results = pwsh . Invoke ( ) ;
1377+ try
1378+ {
1379+ // Invoke the pipeline and retrieve the results
1380+ var results = pwsh . Invoke ( ) ;
13871381
1388- if ( results [ 0 ] is PSObject pwshObj )
1382+ if ( results [ 0 ] is PSObject pwshObj )
1383+ {
1384+ switch ( pwshObj . BaseObject )
13891385 {
1390- switch ( pwshObj . BaseObject )
1391- {
1392- case ErrorRecord err :
1393- //_cmdletPassedIn.WriteError(error);
1394- break ;
1386+ case ErrorRecord err :
1387+ //_cmdletPassedIn.WriteError(error);
1388+ break ;
13951389
1396- case WarningRecord warning :
1397- //cmdlet.WriteWarning(warning.Message);
1398- break ;
1390+ case WarningRecord warning :
1391+ //cmdlet.WriteWarning(warning.Message);
1392+ break ;
13991393
1400- case VerboseRecord verbose :
1401- //cmdlet.WriteVerbose(verbose.Message);
1402- break ;
1394+ case VerboseRecord verbose :
1395+ //cmdlet.WriteVerbose(verbose.Message);
1396+ break ;
14031397
1404- case DebugRecord debug :
1405- //cmdlet.WriteDebug(debug.Message);
1406- break ;
1398+ case DebugRecord debug :
1399+ //cmdlet.WriteDebug(debug.Message);
1400+ break ;
14071401
1408- case InformationRecord info :
1409- //cmdlet.WriteInformation(info);
1410- break ;
1402+ case InformationRecord info :
1403+ //cmdlet.WriteInformation(info);
1404+ break ;
14111405
1412- case Hashtable result :
1413- dataFileInfo = result ;
1414- return true ;
1415- }
1406+ case Hashtable result :
1407+ dataFileInfo = result ;
1408+ return true ;
14161409 }
14171410 }
1418- catch ( Exception ex )
1419- {
1420- error = ex ;
1421- }
14221411 }
1423- // Return false to indicate "we couldn't parse a valid Hashtable from this .psd1 file."
1424- // The only success path is the 'case Hashtable result' branch above which does return true.
1425- return false ;
1426- }
1427- finally
1428- {
1429- // Always restore the previous default runspace and close/dispose
1430- // the per-thread runspace, even on success (return true) or exception paths.
1431- Runspace . DefaultRunspace = previousDefaultRunspace ;
1432- runspace . Close ( ) ;
1433- runspace . Dispose ( ) ;
1412+ catch ( Exception ex )
1413+ {
1414+ error = ex ;
1415+ }
14341416 }
1417+ runspace . Close ( ) ;
1418+
1419+ return false ;
14351420 }
14361421 catch ( Exception ex )
14371422 {
0 commit comments