通过 ISO 内的 powershell 安装 O365

通过 ISO 内的 powershell 安装 O365

我创建了一个 Office 365 安装程序,它进行本地安装并动态更改 SourcePath,我需要从 ISO 文件运行它(我通常使用 USB,但在 VM 中我使用 ISO)

在任何目录或 USB 上本地运行它都可以完美运行,但是从 ISO 运行它却不行,并且出现错误:

Set-Content : Access to path 'C:\Users\Administrator\AppData\Local\Temp\tmpoffice\configuration.xml' was denied. No E:\SMS\PKG\CM10017B\InstallOffice_OfflineMode.ps1:24 character:164
+ ... fficeMgmtCOM="TRUE" SourcePath="'+$PS1dirEOL) | Set-Content $tempconf
+ ~~~~~~~~~~~~~~~~~~~~~
     + CategoryInfo : NotSpecified: (:) [Set-Content], UnauthorizedAccessException
     + FullyQualifiedErrorId : System.UnauthorizedAccessException,Microsoft.PowerShell.Commands.SetContentCommand

我如何才能让它在 ISO 中也能工作?我知道 ISO 是只读的,但我觉得很奇怪,他试图修改 ISO 中没有的内容,而是在临时目录中,却仍然无法修改。

$PS1dir = Get-Location

#Paths of the configuration
$tempdir = "$env:TEMP\tmpoffice"
$conf = "$($PS1dir)\configuration.xml"
$tempconf = "$env:TEMP\tmpoffice\configuration.xml"

#Current path with reformated end of XML line
$PS1dirEOL = "$($PS1dir)`" `AllowCdnFallback=`"TRUE`">"

#Copy configuration file for temp folder and set variable for same
Copy-Item $conf -Destination (New-Item -Path $tempdir -Type Directory -Force) -Recurse -Force

#Replace old line with the current folder
(Get-Content $tempconf) -replace '<Add OfficeClientEdition=.*', ('<Add OfficeClientEdition="64" Channel="Current" OfficeMgmtCOM="TRUE" SourcePath="'+$PS1dirEOL) | Set-Content $tempconf

#Running O365 installation from new configuration file
Start-Process cmd.exe -ArgumentList "/c start /MIN $($PS1dir)\setup.exe /configure $tempconf" -Wait

Remove-Item -Path $tempdir -Force -Recurse

答案1

问题是,该文件是只读的。.iso 映像上的所有文件都具有只读属性,复制文件时会保留该属性。您需要先删除它,然后才能编辑该文件。

Set-ItemProperty $tempconf -Name IsReadOnly -Value $false

但这不是唯一的问题。删除 ReadOnly 属性后,您将遇到下一个错误,告诉您无法写入文件,因为它正在被另一个进程使用(因为Get-Content仍然处于活动状态)。您需要使用临时变量。

$conf = (Get-Content $tempconf) -replace '<Add OfficeClientEdition=.*', ('<Add OfficeClientEdition="64" Channel="Current" OfficeMgmtCOM="TRUE" SourcePath="'+$PS1dirEOL)
$conf | Set-Content $tempconf

相关内容