Note
Access to this page requires authorization. You can try signing in or changing directories.
Access to this page requires authorization. You can try changing directories.
Question
Monday, November 24, 2014 8:04 PM
Hi
I want a way to tell if my powershell script is running. I am trying to do this like the example below but the query returns nothing even though I know my script is running as I had double clicked on it to launch it.
"Select CommandLine from Win32_Process where CommandLine like '%deploy%'"
Get-WmiObject -Query $query
All replies (11)
Monday, November 24, 2014 11:19 PM ✅Answered | 1 vote
Use the -file parameter when starting and the CommandLine will reflect that.
PS C:\> PowerShell -File F:\Software\Win32-ObjectBrowser.ps1
Query WMI
PS C:\> Get-WMIObject -Class Win32_Process -Filter "Name='PowerShell.EXE'" | Where {$_.CommandLine -Like "*ObjectBrowser*"} | Select Handle,CommandLine | FT -AutoSize
Handle CommandLine
1972 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File F:\Software\Win32-ObjectBrowser.ps1
Querying it (The first and only instance)
PS C:\> $Processes = Get-WMIObject -Class Win32_Process -Filter "Name='PowerShell.EXE'" | Where {$_.CommandLine -Like "*ObjectBrowser*"}
PS C:\> $Processes.GetType()
IsPublic IsSerial Name BaseType
True True ManagementObject System.Management.ManagementBaseObject
PS C:\> $Processes.Count
PS C:\> $Processes.Count -EQ $Null
True
Started another session with the file parameter specified
PS C:\> $Processes = Get-WMIObject -Class Win32_Process -Filter "Name='PowerShell.EXE'" | Where {$_.CommandLine -Like "*ObjectBrowser*"}
PS C:\> $Processes | Select Handle,Commandline | FT -AutoSize
Handle Commandline
1972 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -File F:\Software\Win32-ObjectBrowser.ps1
2556 "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe" -file F:\Software\WIN32-ObjectBrowser.ps1
PS C:\> $Processes.GetType()
IsPublic IsSerial Name BaseType
True True Object[] System.Array
PS C:\> $Processes.Count
2
Within the script check the count for null, if it is not null then a second session has been started
Tuesday, November 25, 2014 1:24 AM ✅Answered
Go through some sort of initialize function with something like this
$Processes = Get-WMIObject -Class Win32_Process -Filter "Name='PowerShell.EXE'" | Where {$_.CommandLine -Like "*ObjectBrowser*"}
IF ($Processes -EQ $Null)
{
PowerShell.EXE -File ScriptFile
}
ElseIf ($Processes.Count -NE $Null)
{
Write-Host "Second Instance has started"
}
Else
{
#It is good
}
Monday, November 24, 2014 8:08 PM
Hi,
Check the process list for powershell.exe.
PS - double clicking on a .ps1 file does not run it, it only opens it in Notepad.
Don't retire TechNet! - (Don't give up yet - 13,085+ strong and growing)
Monday, November 24, 2014 9:54 PM
I can not find it. In a PowerShell session, I start another PowerShell specifying the file parameter
PS C:\> $PID
6028
PS C:\> PowerShell -File F:\Software\Win32-ObjectBrowser.ps1
Within the new PowerShell session I can check that $PID is different
I can find the new PowerShell process in the process list but without the $PID, can not guarantee that it is correct PowerShell Session that it is found and identified. The script name is nowhere to be found.
Monday, November 24, 2014 10:20 PM
Have the script write an event to an event log when it starts, and include it's PID in the event message.
Retrieve the PID from that event, and check to see if there's a Powershell process with that PID still running.
[string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "
Monday, November 24, 2014 10:30 PM
I checked the WMI WIN32_Process class and set the filter to conserve space.
Checking the instance values of the filtered WIN32_Process class it can be found there.
Found it in another another PowerShell session, (ISE session)
PS C:\> $PID
4248
PS C:\> Get-WMIObject -Class Win32_Process -Filter "Name='PowerShell.EXE'" | Where {$_.CommandLine -Like "*ObjectBrowser*"} | FT
__GENUS __CLASS __SUPERCLASS __DYNASTY __RELPATH __PROPERTY_C __DERIVATION __SERVER __NAMESPACE __PATH
OUNT
2 Win32_Process CIM_Process CIM_Manage... Win32_Pro... 45 {CIM_Proc... VM-HOST1 root\cimv2 \\VM-HOST...
Like operator does not work in filter?
Monday, November 24, 2014 10:54 PM
Hi Brian,
I want to query the win32_Process kind of like how you show it but I don't see the script name in the commandline. Do i need to specify the -file option in the powershell script that is running? What is the syntax if that is possible. I am trying to prevent multiple instances of my script running. If i find one instance of it already running I just want to print a message and exit.
Tuesday, November 25, 2014 1:17 AM
thank you Brian so the other question I had is what about if someone launches the file by a double click. Is there anyway to force that powershell -file option?
Tuesday, November 25, 2014 10:22 PM
Thank you Brian The next issue I am having that is that when I relaunch my window as Administator level using the script at the link below https://gallery.technet.microsoft.com/scriptcenter/63fd1c0d-da57-4fb4-9645-ea52fc4f1dfb
The command line and file are blank. There is no way to see the name of my script. example is below. Is this a limitation of the Start-Process cmdlet. What does anyone think?
param([string]$vname)
function Use-RunAs
{
# Check if script is running as Adminstrator and if not use RunAs
# Use Check Switch to check if admin
param([Switch]$Check)
$IsAdmin = ([Security.Principal.WindowsPrincipal] [Security.Principal.WindowsIdentity]::GetCurrent()`
).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator")
if ($Check) { return $IsAdmin }
if ($MyInvocation.ScriptName -ne "")
{
if (-not $IsAdmin)
{
try
{
$arg = "-file `"$($MyInvocation.ScriptName)`""
Start-Process "$psHome\powershell.exe" -Verb Runas -ArgumentList $arg -ErrorAction 'stop'
}
catch
{
Write-Warning "Error - Failed to restart script with runas"
break
}
exit # Quit this session of powershell
}
}
else
{
Write-Warning "Error - Script must be saved as a .ps1 file first"
break
}
}
Use-RunAs
$vname = Read-Host 'enter version'
Thursday, December 4, 2014 5:51 AM
what does anyone think?
Thursday, December 4, 2014 9:42 AM
I tried the start-process and the command line shows in a instance of WIN32_Process.CommandLine within another PowerShell session.
A Browser for just the WIN 32 Classes
Powershell GUI Application - WIN 32 Class Explorer
https://gallery.technet.microsoft.com/Powershell-GUI-Application-a884a710