我有一个脚本,它将ISO复制到C:\temp,然后挂载ISO,并将驱动器号存储在一个变量中,用于在挂载的驱动器上执行文件。我是这样做的:
$mountinfo = Mount-DiskImage -ImagePath $ctempiso
$driveletter = (($mountinfo | Get-volume).driveletter)
$installpath = "${driveletter}:\setup.exe"
$scriptblock = [scriptblock]::create($installpath)
Invoke-Command -ScriptBlock $scriptblock如果我手动将“执行策略”设置为“无限制”,然后执行我的PowerShell脚本,则所有这些都可以正常工作。
为了避免设置执行策略,我在以管理员身份运行的批处理文件中使用了以下内容:
PowerShell.exe -ExecutionPolicy Bypass -File \\server.contoso.local\share\test.ps1当我运行批处理文件时,它无法存储驱动器号,并返回无法执行${driveletter}:\setup.exe的错误
我猜我的第一个问题是为什么我的脚本在第一个方法中工作,而在第二个方法中不起作用?
其次,有没有其他方法可以绕过执行策略,让我的脚本像我想要的那样运行?
发布于 2021-08-10 08:59:17
我找到问题了..。魔术单词是"-passthru",它在我的代码中缺失了。
$mountinfo = Mount-DiskImage -ImagePath $ctempiso -Passthru
$driveletter = (($mountinfo | Get-volume).driveletter)
$installpath = "${driveletter}:\setup.exe"
$scriptblock = [scriptblock]::create($installpath)
Invoke-Command -ScriptBlock $scriptblock https://stackoverflow.com/questions/68544695
复制相似问题