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

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

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脚本的尝试:

  1. $regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
  2. $DisplayVersion = '21.1.21.45'
  3. $Notdone = $true
  4. DO {
  5. $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue
  6. if (($exists -ne $null) -and ($exists.Length -ne 0)) {
  7. & "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"
  8. $Notdone = $false
  9. }
  10. else {
  11. Start-Sleep -Seconds 15
  12. }
  13. } 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

  1. $regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
  2. $DisplayVersion = '21.1.21.45'
  3. $Notdone = $true
  4. DO {
  5. $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue
  6. if (($exists -ne $null) -and ($exists.Lenght -ne 0)) {
  7. & "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"
  8. $Notdone = $false
  9. }
  10. else {
  11. Start-Sleep -Seconds 15
  12. }
  13. } While ($Notdone)

答案1

得分: 1

你每隔15秒重新启动安装

  1. $regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
  2. $DisplayVersion = '21.1.21.45'
  3. $Notdone = $true
  4. 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
  5. DO {
  6. $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue
  7. if ([string]::IsNullOrEmpty($exists)){
  8. $Notdone = $true
  9. Start-Sleep -Seconds 15
  10. }else {$Notdone = $false}
  11. } While ($Notdone)
英文:

you restart the installation every 15 seconds

  1. $regkey = 'SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\{AA3C5F33-570B-3989-8542-D5DC0F759221}'
  2. $DisplayVersion = '21.1.21.45'
  3. $Notdone = $true
  4. 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
  5. DO {
  6. $exists = Get-ItemProperty -Path $regkey -Name $DisplayVersion -ErrorAction SilentlyContinue
  7. if ([string]::IsNullOrEmpty($exists)){
  8. $Notdone = $true
  9. Start-Sleep -Seconds 15
  10. }else {$Notdone = $false}
  11. } While ($Notdone)

答案2

得分: 0

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

  1. 这是对我有效的代码
  2. [Version]$minVersion = "21.1.21.45";
  3. do {
  4. $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 { }
  5. if ($version -ge $minVersion) {
  6. #Write-Host "The version is now '$($version)', which is greater than the Minimum version '$($minVersion)'";
  7. }
  8. else {
  9. #Write-Host "The version is now '$($version)', which is less than the Minimum version '$($minVersion)'. Will wait 15secs.";
  10. Start-Sleep -Seconds 15;
  11. }
  12. }
  13. until($version -ge $minVersion);

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

英文:

here is the code that worked for me

  1. [Version]$minVersion = "21.1.21.45";
  2. do {
  3.   $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 { }
  4.   if ($version -ge $minVersion) {
  5.   #Write-Host "The version is now '$($version)', which is greater than the Minimum version '$($minVersion)'";
  6.   }
  7.   else {
  8.   #Write-Host "The version is now '$($version)', which is less than the Minimum version '$($minVersion)'. Will wait 15secs.";
  9.   Start-Sleep -Seconds 15;
  10.   }
  11. }
  12. 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:

确定