首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用PowerShell更改安装的windows映像中的Env:Path

使用PowerShell更改安装的windows映像中的Env:Path
EN

Stack Overflow用户
提问于 2022-11-21 02:05:44
回答 2查看 36关注 0票数 0

我编写了一个PowerShell脚本来创建一个自定义Boot.wim文件,其中包含一些有用的实用程序和自定义.ps1脚本以及PowerShell可执行文件。这一切都是应该做的。我无法理解的是,如何向环境路径变量中添加实用程序“x:\Users\U实用程序”到挂载图像的目录。

下面是程序中的一个代码片段,它展示了我如何操纵挂载的图像来添加实用程序等等。

代码语言:javascript
复制
$WinADK="C:\Program Files (x86)\Windows Kits\10\" +
        "Assessment and Deployment Kit" +
        "\Windows Preinstallation Environment\" +
        "$(Get-ArchitectureString)"
    
$WinPETemp='G:\TempPE'

New-Item -ItemType Directory -Path "$($WinPETemp)\Media" -force
$CIArgs = @{path        = $(Join-Path $WinAdk -ChildPath "en-us\winpe.wim")
            Destination = "$($WinPETemp)\Media\boot.wim"}
Copy-Item @CIArgs
New-Item -ItemType Directory -Path $(Join-Path $WinPETemp -ChildPath "\Mount") –Force

#----- Mount the WinPE WIM file for Editing -----

$MTArgs = @{ImagePath = $(Join-Path $($WinPETemp) -ChildPath "\Media\boot.wim")
            Index     = 1 
            path      = $(Join-Path $($WinPETemp) -ChildPath "\Mount")
           }

Try { Mount-WindowsImage @MTArgs -ErrorAction Stop}
Catch { 
        Get-WindowsImage -mounted | 
          Dismount-Windowsimage -discard
        Mount-WindowsImage @MTArgs
}
#--- Packages List - PowerShell, Storage, and DISM cmdlets --#
$PkgList = @(
    "WinPE-WMI.cab", 
    "en-us\WinPE-WMI_en-us.cab", 
    "WinPE-NetFx.cab", 
    "en-us\WinPE-NetFx_en-us.cab", 
    "WinPE-Scripting.cab", 
    "en-us\WinPE-Scripting_en-us.cab", 
    "WinPE-PowerShell.cab", 
    "en-us\WinPE-PowerShell_en-us.cab", 
    "WinPE-DismCmdlets.cab", 
    "en-us\WinPE-DismCmdlets_en-us.cab", 
    "WinPE-EnhancedStorage.cab", 
    "en-us\WinPE-EnhancedStorage_en-us.cab", 
    "WinPE-StorageWMI.cab", 
    "en-us\WinPE-StorageWMI_en-us.cab") 

 ForEach ($Pkg in $PkgList) {
 $AWPArgs = @{PackagePath = "$($WinAdk)\Winpe_OCS\$($Pkg)"
              Path        = "$($WinPETemp)\Mount"
              IgnoreCheck = $true }

 Add-WindowsPackage @AWPArgs 

 } #End ForEach ($Pkg...

<#
  Add your modifications now...
  Such as Directory for User Scripts, 
  Registry Modifications, specifically Powershell 
  Execution Policy
#>

#--- Add User Directories ---
$UserDirectories = @("Utilities","Scripts")

ForEach ($Dir in $UserDirectories) {
  $NIArgs = @{ItemType = 'Directory' 
              Path     = "$($WinPETemp)\Mount\Users\$($Dir)"}
  New-Item @NIArgs
}

#--- Add PowerShell Profile ---
$CIArgs = @{Path        = 
 "G:\WinPE Include Files\Microsoft.PowerShell_profile.ps1"
            Destination = 
 "$($WinPETemp)\Mount\Windows\System32\WindowsPowerShell\V1.0"}

 Copy-Item @CIArgs

#--- Add User Scripts to Directory ---

$Scripts = @("Get-DotNetVersionsInstalled.ps1")

ForEach ($FN in $Scripts) {
  $CIArgs = @{Path        = "G:\BEKDocs\Scripts\Production\$FN" 
              Destination = "$($WinPETemp)\Mount\Users\Scripts"}
  Copy-Item @CIArgs
}

#--- Add Portable Pgms to the Utilities Directory     ---
#--- Note: Must be 64 bit Programs no support for WOW ---

$Utilities = @("Speccy64\Speccy64.exe",
               "HWiNFO64\HWiNFO64.exe",
               "NirSoftx64\*.*")

ForEach ($FN in $Utilities) {
  $CIArgs = @{Path        = "G:\BEKDocs\NonInstPrograms\$FN" 
              Destination = "$($WinPETemp)\Mount\Users\Utilities"
              Recurse     = $True
             }

  If (Test-Path -Path "$(($CIArgs).Path)" ) {
    Copy-Item @CIArgs
  }
  Else {
         "Error:" + $CIArgs.Path + " Does NOT Exist!"
  }

}

#--- Mount WinPE Registry to the Local Registry for mods ---
$RegLoc = "$($WinPETemp)\Mount\Windows\System32\config\software"
reg load "HKLM\WimPE" "$RegLoc"

#--- Modify PowerShell Execution Policy in WIM Registry ---
$RegPath = "HKLM:\WimPE\Microsoft\PowerShell\1\ShellIds" +
           "\Microsoft.PowerShell\"
$NIArgs = @{Path  = $RegPath 
            Name  = 'ExecutionPolicy' 
            Value = "RemoteSigned"
            PropertyType = 'String'
            Force = $True}

New-ItemProperty @NIArgs

reg unload "HKLM\WimPE"  #--- UnMount WinPE registry ---

#----- DisMount the Modified WinPE WIM File -----

Dismount-WindowsImage -path "$($WinPETemp)\Mount" -Save

#----- Make a Backup of the Modified WIM file -----

$Destination='G:\Pewim'
New-Item -Path $Destination -ItemType Directory –Force

$CIArgs = @{Path        = "$($WinPETemp)\Media\boot.wim" 
            Destination = "$($Destination)\"}
Copy-Item @CIArgs
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2022-11-22 04:31:32

您可以在HKLM:\SYSTEM\<ControlSet>\Control\Session Manager\Environment将其放入图像的注册表中。

"ControlSet“通常是001和002。但是,不要使用CurrentControlSet,因为它不能用于脱机映像。

票数 0
EN

Stack Overflow用户

发布于 2022-11-23 01:12:23

这个答案只是为了显示我写的代码,供其他搜索者参考,这是拉尔夫的好建议。一切都在按要求工作!

代码语言:javascript
复制
#--- Reset to load HKLM\System to add item to Path ---
$RegLoc = "G:\TempPE\Mount\Windows\System32\config\System"
reg load "HKLM\WimPE" "$RegLoc"
 
$RegPath = "HKLM:\WimPE\ControlSet001\Control\" +
           "Session Manager\Environment"

$GIArgs = @{Path  = "$RegPath" 
            Name  = 'Path' 
           }
$CurPath = (Get-ItemProperty @GIArgs).Path

$Curpath = "$CurPath;X:\Users\Utilities"

$SIArgs = @{Path  = "$RegPath"
            Name  = "Path"
            Value = "$CurPath"}
Set-ItemProperty @SIArgs

reg unload "HKLM\WimPE"  #--- UnMount WinPE registry ---

上述代码在卸载问题中显示的代码中的"HKLM\WimPe“后立即开始。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74513506

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档