我正在尝试执行以下代码的PowerShell脚本:
function Invoke-InstallationOfANewBuild() {
param (
$PathToTheLocalFolderWhereBuildsAreHeld = "$($env:USERPROFILE)\Desktop\",
$PlaceOnANetworkDriveWhereBuildsAreHeld = "\\r\P\Al\OSystem\D B\20\x64"
)
begin {
Write-Verbose -Message "Searching for a build with the biggest CL number a in build name in local folder." -Verbose
$CheckClNumberOfABuildOnADesktop = Get-ChildItem $PathToTheLocalFolderWhereBuildsAreHeld -Filter *.exe | Where-Object Name -Like '*OSystemInstaller_20*' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1
Write-Verbose -Message "Searching for a build with the biggest CL number in a build name on a network drive." -Verbose
$CheckClNumberOfABuildOnANetworkDrive = Get-ChildItem $PlaceOnANetworkDriveWhereBuildsAreHeld -Filter *.exe | Where-Object Name -NotMatch '.*NoDB\.exe$' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1
Write-Verbose -Message "Comparison of two hash sums. Please, wait." -Verbose
if ($CheckClNumberOfABuildOnADesktop)
{
$GetHashOfFileWhichIsPlacedOnDesktop = Get-MyFileHash $CheckClNumberOfABuildOnADesktop -Algorithm MD5
$GetHashOfFileWhichIsPlacedOnNetworkDrive = Get-MyFileHash $CheckClNumberOfABuildOnANetworkDrive -Algorithm MD5
}
else {
Write-Verbose -Message "There are no O System 20-1 (dev branch) builds in specified local folder. Extracting hash of the newest build in the network folder..." -Verbose
$GetHashOfFileWhichIsPlacedOnNetworkDrive = Get-MyFileHash $CheckClNumberOfABuildOnANetworkDrive -Algorithm MD5
}
if ($GetHashOfFileWhichIsPlacedOnDesktop.MD5 -ne $GetHashOfFileWhichIsPlacedOnNetworkDrive.MD5)
{
Write-Verbose -Message "Hash sum of a file which is placed on the desktop and file in the network drive are different or there is no O System 20-1 build in specified local folder. The newest build will be copied from the network folder to to the local folder." -Verbose
}
else {
Write-Verbose -Message "Hash sum of a file which is placed on the desktop and a file on the network drive are the same. No need to copy anything." -Verbose
}
}
process {
if ($GetHashOfFileWhichIsPlacedOnDesktop.MD5 -eq $GetHashOfFileWhichIsPlacedOnNetworkDrive.MD5){
Write-Verbose -Message "Installation... Please, wait." -Verbose
Get-ChildItem $PathToTheLocalFolderWhereBuildsAreHeld -Filter *.exe | Where-Object Name -Like '*OSystemInstaller_20*' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1 | ForEach-Object { & $_ -s2 -sp"-SilentInstallation=standalone -UpdateMaterials=yestoall -UpgradeDBIfRequired=yes"}
}
else {
Write-Verbose -Message "The newest build doesn't exist in specified folder. Downloading, please wait." -Verbose
$SelectTheNewestBuildInFolder = Get-ChildItem $PlaceOnANetworkDriveWhereBuildsAreHeld -Filter *.exe | Where-Object Name -NotMatch '.*NoDB\.exe$' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1 | Copy-Item -Destination $PathToTheLocalFolderWhereBuildsAreHeld
}
$HashNumberOfCopiedBuild = Get-MyFileHash $SelectTheNewestBuildInFolder -Algorithm MD5
if ($HashNumberOfCopiedBuild.MD5 -eq $GetHashOfFileWhichIsPlacedOnNetworkDrive.MD5) {
Write-Verbose -Message "Hash sum of the copied file and hash sum of original file are the same. Builds are the same." -Verbose
Write-Verbose -Message "Installation... Please, wait." -Verbose
Get-ChildItem $PathToTheLocalFolderWhereBuildsAreHeld -Filter *.exe | Where-Object Name -Like '*OSystemInstaller*' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1 | ForEach-Object {& $_ -s2 -sp"-SilentInstallation=standalone -UpdateMaterials=yestoall -UpgradeDBIfRequired=yes"}
}
else {
Write-Verbose -Message "Hash sum of the copied file and hash sum of original file are different. Builds are the same." -Verbose
} # [Block moved]
}
}
Invoke-InstallationOfANewBuild
但是最后一个else语句中的if
语句工作两次(因此安装过程被调用两次)。如何将if语句放在else
语句中,以便只调用一次?实施例
1)如果true
中的值为else
而不是执行安装并停止脚本
2)如果false
中的值为else
,则移至if
中的else
并从那里执行安装。
答案 0 :(得分:0)
process {
if ($GetHashOfFileWhichIsPlacedOnDesktop.MD5 -ne $GetHashOfFileWhichIsPlacedOnNetworkDrive.MD5) {
Get-ChildItem $PlaceOnANetworkDriveWhereBuildsAreHeld -Filter *.exe | Where-Object Name -NotMatch '.*NoDB\.exe$' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1 | Copy-Item -Destination $PathToTheLocalFolderWhereBuildsAreHeld
}
$CheckClNumberOfABuildOnADesktop = Get-ChildItem $PathToTheLocalFolderWhereBuildsAreHeld -Filter *.exe | Where-Object Name -NotMatch '.*NoDB\.exe$' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1
$HashNumberOfTheCopiedBuild = Get-MyFileHash $CheckClNumberOfABuildOnADesktop -Algorithm MD5
if ($HashNumberOfTheCopiedBuild.MD5 -eq $GetHashOfFileWhichIsPlacedOnNetworkDrive.MD5) {
Write-Verbose -Message "Installation... Please, wait." -Verbose
Get-ChildItem $PathToTheLocalFolderWhereBuildsAreHeld -Filter *.exe | Where-Object Name -Like '*OrthoSystemInstaller*' | ForEach-Object {
New-Object psobject -Property @{
No = [int]([regex]::Match($_.Name, '(?<=CL)\d+').Value)
Name = $_.FullName
}
} | Sort-Object No -Descending | Select-Object -ExpandProperty Name -First 1 | ForEach-Object {& $_ -s2 -sp"-SilentInstallation=standalone -UpdateMaterials=yestoall -UpgradeDBIfRequired=yes"}
}
else {
Write-Verbose -Message "H" -Verbose
}
}
}
我所做的一切 - 从第一个if
移除安装并放到第二个if
。