@@ -185,9 +185,6 @@ private void InitializeRemoteSession()
185185 _logger . LogTrace ( "Initializing WinRM connection" ) ;
186186 try
187187 {
188- var pw = new NetworkCredential ( serverUserName , serverPassword ) . SecurePassword ;
189- PSCredential myCreds = new PSCredential ( serverUserName , pw ) ;
190-
191188 // Create the PSSessionOption object
192189 var sessionOption = new PSSessionOption
193190 {
@@ -197,8 +194,16 @@ private void InitializeRemoteSession()
197194 PS . AddCommand ( "New-PSSession" )
198195 . AddParameter ( "ComputerName" , ClientMachineName )
199196 . AddParameter ( "Port" , port )
200- . AddParameter ( "Credential" , myCreds )
201197 . AddParameter ( "SessionOption" , sessionOption ) ;
198+
199+ if ( ! string . IsNullOrEmpty ( serverUserName ) )
200+ {
201+ var pw = new NetworkCredential ( serverUserName , serverPassword ) . SecurePassword ;
202+ PSCredential myCreds = new PSCredential ( serverUserName , pw ) ;
203+
204+ PS . AddParameter ( "Credential" , myCreds ) ;
205+ }
206+
202207 }
203208 catch ( Exception )
204209 {
@@ -235,20 +240,19 @@ private void InitializeRemoteSession()
235240
236241 private void InitializeLocalSession ( )
237242 {
243+ _logger . LogTrace ( "Creating out-of-process Powershell Runspace." ) ;
244+ PowerShellProcessInstance psInstance = new PowerShellProcessInstance ( new Version ( 5 , 1 ) , null , null , false ) ;
245+ Runspace rs = RunspaceFactory . CreateOutOfProcessRunspace ( new TypeTable ( Array . Empty < string > ( ) ) , psInstance ) ;
246+ rs . Open ( ) ;
247+ PS . Runspace = rs ;
248+
238249 _logger . LogTrace ( "Setting Execution Policy to Unrestricted" ) ;
239250 PS . AddScript ( "Set-ExecutionPolicy Unrestricted -Scope Process -Force" ) ;
240251 PS . Invoke ( ) ; // Ensure the script is invoked and loaded
241252 CheckErrors ( ) ;
242253
243254 PS . Commands . Clear ( ) ; // Clear commands after loading functions
244255
245- // Trying this to get IISAdministration loaded!!
246- PowerShellProcessInstance psInstance = new PowerShellProcessInstance ( new Version ( 5 , 1 ) , null , null , false ) ;
247- Runspace rs = RunspaceFactory . CreateOutOfProcessRunspace ( new TypeTable ( Array . Empty < string > ( ) ) , psInstance ) ;
248- rs . Open ( ) ;
249-
250- PS . Runspace = rs ;
251-
252256 _logger . LogTrace ( "Setting script file into memory" ) ;
253257 PS . AddScript ( ". '" + scriptFileLocation + "'" ) ;
254258 PS . Invoke ( ) ; // Ensure the script is invoked and loaded
0 commit comments