I want to get the start time of a process running on remote machine using powershell.
On my local computer I get it simply by using get-process $processname | select StartTime.
I tried using get-process $processname -computername $server1 | select StartTime but this is returning me nothing.
Please suggest any better way.
Using WMI. This code return start time of powershell.exe process:
$a = gwmi win32_process -computername $server1| ? { $_.name -eq "powershell.exe" }
$a | % { $_.ConvertToDateTime( $_.CreationDate )}
PS> $StartTime= #{n='StartTime';e={$_.ConvertToDateTime($_.CreationDate)}}
PS> gwmi win32_process -cn $server1 -filter "Name='$processname' AND CreationDate IS NOT NULL" | select Name,$StartTime
Related
I need to get the name and size of the file that has been sent to printer. So I need to get info about it BEFORE file is printed.
I have tried to work with files in Windows\System32\spool\PRINTERS, but I can't get any info from .SHD and .SPL files even if I pause the print work.
I started to look for some solution using Get-WmiObject -Class Win32_Printer.
Is it a correct approach? Maybe I should use some particular methods or something?
I tried this code, but it shows a mistake
$comp = $(Get-WmiObject Win32_Computersystem).Name
if ( (Get-ChildItem C:\Windows\System32\spool\PRINTERS | Measure-Object).Count -ne 0)
{
Get-WmiObject -Class win32_service -filter 'name="spooler"' -ComputerName $comp | Invoke-WmiMethod -Name StopService | out-null
$name = $(Get-WmiObject Win32_PrintJob).Document
$size = $(Get-WmiObject Win32_PrintJob).Size
$time = $(Get-WmiObject Win32_PrintJob).StartTime
"$comp,$name,$size,$time" | Out-file C:\Scripts\PrintJobs.csv -Append
Set-Service spooler -ComputerName $comp -Status Running
}
What is wrong?
PowerShell is a new thing for me and for now I'm totally lost with this task
Unless you know when to run the powershell I don't see how you can get this information "Before" the print job is sent. You would have to keep the spooler "Paused" and then when the script is run set it to Running and then pause it again until the next time the script is run.
one thing is that "Get-WmiObject Win32_PrintJob" will most likely return a collection of all current spooled jobs. so your code should look something like this to get the information you are looking for:
$comp = $(Get-WmiObject Win32_Computersystem).Name
$jobs=Get-WmiObject Win32_PrintJob
$jobInfo = $jobs | ForEach-Object {"$comp,$($_.Document),$($_.Size),$($_.StartTime)"}
$jobInfo | Out-file C:\Scripts\PrintJobs.csv -Append
im a writing a script in powershell that show services running on a computer and start the services that is not running.
My command is the following
Show all services on Powershell with startmode "Auto"
$shse = Get-CimInstance -ClassName win32_service -Computername pshells '
-Filter "startmode = 'auto'" |
ConvertTo-Html
Service start all services with status = stopped.
$Service = (Get-CimInstance -ClassName win32_service - Computername pshells '
-Filter "startmode = 'auto'")
If ($Service.status -eq "Stopped"){
Start-Service}
Else {}
Convert Output to HTML.
ConvertTo-Html -body "$shse " |
Out-File C:\Scripts\Service.htm
When running this in powershell i get this error
At line:6 char:30
+ -Filter "startmode = 'auto'")
+ ~
Unexpected token ')' in expression or statement.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordExceptio
n
+ FullyQualifiedErrorId : UnexpectedToken
try this:
Get-Service -ComputerName pshells | where {$_.StartType -EQ 'Automatic' -and $_.Status -eq 'Stopped'} | Start-Service
Replace your existing code with this:
$Services =Get-CimInstance -ClassName win32_service -Computername pshells -Filter "startmode = 'auto'"
foreach($service in $Services)
{
If ($Service.state -eq "Stopped"){Start-Service}
Else {"Service is already running."}
}
Few things you need to keep in mind , if you wish to use a line continuation then you should use baktick(`) not single-quote(') .
You are getting all the services status and you wish to start them all if stopped. So as a whole it will all the return you as OK since you were checking status. You should check state not status.
As a whole if it is returning and in case any of them is already running, then PS will have conflict in the condition, thats when you should use foreach loop.
Iterate each value you got in $services, check the state and take necessary action and loop over ; proceed with the same for the next one unless the list is exhausted.
Hope it helps you.
On the line:
$Service = (Get-CimInstance -ClassName win32_service - Computername pshells '
-Filter "startmode = 'auto'")
You aren't using a back-tick (`) you're using a single quote ('). It should be a back-tick to escape the new line.
So i got the command to work, i typed in the following
Do
{
$shse = Get-CimInstance -ClassName win32_service -Computername WIN-9HMIGR3QFE6 -Filter "startmode = 'auto'" |
ConvertTo-Html
$Service = (Get-CimInstance -ClassName win32_service -Computername WIN-9HMIGR3QFE6 -Filter "startmode = 'auto'")
If ($Service.status -eq "Stopped"){
Start-Service}
Else {}
ConvertTo-Html -body "$shse " |
Out-File C:\Test\Service01.html
Start-sleep -Seconds 600
}until ($infinity)
It created an html, where i can look at the services... But there is one problem, when i close down a service, the script is set to start all scripts again after 600 seconds, but service never gets up again.. So now the stopped-start service function is not working correctly
could you help me with, i'm trying to get the computer name and the output from this command below in a txt file.
for /f %1 in (user_plant.txt) do >>\brspd010\c$\users\machael1\desktop\nic.txt ((wmic /node:%i computersystem get caption | more) & (powershell -command "& {Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter IPEnabled=TRUE -ComputerName %i. | Format-Table -Property IPAddress, DHCPEnabled}))
the command powershell when i run without the rest, it's works fine, but when i try to put together not work.
userplant.txt has:
brspd001
brspd002
...
My needly is: A txt file with Name of machine and IPV4,DHCP status as the same showed when i run only the powershell command.
example:
Computername:
BRSPD001
IP
172 ...
DHCP Enabled
true.
or someone like it.
could you help me?
This is probably better done in pure PowerShell. Assuming one computername per line in user_plant.txt:
foreach ($Computer in (Get-Content -Path user_plant.txt)) {
$c = Get-WMIObject -ComputerName $Computer -Class Win32_NetworkAdapterConfiguration -Filter "IPEnabled = $true"
"ComputerName: {0} IP: {1} DHCPEnabled {2}" -f $Computer,$C.IPAddress[0],$C.DHCPEnabled | Add-Content -Path "\\brspd010\c$\users\machael1\desktop\nic.txt"
}
In powershell I am trying to do the following:
$name = "computername"
#get installed programs
Write-Host "****APPLICATIONS"
gwmi win32_Product -ComputerName $name | select name
#gets services
write-host "****SERVICES"
Get-Service -ComputerName $name | ft
the expected output would be
****APPLICATIONS
name
of
app
****SERVICES
running services here
more services here
the actual result is
****APPLICATIONS
****SERVICES
name
of
app
running services here
more services here
I have attempted to do start-job then wait-job , but running gwmi as a job seems to output nothing to the console and sending the output to a separate file defeats the purpose of other parts of the script
I also attempted to use start-sleep and it still finishes both write-host commands before proceeding
Try this:
$name = "computername"
Write-Host "`n****APPLICATIONS`n"
gwmi win32_Product -ComputerName $name | % {$_.name}
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
If you want the results alphabetical:
$name = "computername"
Write-Host "`n****APPLICATIONS`n"
$apps = gwmi win32_Product -ComputerName $name | % {$_.name}
$apps | sort
write-host "`n****SERVICES"
Get-Service -ComputerName $name | ft
Param(
$ComputerName = 'AT805061'
)
# Get installed programs
Write-Host "`n****APPLICATIONS`n"
Get-WmiObject win32_Product -ComputerName $ComputerName | Select-Object -ExpandProperty Name | Sort-Object
# Get services
Write-Host "`n****SERVICES`n"
Get-Service -ComputerName $ComputerName | Where-Object -Property Status -eq -Value Running | Select-Object -ExpandProperty Name | Sort-Object
I am trying to get a list of running processes and filter by two process names - can any one tell me how to get this working?
I've so far got it working and filtering out one process name:
$rn = Get-WMIObject Win32_Process -computer servername `
-credential mydomain\administrator -filter "Name='program1.exe'" |
select -expand path
$lst = Get-Content “C:\path\path2\List.txt”
Compare-Object $lst $rn
What I want it to do is filter two process names but nothing I've tried works. Any ideas?
Here's how to get a complete set of Process objects which match a list of process names you're interested in.
$ProcessNames = #( 'explorer.exe', 'notepad.exe' )
Get-WmiObject Win32_Process -Computer 'localhost' |
Where-Object { $ProcessNames -contains $_.Name } |
Select-Object ProcessID, Name, Path |
Format-Table -AutoSize
This example finds all processes, then filters that list by sending them to a pipeline filter that checks to see if the process name is contained in the list of interesting process names. The main benefit of using the pipeline this way is that you can easily access other attributes (such as ProcessID) of the returned processes.
ProcessID Name Path
--------- ---- ----
5832 explorer.exe C:\Windows\Explorer.EXE
4332 notepad.exe C:\Windows\system32\NOTEPAD.EXE
2732 notepad.exe C:\Windows\system32\notepad.exe
Use WQL operators like OR, AND, LIKE etc:
Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe' OR Name='program2.exe'"
Create an array of the processes you're after:
$processes = #('winword.exe', 'notepad.exe', 'excel.exe') | `
% {
$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\admin -filter "Name='$_'" | select -expand path
#$lst = Get-Content “C:\path\path2\List.txt”
#Compare-Object $lst $rn
write-host $rn
}
I've commented out your compare so you can see how we are looping through the array clearly.
if I understood well try this:
$rn = Get-WMIObject Win32_Process -computer servername -credential mydomain\administrator -filter "Name='program1.exe OR Name='program2.exe'"
Compare-Object $rn[0].path $rn[1].path # if there are only one instance for process with name program1.exe and program2.exe