Powershell Script Service Check - powershell

I am trying to put together a script that I will be used to check and see if a service is running on the local machine. If it is then it will run an exe to launch a program. If it is not it needs to start the service then run the program after it starts. I think I am close just having issues if the service is not running.
If((Get-Service -Name ServiceName*).Status -eq 'Running')
{Invoke-Item "C:\Program Files (x86)\Application.exe"}
Else
{(Start-Service -Name ServiceName*) , (Invoke-Item "C:\Program Files (x86)\Application.exe")}
Any help would be greatly appreciated.

You could use Start-Process instead of Invoke-Item. The script below successfully works on my machine and launches a dummy exe that just shows a WPF window:
$serviceName = "service_name"
$pathToExe = "c:\path\to\file.exe"
if ( (Get-Service -Name $serviceName).Status -eq 'Running' ){
Start-Process $path
}else{
Try{
Start-Process -Name $serviceName -ErrorAction Stop
Start-Process $path
}catch{
Write-Host "Failed to start service!"
}
}
Let me know if that works for you :)

Well if I run this script as an Administrator through Powershell it actually works....

Related

While installing msi using PowerShell command Start-Process, getting Exit-code 1603 error

We are tying to install an MSI file on Windows servers using the following script and are able to install the MSI file in a Windows server. The Following code IS working fine for some MSI files, but it's failing for others. getting exit-code as 1603. If we do clean installation it works fine, but while trying to reinstall, we are getting an exit-code:1603 error. All the configuration settings are same for all services.
As mentioned on the Microsoft web site, we verified that following conditions and none are applied to our case.
Windows Installer is attempting to install an app that is already installed on your PC.
The folder that you are trying to install the Windows
Installer package to is encrypted.
The drive that contains the folder that you are trying to install the Windows Installer package to is accessed as a substitute drive.
The SYSTEM account does not have Full Control permissions on the folder that you are trying to install the Windows Installer package to. You notice the error message because the Windows Installer service uses the SYSTEM account to install software.
Code:
:outer for($i=1; $i -le $attempts; $i++) {
$timeout = $null
$proc = Start-Process -filePath $InstallerPath -ArgumentList $InstallCommand -PassThru
$proc | Wait-Process -Timeout $SecondsToWait -ea 0 -ev timeout
If (($timeout) -or ($proc.ExitCode -ne 0)) {
$proc | kill
$error = "`tFailed To Run $($ProcessTitle)Operations: Exit-Code = $($proc.ExitCode)"
If(($i+1) -le $attempts) {
WriteLog -Message($error) -MainLoggingConfigs $MainLoggingConfigs
Start-Sleep -s $WaitTimePerAttempt
}
Else {
throw $error
}
}
Else {
break outer
}
If using an MSI, you'll want to use Start-Process msiexec.exe -wait -NoNewWindow instead of Wait-Process . If you are really worried about it running forever, consider using PowerShell jobs:
Start-Job -Name MyInstall -scriptBlock {
Start-Process msiexec.exe -NoNewWindow -ArgumentList $MSIArguments
}
Wait-Job -Name MyInstall
Then check the job Get-Job MyInstall for output, status messages, state, errors, and especially child jobs.
The error you get may be due to competing installation attempts if your Start-Process creates child processes that haven't ended. Try out using something like Kevin Marquette's solution to save off the verbose MSI logs as well:
$MSI = 'C:\path\to\msi.msi'
$DateStamp = get-date -Format yyyyMMddTHHmmss
$logFile = "$MSI-$DateStamp.log"
$MSIArguments = #(
"/i"
"`"$MSI`""
"/qn"
"/norestart"
"/L*v"
$logFile
)
Start-Process "msiexec.exe" -ArgumentList $MSIArguments -Wait -NoNewWindow

How do you log the results of remotely invoked commands on the system running the PowerShell session?

I have a script that loops through a list of my servers, and runs various commands on them.
I am attempting to log the commands and their results for both the computer running the PowerShell script, and the remote computer running the commands.
Right now, I am trying to use the PSLogging module with the Start-Log cmdlet. I am able to log the commands run on the system running the Powershell script, but things run on the remote computer are not showing up.
My code looks like this.
Import-Module -Name "C:\Windows\System32\WindowsPowerShell\v1.0\Modules\PSLogging\2.5.2\PSLogging.psm1"
$Date = Get-Date -Format "MM-dd-yyyy hh-mm tt"
$LogFileOutput = "Output Log"+" $Date"+".log"
$LogFilePlusPath = "\\files\Logs\"+"$LogFileOutput"
Start-Log -LogPath "\\files\Logs\" -LogName "LogFileOutput" -ScriptVersion "1.0"
Servers = #("Server1","Server2")
ForEach ($Server in $Servers)
{
Write-Host "Running script on $Server"
param([string]$Server)
Invoke-Command -Computer $Server -ScriptBlock {
Write-Host "Stopping IIS App Pool on $Server."
c:\windows\system32\inetsrv\appcmd.exe stop apppool "IISAppPool"
Write-Host "Switching Service to manual on $Server and stopping it."
Stop-Service Service -Force
Set-Service Service-StartupType Manual
} -args $Server
}
Write-Host "Script complete at $(Get-Date -Format "hh:mm tt MM/dd/yyyy") Logs available at \\files\Logs"
Stop-Log
The script works, and the services and App pools perform their operation as desired. The results just aren't being logged.
The only thing in the log would be:
Running script on Server1
Running script on Server2
Script complete at 12:20 PM 09/19/2020 Logs available at \files\Logs
Any suggestions on the best way to capture everything?
Thank you!

Powershell is returning exit code too quickly

I have a script to uninstall McAfee antivirus and the agent associated with it.
The issue i'm having is that the script provides an exit code too early and doesn't continue through. If I run the script multiple times I get the desired result, but as we're trying to push it out via PDQ remotely, we need it to run through the script and only provide an exit code at the end of the script.
I'm a powershell novice so there's probably a much better and easier way to write this script but any advice would be greatly appreciated.
Start-Process -FilePath "msiexec.exe" -ArgumentList "/x {CE15D1B6-19B6-4D4D-8F43-CF5D2C3356FF} REMOVE=ALL REBOOT=R /q"; Write-Host "Uninstalling McAfee VirusScan Enterprise 8.8..."
$version = (Get-WmiObject -class Win32_OperatingSystem).Caption
Write-Host "Detected OS as $version"
if ($version -like '*Windows 7*')
{
Write-Host "Uninstalling McAfee Agent..."
Start-Process -FilePath "C:\Program Files (x86)\McAfee\Common Framework\frminst.exe" -ArgumentList "/forceuninstall"
}
elseif ($version -like '*Windows 10*')
{
Write-Host "Unmanaging McAfee Agent for Uninstall Process.."
Start-Process -FilePath "C:\Program Files\McAfee\Agent\maconfig.exe" -ArgumentList "/provision /unmanaged";
Write-Host "Uninstalling McAfee Agent..."
Start-Process -FilePath "C:\Program Files\McAfee\Agent\x86\frminst.exe" -ArgumentList "/forceuninstall"
}
else
{
exit
}
Start-Process reports a return code as soon as it starts the process indicating whether it was successful or not. Either use -wait to force the script to wait until it finishes or capture the output and proceed based on what the returnvalue is. See the docs for Start-Process

Cmdlets not found in command line but available in ISE

I'm trying to create an IIS application and app pool using PowerShell on a Windows Server 2008 R2 VM. The powershell script is as follows:
Param(
[string] $branchName,
[string] $sourceFolder
)
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script. `nPlease re-run this script as an Administrator."
Exit
}
$appPool = $branchName
$site = "Default Web Site"
#Add APPPool
New-WebAppPool -Name $appPool -Force
#Create Applications
New-WebApplication -Name $branchName -Site $site -PhysicalPath $sourceFolder - ApplicationPool $appPool -Force
If I run the script in the PowerShell ISE it works fine but if I run it from a command line (or a batch file using the command line) I get the error
The term New-WebAppPool is not recognized as the name of a cmdlet... etc.
Is there a way that the web administration cmdlets could be installed in the ISE but not the command line?
Assuming that you're using PowerShell v2 (the default version installed with Server 2008 R2) it's as #jisaak suspected: you need to import the module WebAdministration explicitly in your code:
Import-Module WebAdministration
ISE seems to do that automatically.
Another option would be to upgrade to PowerShell v4, which also automatically imports modules when one of their exported cmdlets is used.
For anyone interested, the answer was painfully simple. As suggested, the solution was to import the module, but there was an issue was that it wasn't available after closing the console and reopening it. To solve this I simply had to add the line into the powershell itself so it imports the module at every runtime.
Param(
[string] $branchName,
[string] $sourceFolder)
if (-NOT ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script.`nPlease re-run this script as an Administrator."
Exit
}
$appPool = $branchName
$site = "Default Web Site"
*Import-Module WebAdministration*
#Add APPPool
New-WebAppPool -Name $appPool -Force
#Create Applications
New-WebApplication -Name $branchName -Site $site -PhysicalPath $sourceFolder - ApplicationPool $appPool -Force

How do I run a Windows installer and get a succeed/fail value in PowerShell?

I would like to install a set of applications: .NET 4, IIS 7 PowerShell snap-ins, ASP.NET MVC 3, etc. How do I get the applications to install and return a value that determines if the installation was successful or not?
These answers all seem either overly complicated or not complete enough. Running an installer in the PowerShell console has a few problems. An MSI is run in the Windows subsystem, so you can't just invoke them (Invoke-Expression or &). Some people claim to get those commands to work by piping to Out-Null or Out-Host, but I have not observed that to work.
The method that works for me is Start-Process with the silent installation parameters to msiexec.
$list =
#(
"/I `"$msi`"", # Install this MSI
"/QN", # Quietly, without a UI
"/L*V `"$ENV:TEMP\$name.log`"" # Verbose output to this log
)
Start-Process -FilePath "msiexec" -ArgumentList $list -Wait
You can get the exit code from the Start-Process command and inspect it for pass/fail values. (and here is the exit code reference)
$p = Start-Process -FilePath "msiexec" -ArgumentList $list -Wait -PassThru
if($p.ExitCode -ne 0)
{
throw "Installation process returned error code: $($p.ExitCode)"
}
Depends. MSIs can be installed using WMI. For exes and other methods, you can use Start-Process and check the Process ExitCode.
msi's can also be installed using msiexec.exe, msu's can be installed using wusa.exe, both have a /quiet switch, /norestart and /forcerestart switches and a /log option for logging (specify the file name).
You can read more about the options if you call them with /?
Note: wusa fails silently when they fail, so you have to check the log file or eventlog to determine success.
I have implemented exactly what you are looking for at my current project. We need to automate deployment and instillation of n number of apps across multiple environments and datacenters. These scripts are slightly modified from the original version for simplicity sake since my complete code is reaching 1000 lines but the core functionality is intact. I hope this does what you are asking for.
This PS function pulls all the apps from the registry (what add/remove programs reads from) and then search's for the supplied app name and display version. In my code (PSM1) I run this function before I install to whether or not it is their and then afterword’s to verify that it got installed…. All this can be wrapped in one master function to manager flow control.
function Confirm-AppInstall{
param($AppName,$AppVersion)
$Apps = Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
$Apps += Get-ItemProperty Registry::HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\*|?{$_.DisplayName -ne $Null}|?{$_.DisplayName -ne ""}
$Installed = $Apps|?{$_.DisplayName -eq ""}|?{$_.DisplayVersion -eq ""}|select -First 1
if($Installed -ne $null){return $true}else{return $false}
}
This PS function will load a txt file that has the install commands prepopulated (one command per line). and run each line indivaduly and wait for the install to complete before moving on to the next.
function Install-Application{
param($InstallList = "C:\Install_Apps_CMDS.txt")
$list = gc -Path $InstallList
foreach ($Command in $list){
Write-Output ("[{0}]{1}" -f (Get-Date -Format G),$call)
#Make install process wait for exit before continuing.
$p = [diagnostics.process]::Start("powershell.exe","-NoProfile -NoLogo -Command $Command")
$p.WaitForExit()
Start-Sleep -Seconds 2
#Searches for the installer exe or msi that was directly opened by powershell and gets the process id.
$ProcessID = (gwmi -Query ("select ProcessId from Win32_Process WHERE ParentProcessID = {0} AND Name = '{1}'" -f $p.Id,$ParentProcessFile)|select ProcessId).ProcessId
#waits for the exe or msi to finish installing
while ( (Get-Process -Id $ProcessID -ea 0) -ne $null){
Start-Sleep -Seconds 2
$ElapsedTime = [int](New-TimeSpan -Start $P.StartTime -End (Get-Date)|select TotalSeconds).TotalSeconds
#install times out after 1000 seconds so it dosent just sit their forever this can be changed
if(2000 -lt $ElapsedTime){
Write-Output ('[{0}] The application "{1}" timed out during instilation and was forcfully exited after {2} seconds.' -f (Get-Date -Format G),$App.Name,(([int]$App.InstallTimeOut) * 60))
break
}
}
#clean up any old or hung install proccess that should not be running at this point.
Stop-Process -Name $ParentProcessName -ea 0 -Force
Stop-Process -Name msiexec -ea 0 -Force
}
}
The TXT file should be formatted as such... you will need to do you research on how to each app needs to be installed. a good resource is appdeploy.com
C:\Install.exe /q
C:\install.msi /qn TRANSFORMS='C:\transform.mst'
C:\install2.msi /qn /norestart
C:\install3.exe /quiet
Let me know if there are any errors I had to modify my existing code to remove the proprietary values and make this a little more simplistic. I am pulling my values from a custom XML answer sheet. But this code should work as I have supplied it.
If you would like for me to discuss more about my implementation let me know and i can make a more detailed explanation and also add more of the supporting functions that I have implemented.