PowerShell: Make an Installation Process Silent - powershell

I have a script that calls an application from Microsoft's Company Portal and automatically installs it. This launches Company Portal, potentially disrupting end user workflow. Is there any way to make this process run in the background? I tried to add an Argument List, but the process still launches Company portal. Any ideas? Thanks.
$OutputFile = "$env:WINDIR\TEMP\PythoncpInstall.log"
$Process = "companyportal:ApplicationId=e60a5520-dc39-4156-9223-825264cd5145"
$ProcessArgs = " /s -Wait -NoNewWindow"
##########ERROR LOGGING#####
Function Set-WriteToLog ($Write1)
{
Write-Host "$(Get-Date -format yyyy-MM-dd-hh-mm-ss)`t-`t$Write1"
}
#########START OF SCRIPT BODY#############
Start-Transcript -Path $OutputFile
start-process $Process -ArgumentList $ProcessArgs
sleep 10
[void][System.Reflection.Assembly]::LoadWithPartialName('System.Windows.Forms')
[System.Windows.Forms.SendKeys]::SendWait("^{i}")
Stop-Transcript

Without knowing more about the application and its installer, I'm not sure how if what I will suggest will work. Please share this information when you can.
What might help you is using Start-Process with -RedirectStandardInput and -WindowStyle.
-RedirectStandardInput will allow you to provide a text file that will be passed to standard input. This might let you provide input, depending on how the installer is implemented.
-WindowStyle would allow you to launch the process minimized or hidden.
Unfortunately, the way your code is currently written relies on SendKeys, which would in turn rely on the window being visible and focused.
Please let me know what installer you're using (if it's external), and I might be able to provide more help.

Related

Powershell - How to use Start-Process to call file from share/pass args in single line

To preface this, I am self teaching and brand new to scripting in general, let alone powershell.
After a cumulative 12 hours, my Google fu has run out.
I had a series of programs tailored to different models of computer we support that ran a staged series of installers from a fileshare. The program would check to see if the tech deploying the software was running it as admin, if not, it used a Start-Process line to elevate and run again.
It worked flawlessly, but we wanted to see if we could remove the need for the tech to enter r to run the scripts from the share.
In trying to figure out how to add -executionpolicy bypass to the arg list for Start-Process, I've hit a wall.
It now errors on trying to call to the fileshare to retrieve the parent script, before getting to the point where I can troubleshoot the bypass can of worms.
Below is my rough framework, remember I'm self taught by googling and using tutorials point.
$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator))
{
#usually I have a get-childitem | foreach-object loop here that runs the files from the folder one by one in a specific order,
#it also checks to see if msiexec is running or not before trying to load and install files using a if-else>do-while combo
}
else
{
Start-Process -FilePath "powershell" -ArgumentList "$('-File "\\server\dir\foo".ps1')$($MyInvocation.MyCommand.Name)$('""')" -Verb runAs
}#this calls to a script that is a 1:1 copy of the code in the if{} block
This returns an error from the -File parameter that says it can't call the file because it doesn't exist.
What am I doing wrong?
How do I pass -executionpolicy bypass as an additional arg without breaking it further?
Is there a better way to do this?
Is there a neater way to automate this?
Please help me geniuses of StackOverflow before I start gnawing on my keyboard.

How do I get a Powershell process that was opened by another Powershell process?

