PowerShell won't terminate hung process - powershell

I have a scenario where a process is stuck every single Monday morning because of an Oracle database so I tried creating a PowerShell script to run every Monday but regardless of getting an error or not, the process remains.
The line I'm attempting to use for the "kill" is:
Get-Process -Name ez0* -ComputerName $server | Stop-Process -Force
Tried doing this locally as well without the -ComputerName.
I'm not getting any errors from this line with or without the -Force it just executes and moves on.
Just doing Get-Process works and I can see it but I can't end it with PowerShell. After many attempts I remotely logged on to the server and just right-clicked the process and chose "End task" which worked just fine.
It is an odd process because it's one out of initial 8 (based on cores) and when you stop the service, all but one of the processes is removed save for the one that is hung.

Try using:
$termproc = (get-wmiobject -ComputerName $server -Class Win32_Process -Filter "name like 'ez0%'"
$termproc.terminate()
You could also just do the below if you don't want to check the processes in the variable first.
(get-wmiobject -ComputerName $server -Class Win32_Process -Filter "name like 'ez0%'").terminate()
Thanks, Tim.

Related

PowerShell Stopping and Starting Windows Services on Remote Computer

I have a very basic PowerShell that accepts a list of Windows Server and Services on them and then stops the Services
Get-Service -ComputerName machine1 -Name service1 -ErrorAction Stop | Stop-Service -WarningAction SilentlyContinue
Although I had explicitly mentioned -ErrorAction as Stop, in a case if one of the Server is not reachable, the Powershell doesn't come back. How do I change this behaviour at least in a way to stop processing after a certain amount of time as "n" secs which again can be passed thru parameter?
Incorporating both comments, we end up with following
Enclose the current script in a Test-Connection that allows you to first verify if a computer is actually reachable:
if (Test-Connection -ComputerName machine1 -Quiet)
{
all your current code
}

Powershell: waiting for changed directory

To make it short, I want to connect to a server that is running Virtual Machines and then get a List of all installed machines, the command I use for this is:
Invoke-Command -ScriptBlock {enter-pssession -ComputerName <name>}; Invoke-Command -ScriptBlock {Get-VM} | select-Object -Property name
This line contains two commands at first:
Invoke-Command -ScriptBlock {enter-pssession -ComputerName <name>};
this part connects to the server, and then:
Invoke-Command -ScriptBlock {Get-VM} | select-Object -Property name
This command gets a list of the VMs currently on the server and returns specific properties of these servers.
However, because the connection needs a short time until it is set up, the "get-vm" command is still set in the previous direction and results in an error report.
I want to know if there is a way to wait for ether a command to be finished or for a change in the directory, without having an extra loop running for this time, or waiting for a hard set time.
I don't know why are you trying to do what you are trying to do, what you should do is:
Invoke-Command -SessionName (or -ComputerName) -ScriptBlock {Get-VM | Select-Object -Property name}

Invoke-command and msiexec

I'm trying to remove an application on a remote machine using the Invoke-Command cmdlet but it's not working.
Here is my script:
Invoke-Command -ComputerName "Computername" -Verbose -ScriptBlock {
msiexec.exe /x '{4ADBF5BE-7CAF-4193-A1F9-AM6820E68569}' /qn /passive
}
Are there any reliable, working alternatives in this context?
This doesn't use Invoke-Command or MSIExec, but it's a functional uninstall method for removing applications on remote machines using WMI for anything registered with WMI (should be anything installed via msiexec).
(Get-WmiObject -Class Win32_product -ComputerName ComputerName -Filter {IdentifyingNumber LIKE '{4ADBF5BE-7CAF-4193-A1F9-AM6820E68569}'}).uninstall()
Additionally that can be put into a ForEach loop if you have several computers to do it on. If you have the Name, IdentifyingNumber, and Version listed in WMI you can make it much faster with the following context (using AT&T Connect Participant Application v9.0.82):
$App="IdentifyingNumber=`"`{1F3A6960-8470-4C84-820C-EBFFAF4DA580`}`",Name=`"AT&T Connect Participant Application v9.0.82`",version=`"9.0.82`""
([WMI]\\ComputerName\root\cimv2:Win32_Product.$App).Uninstall()
Yes, the $App string is horribly escaped, but that's due to the way WMI requires the string to be formatted with curly braces and double quotes and what not. This is not exactly useful for a single uninstall since it requires you to get all that info up front and format the key string. If you were going to remove a piece of software off 30 machines though, it would be much better. You can get all that info by just leaving off the .Uninstall() method from my first command, so...
Get-WmiObject -Class Win32_product -ComputerName RemoteComputer -Filter {IdentifyingNumber LIKE '{1F3A6960-8470-4C84-820C-EBFFAF4DA580}'}
Will spit back something like:
IdentifyingNumber : {1F3A6960-8470-4C84-820C-EBFFAF4DA580}
Name : AT&T Connect Participant Application v9.0.82
Vendor : AT&T Inc.
Version : 9.0.82
Caption : AT&T Connect Participant Application v9.0.82
Can also be used with the name, or even partial names by changing the filter to something like `{Name LIKE '%AT&T Connect%'} or you can query WMI to list all the applications registered with it by leaving the -Filter off completely, though you probably want to pipe that to Format-Table to make it readable. I used:
gwmi -class win32_product -computername RemoteComputer|ft IdentifyingNumber,Name,Version
A good read with more info about this can be found at this link
Here is the solution I came up with
$myses = New-PSSession -ComputerName "Computer"
Invoke-Command -Session $myses -ScriptBlock {
#finds all instances of Java installed
$find_sep = gwmi win32_product -filter "Name LIKE '%Java%'" | select -ExpandProperty IdentifyingNumber
foreach($i in $find_sep){
msiexec.exe /x $i /qn /passive /l*v! c:\uninst.log
}
}

Get Process StartTime from a remote server using Powershell

I'm using following command to get the start time of a windows process. This is to get the running time of a process to terminate if it running too long.
$ProcessStartTime =(Get-Process $WinProcess -computer $computer).StartTime (Not working)
above code not returning Start Time value from a remote server ( it can access other process information). But it getting values for a local process with following command.
$ProcessStartTime =(Get-Process $WinProcess).StartTime (Working)
Can some one help me.
You can use wmi for this job:
gwmi win32_process -computername $computer|
? { $_.name -eq "powershell.exe" } |
% { $_.ConvertToDateTime( $_.CreationDate )}
i've the same result as you, but you can create a new session on the remote computer then use invoke-command to run your script :
$sess=new-pssession $computer
$ProcessStartTime =invoke-command -session $sess -ScriptBlock{ (Get-Process $WinProcess).StartTime}

Remote Machine not executing program

Here is what I have so far:
$source1="C:\Folder\Files\IPList.txt"
Get-Content $source1 |
Where-Object {-not(gwmi win32_process -ComputerName $_ -filter "name='Program.exe'")} |
Foreach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {"C:\Program Files\Folder\Folder\Program.exe"}}
When I run this in ISE everything comes back as normal and says it has run correctly. However, when I look at my remote machine nothing has been executed. The Process Program.exe is not running and there for the exe should be launched. I am running this from a server to hit about 50 remote machines. Once it goes through all 50, I will loop it and have it do it again, then continue the process in an infinite loop.
What am I missing for the program to start remotely? By the way I am running this script on Server 2008 R2 and it is hitting Windows 7 machines.
Edit
I am wondering since I can see the process firing off, is this an issue with Windows 7? I know Microsoft changed things and a service cannot fire off an application in the User space. Do you think this would be part of the same problem?
Try adding the call (&) operator to the ScriptBlock:
$source1="C:\Folder\Files\IPList.txt"
Get-Content $source1 |
Where-Object {-not(gwmi win32_process -ComputerName $_ -filter "name='Program.exe'")} |
Foreach-Object {Invoke-Command -ComputerName $_ -ScriptBlock {& "C:\Program Files\Folder\Folder\Program.exe"}}
Here's a good article on the various methods available to you: http://social.technet.microsoft.com/wiki/contents/articles/7703.powershell-running-executables.aspx
In your current syntax, the command your passing is just a string! This is what is happening on the remote end:
PS C:\> "C:\Program Files\Console2\Console.exe"
C:\Program Files\Console2\Console.exe
Powershell is echoing your string!
I am going to quote Oliver Lipkau as mentioned here: Source
If you need to start a process on a remote computer that keeps running after the script finished, use this function:
Function New-Process ([string]$computername, [string]$name) {
([WMICLASS]"\\$computername\ROOT\CIMV2:win32_process").Create($name)
}