Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 如果不满足条件,如何退出巧克力安装?_Windows_Powershell_Chocolatey - Fatal编程技术网

Windows 如果不满足条件,如何退出巧克力安装?

Windows 如果不满足条件,如何退出巧克力安装?,windows,powershell,chocolatey,Windows,Powershell,Chocolatey,在我通常的PowerShell脚本中,与往常一样,我在执行正常的健全性检查时使用Exit 1退出脚本,因此如果不满足某个条件,我会提前退出并通知用户满足该条件 chocolatepackage开发是否遵循同样的时尚方式?或者是否有任何chocolate方式,例如helpers或core extension为开发人员提供这种功能?例如: 我希望用户为我的SQL Server巧克力包--params”/IsoPath:C:\Pkg“提供SQL Server iso路径,如果未提供路径,包安装应失败:

在我通常的
PowerShell
脚本中,与往常一样,我在执行正常的健全性检查时使用
Exit 1
退出脚本,因此如果不满足某个条件,我会提前退出并通知用户满足该条件

chocolate
package开发是否遵循同样的时尚方式?或者是否有任何
chocolate
方式,例如
helpers
core extension
为开发人员提供这种功能?例如:

我希望用户为我的
SQL Server
巧克力包
--params”/IsoPath:C:\Pkg“
提供
SQL Server iso
路径,如果未提供路径,包安装应失败:

$pp = Get-PackageParameters
if (!pp['IsoPath']) {
   Write-Host "No 'ISO' path is provided, please provide path in '--params'"
   Exit 1
}

上面的代码片段是否会导致安装失败,或者是否有任何巧克力般的方法可以做到这一点?

有几种方法可以做到这一点

第一种方法是简单地抛出一个异常。包中就是一个很好的例子,它决定这个包是否打算在当前安装的操作系统上使用

$os = Get-WmiObject -Class Win32_OperatingSystem
$version = [Version]$os.Version
if ($version -ge [Version]'6.1' -and $version -lt [Version]'6.2' -and 
$os.ServicePackMajorVersion -lt 1)
{
  # On Windows 7 / Server 2008 R2, Service Pack 1 is required.
  throw 'This package requires Service Pack 1 to be installed first. The 
"KB976932" package may be used to install it.'
}
elseif ($version -ge [Version]'6.0' -and $version -lt [Version]'6.1' -and 
$os.ServicePackMajorVersion -lt 2)
{
  # On Windows Vista / Server 2008, Service Pack 2 is required.
  throw 'This package requires Service Pack 2 to be installed first.'
}
elseif ($version -ge [Version]'5.2' -and $version -lt [Version]'6.0' -and 
$os.ServicePackMajorVersion -lt 2)
{
  # On Windows Server 2003 / XP x64, Service Pack 2 is required.
  throw 'This package requires Service Pack 2 to be installed first.'
}
elseif ($version -ge [Version]'5.1' -and $version -lt [Version]'5.2' -and 
$os.ServicePackMajorVersion -lt 3)
{
  # On Windows XP, Service Pack 3 is required.
  throw 'This package requires Service Pack 3 to be installed first.'
}
另一种方法是使用Chocolate提供的
Set-PowerShellExitCode
helper方法:


如果您需要将退出代码专门设置为以后使用的代码。

谢谢Gary,
set PowerShellExitCode
是我一直在寻找的。