B
B
Ba4ana2021-06-01 18:00:37
PowerShell
Ba4ana, 2021-06-01 18:00:37

PS script download and install BGInfo, simplify if(If)?

Good afternoon, comrades, there is a PS code for downloading and installing BGInfo from the site, it seems to me that there are a lot of ifs here to check the \BGInfo directory, to check the downloaded archive \BGInfo\BGInfo.zip, to check the bitness of the system.
If you do not check \BGInfo, the archive will not be downloaded. If you do not check the downloaded archive, it will be downloaded every time.

if(!(test-path "$adms\BGInfo")) {
  New-Item -Path "$adms\BGInfo" -ItemType Directory
  Invoke-WebRequest "https://download.sysinternals.com/files/BGInfo.zip" -outfile "$adms\BGInfo\BGInfo.zip"
  }
  Expand-Archive -path "$adms\BGInfo\BGInfo.zip" -destinationpath "$adms\BGInfo" -force
if ((Get-WmiObject win32_operatingsystem | select osarchitecture).osarchitecture -like "64*")
  {
  C:\Admins\Add\BGInfo\Bginfo64.exe C:\Admins\Add\BGInfo\PIT.bgi /silent /timer:00 /nolicprompt
  }
else
  {
  C:\Admins\Add\BGInfo\Bginfo.exe C:\Admins\Add\BGInfo\PIT.bgi /silent /timer:00 /nolicprompt
  }


Maybe you can somehow simplify it or do it differently, I will be glad to help or hint on the logic of the code)
Or suggest your own version

PS
I apologize for the style of the code

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
MaxKozlov, 2021-06-01
@MaxKozlov

So you still want to not download on a new one, or do you care?
You can download in $env:temp, then you don't have to check. Create a folder for unpacking with the
-Force key, then it will not swear at its presence
.

$bits = (Get-WmiObject win32_operatingsystem).OSArchitecture -replace '\D+'

and then substitute directly into the name through if or from a hash, for example, as you like
$filename = "C:\Admins\Add\BGInfo\Bginfo" + (if ($bits -eq '64) { '64' } else { '' } ) + ".exe"
$filename = "C:\Admins\Add\BGInfo\Bginfo" + @{'64'=64; '32'=32 }[$bits] + ".exe"

if (test-path $filename) .....

And you can also download from your network, where you can keep two archives at once for different bitness with the corresponding file inside and with the name bginfo32/bginfo64 to make it even easier
.

B
Ba4ana, 2021-06-23
@DckWB

Thank you, adopted

$bits = (Get-WmiObject win32_operatingsystem).OSArchitecture -replace '\D+'
$bginf = "$adms\BGInfo\BGInfo" + @{'64'=64; '32'=32}[$bits] + ".exe"
if (!(test-path $bginf))
{
New-Item -Path "$adms\BGInfo" -ItemType Directory
Invoke-WebRequest "https://download.sysinternals.com/files/BGInfo.zip" -outfile "$adms\BGInfo\BGInfo.zip"
Expand-Archive -path "$adms\Bginfo\BGInfo.zip" -destinationpath "$adms\BGInfo" -force
Remove-Item "$adms\BGInfo\BGInfo.zip" -force
}
& "$bginf" "$adms\BGInfo\PIT.bgi" /silent /timer:00 /nolicprompt

You set the $adms variable at your discretion
. What happens is that the folder is checked for the presence of an .exe file based on the bitness of the system, if the file is missing, then:
  • Directory is created
  • Downloading the archive from the site
  • Unpacked into this directory
  • The archive is deleted
  • And the utility with keys is launched

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question