Hi everyone, I’m a Powershell newbie. I have prepared this script for installing Oracle Client starting from examples taken on the internet. Everything works as it should but I was wondering if it was possible to optimize and write it in a more elegant and simplified way.
Thank you
G.
Remove-Variable * -ErrorAction SilentlyContinue; Remove-Module *; $error.Clear(); Clear-Host
Add-Type -AssemblyName System.Windows.Forms
#the script exits if run on a server
if ($MachineName -like ("SRV-")) {
exit
}
#kill the "ICON.exe" process which prevents the installation of the Oracle Client
if((get-process "icone" -ea SilentlyContinue) -eq $Null){
echo "Not Running"
}
else{
echo "Running"
Stop-Process -processname "icone"
}
#Delete registry Key
if (Test-path "HKLM:\SOFTWARE\oracle")
{
Remove-Item "HKLM:\SOFTWARE\oracle"
}
if (Test-path "HKLM:\SOFTWARE\WOW6432Node\oracle")
{
Remove-Item "HKLM:\SOFTWARE\WOW6432Node\oracle"
}
echo "Registry Key Deleted"
#VCRedist Install
Start-Process -FilePath "$PSScriptRoot\VC_redist.x64.exe" -ArgumentList “/q /norestart” -Wait
Echo "Install VCRedistx64"
Start-Process -FilePath "$PSScriptRoot\VC_redist.x86.exe" -ArgumentList “/q /norestart” -Wait
echo "Install vcredistx86"
#import registry Key
Invoke-Command {reg import "$PSScriptRoot\ODBC_Nuovi_e_Precedenti.reg" *>&1 | Out-Null}
Echo "Import Registry file"
#delete folder x86
$TRGFolderNamex86 = "C:\Program Files (x86)\Oracle32"
$SRCFolderNamex86 = "$PSScriptRoot\Oracle32"
if (Test-Path $TRGFolderNamex86) {
Write-Host "Folder Exists"
Remove-Item -Recurse -Force "C:\Program Files (x86)\Oracle32"
Copy-Item $SRCFolderNamex86 $TRGFolderNamex86 -Force -Recurse
}
else
{
Write-Host "Folder Doesn't Exists"
Copy-Item $SRCFolderNamex86 $TRGFolderNamex86 -Force -Recurse
}
Echo "Delete Folder x86"
#delete folder x64
$TRGFolderNamex64 = "C:\Program Files\Oracle64"
$SRCFolderNamex64 = "$PSScriptRoot\Oracle64"
if (Test-Path $TRGFolderNamex64) {
Write-Host "Folder Exists"
Remove-Item -Recurse -Force "C:\Program Files\Oracle64"
Copy-Item $SRCFolderNamex64 $TRGFolderNamex64 -Force -Recurse
}
else
{
Write-Host "Folder Doesn't Exists"
Copy-Item $SRCFolderNamex64 $TRGFolderNamex64 -Force -Recurse
}
Echo "Delete Folder x64"
#Change Environment path
#remove old key in environment path
$path = [System.Environment]::GetEnvironmentVariable(
'PATH',
'Machine' # For user variable
#'Machine' for system variable
)
$path = ($path.Split(';') | Where-Object { $_ -ne 'U:\Oracle32\product\11.2.0.3\client_32\BIN' }) -join ';'
$path = ($path.Split(';') | Where-Object { $_ -ne 'U:\Oracle64\product\11.2.0.4\client64_home1\BIN' }) -join ';'
# Set it
[System.Environment]::SetEnvironmentVariable(
'PATH',
$path,
'Machine' # For user variable
#'Machine' for system variable
)
Echo "removed old key in environment path"
#Add new key in environment path
[Environment]::SetEnvironmentVariable("PATH", $Env:PATH + "C:\Program Files\Oracle64\instantclient_19;C:\Program Files (x86)\Oracle32\instantclient_19", [EnvironmentVariableTarget]::Machine)
Echo "Add new key in environment path"
#ADD TNSADMIN Environment KEY
#[System.Environment]::SetEnvironmentVariable('TNS_ADMIN','H:\TNS_ADMIN')
[Environment]::SetEnvironmentVariable("TNS_ADMIN", "H:\TNS_ADMIN", "Machine")
Echo "ADD TNSADMIN Environment KEY"
Echo "Reboot"