Отличный результат от сценария, запущенного как запланированное - PullRequest
0 голосов
/ 01 мая 2020

Запуск скрипта для установки Powershell Native Images для повышения производительности. Я хотел бы автоматизировать это во всей моей среде, но если он запускается как удаленное задание, или запланированная задача, или даже psexe c, он устанавливает только три сборки. Если я запускаю скрипт локально, он получит их все. Почему? И предложения о том, как заставить это работать удаленно?

$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

Журнал при запуске вручную:

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.

Журнал при запуске из psexe c или запланированная задача:

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.
...