0

Running a script to install Powershell Native Images to boost performance. I would like to automate this across my entire environment, but if it's ran as a remote job, or scheduled task, or even psexec, it only installs three of the assemblies. If I run the script locally it get's all of them. Why? And suggestions on how to get this working remotely?

$transcript = "C:\temp\transcript.txt"
if(test-path $transcript){Remove-Item -Path $transcript}
Start-Transcript -Path $transcript

function Optimize-PowershellAssemblies {    
    $log = "C:\temp\log.txt"
    if(Test-Path $log){Remove-Item -Path $log}
  # NGEN powershell assembly, improves startup time of powershell by 10x
  $old_path = $env:path
  Add-Content $log "OLD_PATH: $($old_path)"
  try {
    $env:path = [Runtime.InteropServices.RuntimeEnvironment]::GetRuntimeDirectory()
    Add-Content $log "ENV:PATH: $($env:path)"
    $assemblies = [AppDomain]::CurrentDomain.GetAssemblies()
    Add-Content $log "ASSEMBLIES COUNT: ($($assemblies.count))"
    Add-Content $log "ASSEMBLIES: $($assemblies)"
      foreach($ass in $assemblies){
      if (! $ass.location) {continue}
      $index = $assemblies.IndexOf($ass)
      if ($ass.ManifestModule -like "Microsoft.PowerShell*"){
        Add-Content $log "NAME: $($ass.ManifestModule)"       
        try{
            $result = ngen install $ass.location | % {"`t$_"}
            Add-Content $log "RESULT: $($result)"
        }catch{Add-Content $log "ERROR: $($ass.ManifestModule) failed install. $($_)"}
      }
    }
  } catch{Add-Content $log "ERROR: Outside error. $($_)"}

  finally {
    $env:path = $old_path
  }

}
Optimize-PowershellAssemblies
Stop-Transcript

Log when ran manually:

OLD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Users\Administrator\AppData\Local\Microsoft\WindowsApps;
ENV:PATH: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
ASSEMBLIES COUNT: (69)
ASSEMBLIES: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 powershell_ise, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.PowerShell.ISECommon, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.GPowerShell, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.ComponentModel.Composition, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.Editor, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 WindowsBase, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationFramework, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Xaml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 UIAutomationProvider, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Runtime.Serialization, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Accessibility, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a MetadataViewProxies_82642edb-007e-4f89-ad9c-42025ac615d4, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationFramework.Aero2, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationFramework-SystemXml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Microsoft.PowerShell.Security, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.GraphicalHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 UIAutomationTypes, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationFramework-SystemData, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 PresentationFramework-SystemCore, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.GeneratedCode, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null Windows.UI, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Foundation, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime System.Runtime.InteropServices.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Runtime.WindowsRuntime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.Commands.Utility, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Microsoft.PowerShell.Commands.Management, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 SMDiagnostics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.ServiceModel.Internals, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.CSharp, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Dynamic, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Microsoft.WSMan.Management, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.WSMan.Runtime, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Security, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Data.SqlXml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Runtime, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Numerics.Vectors, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Windows.Graphics, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.System, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Devices, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Gaming, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.ApplicationModel, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Storage, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Web, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Media, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime Windows.Security, Version=255.255.255.255, Culture=neutral, PublicKeyToken=null, ContentType=WindowsRuntime System.Web.Extensions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Web, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Xml.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 PresentationFramework-SystemXmlLinq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.ServiceProcess, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Microsoft.PowerShell.ConsoleHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 PresentationFramework-SystemDrawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089
NAME: Microsoft.PowerShell.ISECommon.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.GPowerShell.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Editor.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Security.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.GraphicalHost.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Commands.Utility.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Commands.Management.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.ConsoleHost.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.

Log when ran from psexec or scheduled task:

OLD_PATH: C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\Amazon\cfn-bootstrap\;C:\Windows\system32\config\systemprofile\AppData\Local\Microsoft\WindowsApps
ENV:PATH: C:\Windows\Microsoft.NET\Framework64\v4.0.30319\
ASSEMBLIES COUNT: (17)
ASSEMBLIES: mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.ConsoleHost, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 Microsoft.Management.Infrastructure, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.DirectoryServices, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a System.Xml, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Numerics, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Data, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a Anonymously Hosted DynamicMethods Assembly, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null Microsoft.PowerShell.Security, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Transactions, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 Microsoft.PowerShell.Commands.Management, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35 System.Configuration.Install, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a
NAME: Microsoft.PowerShell.ConsoleHost.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Security.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
NAME: Microsoft.PowerShell.Commands.Management.dll
RESULT:     Microsoft (R) CLR Native Image Generator - Version 4.7.3190.0   Copyright (c) Microsoft Corporation.  All rights reserved.  All compilation targets are up to date.
barry
  • 17
  • 1
  • 2
  • 6
  • a few ideas ... [1] add `Start-Transcript` to the top of your script & point it to a file that the account in use can write to. [2] stop throwing away the error info in your `catch`. while inside the `catch`, the `$_` variable will have the details of any errors. [3] wrap each of your assembly loads in its own `try/catch` and log the full error for each instead of a generic error. – Lee_Dailey May 01 '20 at 16:22
  • Thanks. I've tried Start-Transcript. Nothing useful. I'll try your other suggestions. – barry May 01 '20 at 16:34
  • you are welcome ... and i wish you the best of good luck! [*grin*] – Lee_Dailey May 01 '20 at 16:47
  • No errors are produced. Looking at the log, it only grabs 17 of the assemblies when ran as psexec or sched task. Whereas if I run it manually it grabs 69. – barry May 01 '20 at 17:07
  • that likely means you have a permission problem that prevents the `17` account from seeing [or perhaps just loading] the code needed. have you checked the paths as seen in each method? can the `17` account see the locations that the `69` account can see? – Lee_Dailey May 01 '20 at 17:41

0 Answers0