Function to Import PortQry (a Systernal Utility)

Original version

function importPortQry{
# This function is currently imperfect because I have not yet figured out how to extract PortQryV2.exe with pure command-line, no GUI, and no confirmation. Security errors abound.


# Change these values to reflect your desired downloads
[string]$fileSource = "https://kimconnect.com/wp-content/uploads/2019/05/PortQry.zip";
$fileName="portqry.exe"
[string]$saveAs = "C:\Temp\$fileName";
$destination="C:\WINDOWS\System32\SysInternals\";

$fileExists=Test-Path $destination$fileName -PathType Leaf
"File exists: $fileExists"

if (!($fileExists)){
# Create temp folder to hold the downloads
New-Item -ItemType Directory -Force -Path C:\Temp | Out-Null

# Download
$source = "https://kimconnect.com/wp-content/uploads/2019/05/PortQry.zip";
$destination = "C:\Temp\PortQry.zip";
$download = (New-Object System.Net.WebClient).DownloadFile($source,$destination)

# Extract
$log="C:\Temp\kbLog.txt"
$extractFolder="C:\temp\PortQry\"
New-Item -ItemType Directory -Force -Path $extractFolder | Out-Null
expand-archive $destination $extractFolder

# Put the excutable in its expected directory
New-Item -ItemType Directory -Force -Path C:\WINDOWS\System32\SysInternals | Out-Null
cp "$extractFolder`PortQry.exe" C:\WINDOWS\System32\
}
else{"portqry.exe already exists at $saveAs.";}
}
importPortQry

Buggy version

function importPortQry{
# This function is currently imperfect because I have not yet figured out how to extract PortQryV2.exe with pure command-line, no GUI, and no confirmation. Security errors abound.


# Change these values to reflect your desired downloads
[string]$fileSource = "https://download.microsoft.com/download/0/d/9/0d9d81cf-4ef2-4aa5-8cea-95a935ee09c9/PortQryV2.exe";
$fileName="portqry.exe"
[string]$saveAs = "C:\Temp\$fileName";
$destination="C:\WINDOWS\System32\SysInternals\";

$fileExists=Test-Path $destination$fileName -PathType Leaf
"File exists: $fileExists"

if (!($fileExists)){
# Change this value to reflect your proxy node
$proxy="http://hqproxy:80";

# Check Proxy and configure when necessary
$proxyEnabled=(Get-ItemProperty -Path "Registry::HKCU\Software\Microsoft\Windows\CurrentVersion\Internet Settings").ProxyEnable
if ($proxyEnabled){
"Proxy Enabled: $proxyEnabled"
$PSDefaultParameterValues = @{"*:Proxy"="$proxy";}
"Setting proxy toward: $proxy"
}

# Check for the BitsTransfer module and import when necessary
if(!(Get-Module BitsTransfer)){
"Importing BitsTransfer Module...";
Import-Module BitsTransfer;
}

$download=Start-BitsTransfer -Source $fileSource -Destination $saveAs -Asynchronous;

While( ($download.JobState.ToString() -eq 'Transferring') -or ($download.JobState.ToString() -eq 'Connecting') )
{
$percent = [int](($download.BytesTransferred*100) / $download.BytesTotal)
Write-Progress -Activity "Downloading..." -CurrentOperation "$percent`% complete"
}
Complete-BitsTransfer -BitsJob $download

# Run the Install
C:\Temp\portqry.exe /Q /T:C:\Temp

# Most systems will not allow scripted extraction of files. Thus this manual workaround is used.
"Please click on the extract button with this default location C:\PortQryV2`nYou have 10 seconds to complete this task or this function must be repeated."

sleep 10

# Does't work as $zip.items = $null
#$path = Split-Path $saveAs
#pushd $path
#mv portqry.exe C:\Temp\portqry.zip
#$shell = new-object -com shell.application
#$zip = $shell.NameSpace('C:\Temp\portqry.zip')
#foreach($item in $zip.items()){
# $shell.Namespace('C:\Temp\').copyhere($item)
# }
#

# This commands gives an error
# start-process $saveAs -Argumentlist "/a"

# This causes error: New-Object : Exception calling ".ctor" with "3" argument(s): "End of Central Directory record could not be found."
# Perhaps, portqry.exe was formed differently than other archives. "notmyfault.exe" was generated when calling these commands
#$path = Split-Path $saveAs
#pushd $path
#mv portqry.exe portqry.zip
#Expand-Archive -Path portqry.zip
#popd

# Error: Exception calling "ExtractToDirectory" with "2" argument(s): "End of Central Directory record could not be found."
#$path = Split-Path $saveAs
#add-type -AssemblyName System.IO.Compression.FileSystem
#[system.io.compression.zipFile]::ExtractToDirectory($path)
#

}
else{"portqry.exe already exists at $saveAs.";}
cp C:\PortQryV2\PortQry.exe C:\WINDOWS\System32\SysInternals\
}
importPortQry

Leave a Reply

Your email address will not be published. Required fields are marked *