Get Process StartTime from a remote server using Powershell - 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}

Related

PowerShell remote session and Start-Job issue

I am trying to run following script with job but the code in the block only executes 1st command and exits. Job is displayed completed on my computer
$computers = get-adcomputer -filter * | where { ($_.DNSHostName -match 'server')}
foreach ($computer in $computers) {
$session = New-PSSession -ComputerName $computer.DNSHostName
Invoke-Command -Session $session -ScriptBlock {
Stop-Service W3SVC -Force
Remove-Item "C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root" -Force -Recurse
Start-Service W3SVC
} -asjob -jobname IIS_Maintenance
Remove-PSSession -Session $session
}
If I comment out -asjob -jobname IIS_Maintenance job runs fine but it's synchronous. It takes several seconds to stop IIS but I am not sure why job is not waiting on that.
Thoughts?
Creating the session induces a lot of delay. Why did you chose to use the -Session instead of directly using -ComputerName ?
I think using the -ComputerName way will be more efficient to run with Start-Job. Either way, Since you are invoking the jobs on remote machines, your workstation would have no clue on the progress of the jobs.
If you want to track the progress of the jobs, you shouldn't be using Invoke-Command at all.
OK I figured it out... the point of confusion was:
1) I didn't know I can send block of code without Session and yes session implementation is slow
2) I also didn't know I send invoke-command to several computers are once without foreach. Thank you for that Ansgar Wiechers!

PowerShell won't terminate hung process

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.

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}

Mapping drive on a remote machine in powershell

I am working on a remote machine from my desktop and I have the following script :
Invoke-Command -computername $name -authentification default
-credential $creds1
-scriptblock {net use \share $password2 /user:otherdomain\otheruser}
Then I get A specified logon session does not exist. It may have already been terminated. This thing is frustrating because if I run net use \\share $password2 /user:otherdomain\otheruser directly on the remote machine it works perfectly. Is there a workaround or did I miss something ?
You need to pass the variable $password2 into the script block, otherwise its value will be empty:
Invoke-Command -Computer $name ... -ScriptBlock {
net use \\host\share $args[0] /user:otherdomain\otheruser
} -ArgumentList $password2

Process list of commands on remote machine with Invoke-Command

I'm trying to write a powershell script that will take a list of commands and then run them on a remote machine, I have the following code:
foreach($command in $commands)
{
Invoke-Command -computer "BNEBAK" -scriptblock{"$command"}
}
Which does not throw any error but also does not actually run the command (e.g stop-service servicename). $commands is read in from a text file passed as an argument when the script is called, I know the rest of this script works because I have been using it to do local commands with Invoke-Expression for some time.
Any help would be fantastic.
The correct code would be
$commands = #(get-content com.txt)
for($command in $commands) {
$scriptblock = $ExecutionContext.InvokeCommand.NewScriptBlock($command)
Invoke-Command -computer $computer -scriptblock $scriptblock
}