I am running multiple PowerShell scripts at once. I would like to be able to wait on certain ones to finish before opening new scripts. Basically, I was thinking if I could find the command line option that ran it something like "powershell.exe -Path "<script dir>" that would do it.
I tried doing a Get-Process | gm to find any parameters that I could call to get that information and I didn't see any (doesn't mean they aren't there) I tried looking through Task Manager to see if I could view something through the gui that I could link to but that didn't help either.
I hope I can get something like
Start-Process -FilePath ".\<script>.ps1" -ArgumentList "<args>"
do
{
sleep 10
}
until ((Get-Process -ProcessName "PowerShell" | where "<paramater>" -EQ ".\<script>")
I need to wait until that process is done but I don't want to put a wait at the end of the Start-Process because after that Start-Process kicks off I need some other items to go to while my .\ is running. I just need it to wait before another section of script kicks off.
Have a look at the "Job" cmdlets https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_jobs?view=powershell-6
And the $PID automatic variable, this will give the process ID of the current PowerShell session.

Launch PowerShell script into new window while passing variables

I've been using the site for a while, searching through the questions and answers, trying to map them to my scenario, but I'm either missing something, or what I'm looking to do isn't possible (at least the way I'm trying to do it), hence I'm hoping for a push in the right direction. Thanks in advance for reading.
I've been working on a fairly sizeable automation project. My main script performs a number of tasks, and generally works well, and reliably. At one stage of the script, I execute another PowerShell script, which was written by another team. I call the script as follows:
.\DeployMySQLProvider.ps1 -AzCredential $asdkCreds `
-VMLocalCredential $vmLocalAdminCreds `
-CloudAdminCredential $cloudAdminCreds
-PrivilegedEndpoint $ERCSip `
-DefaultSSLCertificatePassword $secureVMpwd -AcceptLicense
When I call it this way, from my main script, it works fine, however, this script uses and registers a DLL file during it's deployment, and locks it until the PowerShell window and session is closed. At the end of my main script, I have a cleanup phase, which can't complete it's job because of this locked DLL.
My thoughts therefore, were to launch the 2nd script into a new PowerShell window and session, either using Start-Process or Invoke-Expression, but I just can't seem to get either right. Most of the variables I'm passing through to the 2nd script aren't just strings, which is probably where I'm falling over. They are a mix of usernames and passwords (secure strings) along with $ERCSip which is a string.
Should I be looking at Start-Process / Invoke-Expression, or something else entirely? When I was testing with Start-Process, I had the following defined, but couldn't get the ArgumentList side working correcly for me (blank below):
Start-Process "$pshome\powershell.exe" -PassThru -Wait `
-Verb RunAs -ErrorAction Stop -ArgumentList ""
Any pointers in the right direction would be much appreciated.
Thanks!
I've used something similar to this in my scripting:
$scriptpath="c:\pathto\deploysqlProvider"
$a = "$scriptpath\DeployMySQLProvider.ps1 -AzCredential $asdkCreds `
-VMLocalCredential $vmLocalAdminCreds `
-CloudAdminCredential $cloudAdminCreds
-PrivilegedEndpoint $ERCSip ` "
-DefaultSSLCertificatePassword $secureVMpwd -AcceptLicense
Start-Process -Verb runas -FilePath powershell.exe -ArgumentList $a -wait -PassThru ;
Not sure if you need it to runas admin or not (-verb runas).
I'd suggest you then look for the Powershell process and path. So that if you have to kill this separate process you can.

extracting a WinRar file silently using powershell blocks with a GUI Pop-up asking Yes or Yes to All or No or No to All

I am extracting a WinRar file silently using power shell but a popup comes up asking Yes or Yes to All or No or No to All.
How to do it completely silent and if it is possible to extract in another folder ?
I have run below command.
Start-Process 'directory\jre-6u37-windows-i586.exe' -ArgumentList "/s" -Wait
For example, if I have a SFX Archived file in temp folder and when I run the powershell command below I get attached popup for confirmation. How can I make the command complete silently?
Start-Process 'C:\temp\test' -ArgumentList "/s" -Wait
The following dialog pops up:
Without a screenshot of the particular pop up, my guess is the popup you are referring to is the one from PowerShell and not from the process you are starting.
If that's the case its probably tripping your $confirmpreference setting.
You can try either setting $confirmpreference to "none" or you can try adding the parameter -confirm:$false to your cmdlet.

PowerShell - Respond to a Command response

I am trying to write a simple script that will execute a console command in Windows, which will always ask for a confirmation for overwriting the existing data. I thought something like the below would work, but it appears I was mistaken (and no surprise, write-host doesn't interact with the command line, I believe it simply puts text on screen). Can anyone provide some advice on how to handle this? I need to be able to execute the script through task scheduler on a weekly basis with no interaction.
Script:
Start-Process -NoNewWindow -FilePath pw -ArgumentList #"
threshold checkpoint create "WeeklyBackup" "WeeklyBackup"
"#
sleep -Seconds 3
$confirm = Select-String -pattern "About to over-write existing CheckPoint 'WeeklyBackup'." -Quiet
if ($confirm)
{
Write-Host "Y`r"
}
What I expect to see in the console:
D:\BMC_Software\ProactiveNet>pw threshold checkpoint create "WeeklyBackup" "Week
lyBackup"
About to over-write existing CheckPoint 'WeeklyBackup'. Do you want to proceed?
(y/n)
Then a user would hit Y and carriage-return and the process would be done. But I want this automated.
Thanks in advance for any advice.
echo "Y`r" | pw
This is typically used from batch files but it usually works just as well from PowerShell.