Powershell remote job getting killed after a few hours - powershell

I am running a remote job as part of a deployment which is supposed to run forever (seeding random data) however the process keeps getting killed after only a few hours.. I am figuring some missing remote service flag or something.
I am running the remote job via this powershell command
Invoke-Command -ComputerName DEPLY -Credential $cred -AsJob -ScriptBlock {
C:\Deply\${bamboo.Configuration}\Seed\Seed.exe /y
}
Is there someway to prevent this process from being killed?

So it seems to me there is just an undocumented kill timeout for powershell jobs. I confirmed that my program is not crashing and the remoting service is just killing it after 3-4 hours. Or maybe its an OS thing - I don't know.
I switched to psexec which doesn't mess with the process - here is the command:
psexec \\DEPLY -accepteula -d -u "corp\administrator" -p "xxx" C:\Deply\${bamboo.Configuration}\Seed\Seed.exe /y
You can also launch it via WMI like so:
$process = get-wmiobject -query "SELECT * FROM Meta_Class WHERE __Class = 'Win32_Process'" -namespace "root\cimv2" -computername DEPLY -credential $cred
$results = $process.Create( "C:\Deply\${bamboo.Configuration}\Seed\Seed.exe /y" )
But I can't confirm if a remote process created this way lasts forever. Each test takes 4 hours and Im done messing with this.
RELATED: Launching background tasks in a remote session that don't get killed when the session is removed

Related

Unable to restart server after it joins the domain

Morning guys,
I'm running into an issue where I have a script that joins a Server to a Domain and restarts, intalls it's roles/features, etc and then restarts it again. I don't have an issue with the first restart:
Restart-Computer -ComputerName $IP -Credential $AdminCred -Wait -For PowerShell
but when I try to do the second restart at the end of the script it get the following error
Restart-Computer : The computer is skipped. Fail to retrieve its LastBootUpTime via the WMI service with the
following error message: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED)).
The following is the original code I tried
Restart-Computer -ComputerName $HostName -Wait -For PowerShell
Without credentials, as I expect Kerberos to work as the account from the laptop has proper permissions
but I also ran it with -Credential and same error. Then I tried changing $HostName to $IP and still no luck.
I can get around the error, by enclosing the Restart-Computer command into an invoke-command session but then I can't "wait for powershell" unless I set an arbitrary sleep timer for a couple minutes.
Any Ideas are appreciated!
I figured it out. I had to add the -WsmanAuthentication param and specify Kerberos. Final code
Restart-Computer -ComputerName $HostName -WsmanAuthentication Kerberos -Wait -For PowerShell

Start-Job via Invoke-Command: Job is only available in remote session

I'm trying to start TShark on different servers via following command:
Invoke-Command -Session $remoteSession -ScriptBlock {start-job -ScriptBlock {& 'C:\Program Files\Wireshark\tshark.exe' -b filesize:10000 -b files:5 -w "$tsharkResultDirectory\tshark.pcap"} -Name "TShark"}
The command works and everything is fine. But when I connect to the remote server via e.g. remote desktop, and perform the command Get-Job nothing is returned.
So that means that the beforehand started job only runs in the remote session. Does anybody know if there is a way to start a job in "global" scope. I don't want that TShark stops tracing if I accidentally close my PowerShell window.
Thx
You're correct that the job only exists for the duration of the PowerShell host session, which is why you can't retrieve it when you RDP.
A better approach might be to use the -AsJob switch of Invoke-Command. You can then retrieve the results by running Get-Job on your local machine rather than RDP'ing to the remote host:
Invoke-Command -Session $remoteSession -ScriptBlock { & 'C:\Program Files\Wireshark\tshark.exe' -b filesize:10000 -b files:5 -w "$tsharkResultDirectory\tshark.pcap" } -AsJob -Name "TShark"
Note again that you'll lose the job if you end your local PowerShell session.
I believe the only other way to run a remote job and have it persist is via the *-ScheduledJob commands as they record their results on disk:
PS C:\> get-command *ScheduledJob | Select Name
Name
----
Disable-ScheduledJob
Enable-ScheduledJob
Get-ScheduledJob
Register-ScheduledJob
Set-ScheduledJob
Unregister-ScheduledJob

Powershell remote install/app run

I am going to try and install software remotely onto a server and first i am trying to play around with the invoke-command cmdlet in powerhsell. Below is what I have so far
cls
Exit-PSSession
New-PSSession -ComputerName vm912test
Enter-PSSession -ComputerName vm912test -Credential sceris\pmanca
Invoke-Command -Computername vm912test -ScriptBlock {Start-Process "calc.exe" -wait}
Get-PSSession
However when i run this i see no running tasks in task manager. Does anyone know what i am missing? Once i can get this to work i will expand onto trying to remotely install some software first. I have no issues on communicating with the server and i have remote access/admin access on the box.
I updated with some more code but still receiving the same result that nothing is happening.

How do I remotely start a VM on VMWare Workstation using Powershell?

I have VMWare Workstation and beginner's knowledge of PowerShell.
I've created a script that successfully starts a VM on my local machine by using the vmrun tool. However, if I run it through a remote session, nothing happens. Any idea why?
Get-PSSession | Remove-PSSession
$VMHostMachine | New-PSSession
$rs = Get-PSSession # testing using localhost
Write-Debug ("Now starting VM on host: " + $VMHostMachine)
$script = {param($VMImagePath, $VMConsolePath);
$QuotedVMPath = "`"{0}`"" -f $VMImagePath
$Result = Start-Process -FilePath $VMConsolePath -ArgumentList "-T", "ws", "start", $QuotedVMPath -Wait -PassThru -NoNewWindow
}
Invoke-Command -Session $rs -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath
Invoke-Command works if I remove the session parameter:
Invoke-Command -ScriptBlock $script -ArgumentList $vmConfig.VMImagePathOnHost, $vmConfig.VMRunUtiltyPath
I have a similar script that successfully reverts to a snapshot on my localhost through a PSSession, so why is starting a VM giving me trouble?
Does it work if you "enter-pssession" and then try to invoke?
I'm assuming it's "not working" for you here, because you invoke commands through PSSession where there are no visible desktops / gui-session, and you are expecting the VMWare workstation to appear? The receiving end is a service.
You could check if the process is running VMWare with Get-Process.
To do what you want to achieve here you could take a look at PsExec, which allows to start applications into an active session.
https://technet.microsoft.com/en-US/sysinternals/bb897553.aspx
Note the "-i" parameter on PSExec.

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}