在PowerShell中如何执行Windows注册表等待循环

huangapple go评论47阅读模式
英文:

How do I do a windows registry wait loop in PowerShell

问题

Intune给我带来了麻烦,我尝试使用Powershell安装一个程序,通常只需要在最后加上 -wait,脚本就会等待安装完成然后关闭,但是在Intune中它会挂起。

所以我想要做的是创建一个等待循环,以便在创建这个Windows注册表时:

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{AA3C5F33-570B-3989-8542-D5DC0F759221}

并且这个名称字段等于这个数字:

DisplayVersion = 21.1.21.45

以下是PowerShell脚本的尝试:

$regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
$DisplayVersion = '21.1.21.45'
$Notdone = $true

DO {
    $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue

    if (($exists -ne $null) -and ($exists.Length -ne 0)) {
        & "C:\Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\installer.exe" -ArgumentList "-i deploy --offline_mode -o C:/Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\Collection.xml --installer_version 1.35.0.9"

        $Notdone = $false
    }
    else {
        Start-Sleep -Seconds 15
    }
} While ($Notdone)

希望这能帮助你解决问题。

英文:

Intune is giving me the S**t's I am trying to install a program with Powershell and usually, you just put -wait at the end and the scrips wait's until the install is done and then closes but with intune it just hangs.

so what I want to do is put in a wait loop so that when this windows registry is created

Computer\HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall{AA3C5F33-570B-3989-8542-D5DC0F759221}

With this name field equaling this number
DisplayVersion = 21.1.21.45

Below is attempt at the PowerShell script

$regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
$DisplayVersion = '21.1.21.45'
$Notdone = $true

DO {
    $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue

    if (($exists -ne $null) -and ($exists.Lenght -ne 0)) {
        & "C:\Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\installer.exe" -ArgumentList "-i deploy --offline_mode -o C:/Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\Collection.xml --installer_version 1.35.0.9"

    $Notdone = $false
    }
    else {
    Start-Sleep -Seconds 15
    }
} While ($Notdone)

答案1

得分: 1

你每隔15秒重新启动安装

$regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
$DisplayVersion = '21.1.21.45'
$Notdone = $true

Start-Process "C:\Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\installer.exe" -ArgumentList "-i deploy --offline_mode -o C:/Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\Collection.xml --installer_version 1.35.0.9" -Wait -NoNewWindow

DO {
    $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue

    if ([string]::IsNullOrEmpty($exists)){
    
        $Notdone = $true
        Start-Sleep -Seconds 15
    }else {$Notdone = $false}

} While ($Notdone)
英文:

you restart the installation every 15 seconds

$regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
$DisplayVersion = '21.1.21.45'
$Notdone = $true

Start-Process "C:\Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\installer.exe" -ArgumentList "-i deploy --offline_mode -o C:/Autodesk\{D138465E-4098-4F01-B453-1C4BEADE75D8}\image\Collection.xml --installer_version 1.35.0.9" -Wait -NoNewWindow

DO {
    $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue

    if ([string]::IsNullOrEmpty($exists)){
    
        $Notdone = $true
        Start-Sleep -Seconds 15
    }else {$Notdone = $false}

} While ($Notdone)

答案2

得分: 0

以下是对您提供的代码的翻译部分:

这是对我有效的代码

    [Version]$minVersion = "21.1.21.45";
    do {
        $version = [Version]'0.0.0.0'; try { [Version]$version = [Version](Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}' -EA 0).DisplayVersion; } catch { }
        if ($version -ge $minVersion) {
            #Write-Host "The version is now '$($version)', which is greater than the Minimum version '$($minVersion)'";
        }
        else {
            #Write-Host "The version is now '$($version)', which is less than the Minimum version '$($minVersion)'. Will wait 15secs.";
            Start-Sleep -Seconds 15;
        }
    }
    until($version -ge $minVersion);

请注意,这只是代码的翻译部分,不包括注释和注释内的文本。

英文:

here is the code that worked for me

[Version]$minVersion = "21.1.21.45";
do {
  $version = [Version]'0.0.0.0'; try { [Version]$version = [Version](Get-ItemProperty -Path 'HKLM:\Software\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}' -EA 0).DisplayVersion; } catch { }
  if ($version -ge $minVersion) {
  #Write-Host "The version is now '$($version)', which is greater than the Minimum version '$($minVersion)'";
  }
  else {
  #Write-Host "The version is now '$($version)', which is less than the Minimum version '$($minVersion)'. Will wait 15secs.";
  Start-Sleep -Seconds 15;
  }
}
until($version -ge $minVersion);

huangapple
  • 本文由 发表于 2023年2月10日 07:30:39
  • 转载请务必保留本文链接:https://go.coder-hub.com/75405526.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定