none
Script spills out extraneous data that commands didn't outside of script and doesn't work! Bewildered as to what is wrong! RRS feed

  • Question

  • Here is my PowerShell script and I will follow it with the output following running it.

    # Proc-Watch.ps1
    #
    # this is what I came up with to test if a local process was running
    # with the same name of the process that I would be starting up
    # for debugging purposes I am using the notepad process in actuality
    # for my real program it will be excel because my PSscript runs on a
    # schedule and I may have excel open doing something else when the
    # script runs and do not want to kill it..............
    # ==================================================================

    Set-StrictMode -Version Latest

    $ProcessActive = Get-Process notepad -ErrorAction SilentlyContinue

    if($ProcessActive -eq $null)    {
        # Do nothing
            Write-host "Doing nothing."
        }
        else    {
            $SavedProc = (get-process notepad |format-wide -property id -column 1)
            Write-host "Obtained the process id: $SavedProc"
    }

     # -ErrorAction SilentlyContinue because when its not running then may return error

    Start-process notepad

    $ProcList = (get-process notepad |format-wide -property id -column 1)

    foreach ($Proc in $ProcList)        {
        if ($Proc -eq $SavedProc)    {
        # do nothing
        Write-host "My saved proc is $Proc"
        }
        else    {
        #stop-process -id $proc
        Write-host "Would have stopped process: $proc"
        }
    }

    ==============8<----------------------------   (Cut here)
    Output

    [C:\PowerShell]
    PS:106 >.\Proc-Watch.ps1
    Obtained the process id: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData Microsoft.PowerS
    ernal.Format.GroupStartData Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData Microsoft.Pow
    Internal.Format.GroupEndData Microsoft.PowerShell.Commands.Internal.Format.FormatEndData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.FormatStartData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.GroupStartData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.FormatEntryData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.GroupEndData
    Would have stopped process: Microsoft.PowerShell.Commands.Internal.Format.FormatEndData

    [C:\PowerShell]

    Thursday, May 23, 2013 4:02 AM

Answers

  • Here it is I nV2 but it still makes no sense

    #requires -version 2.0
    
    if($SavedProc=(Get-Process notepad -ErrorAction SilentlyContinue)|%{$_.Id}){
         Write-host "Doing nothing."
    }else{
         Write-host "Obtained the process id: $SavedProc"
    }
    
    Start-process notepad
    
    $ProcList=get-process notepad | %{$_.id }
    
    foreach($Proc in $ProcList){
         if ($Proc -eq $SavedProc){
              # do nothing
              Write-host "My saved proc is $Proc"
         }else{
         	stop-process -id $proc -WhatIf
         }
    }


    ¯\_(ツ)_/¯

    • Marked as answer by Jim Gaynor Thursday, May 23, 2013 8:32 PM
    Thursday, May 23, 2013 8:47 AM

All replies

  • Hi,

    Try this:

    Change:
    get-process notepad |format-wide -property id -column 1
    
    To:
    get-process notepad | select id

    Do this each place you use format-wide and give it a try.

    Thursday, May 23, 2013 4:11 AM
  • This also doesn't appear to account for the fact that there might be multiple Notepad processes open when the script is run. It works if only one Notepad process is running.
    Thursday, May 23, 2013 4:16 AM
  • I figured that this will come in handy somewhere down the road, so I adjusted the script until it works for me. Give this a try and see if it works for you:

    Set-StrictMode -Version Latest
    
    $ProcessActive = Get-Process notepad -ErrorAction SilentlyContinue
    
    If($ProcessActive -eq $null) {
        # Do nothing
            Write-Host "Doing nothing."
        }
        Else {
            $SavedProc = (Get-Process notepad | Select Id)
            Write-Host "Obtained the process id:" $SavedProc.Id
    }
    
    
     # -ErrorAction SilentlyContinue because when its not running then may return error
    
    Start-Process notepad
    
    $ProcList = (Get-Process notepad | Select Id)
    
    ForEach ($Proc in $ProcList)        {
        if ($SavedProc.Id -join " " -match $Proc.Id) {
        # do nothing
        Write-Host "My saved proc is" $Proc.Id
        }
        Else    {
        #Stop-Process -Id $Proc.Id
        Write-Host "Would have stopped process:" $Proc.Id
        }
    }

    Thursday, May 23, 2013 4:44 AM
  • This is a close approximation of what appears to be what you are trying to do although the code you posted Is so badly formed it is impossible to tell.

    Try approaching your structure along these lines.  You will find it is easier to avoid weird errors.

    if($SavedProc=(Get-Process notepad -ErrorAction SilentlyContinue).Id){
         Write-host "Doing nothing."
    }else{
         Write-host "Obtained the process id: $SavedProc"
    }
    
    Start-process notepad
    
    $ProcList=(get-process notepad).id
    
    foreach($Proc in $ProcList){
         if ($Proc -eq $SavedProc){
              # do nothing
              Write-host "My saved proc is $Proc"
         }else{
         	stop-process -id $proc -WhatIf
         }
    }
    


    ¯\_(ツ)_/¯

    Thursday, May 23, 2013 4:51 AM
  • Here's the same code with an explanation of the changes I made:

    Set-StrictMode -Version Latest
    
    $ProcessActive = Get-Process notepad -ErrorAction SilentlyContinue
    
    If($ProcessActive -eq $null) {
        # Do nothing
            Write-Host "Doing nothing."
        }
        Else {
            $SavedProc = (Get-Process notepad | Select id) # Use select instead of Format. Use Format-* only to format output, not to manipulate objects.
            Write-Host "Obtained the process id:" $SavedProc.Id # $SavedProc is an object, we want the Id property
    }
    
    
     # -ErrorAction SilentlyContinue because when its not running then may return error
    
    Start-Process notepad
    
    $ProcList = (Get-Process notepad | Select id) # Same as above
    
    ForEach ($Proc in $ProcList)        {
        if ($SavedProc.Id -join " " -match $Proc.Id) { #These are objects, so we look at the Id property again. $SavedProc might contain more than one Id, so join is used.
        # do nothing
        Write-Host "My saved proc is" $Proc.Id # Id property
        }
        Else    {
        #Stop-Process -Id $Proc.Id # Id property
        Write-Host "Would have stopped process:" $Proc.Id # Id property
        }
    }

    Thursday, May 23, 2013 5:07 AM
  • Dunno why but this will not work for me.  I get this:

    [C:\PowerShell]
    PS:118 >.\Proc-Watch.ps1
    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch.ps1:21 char:59
    +         Write-host "Obtained the process id: " $SavedProc. <<<< Id
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch.ps1:31 char:20
    +     if ($SavedProc. <<<< Id -join " " -match $Proc.Id)    {
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch.ps1:31 char:20
    +     if ($SavedProc. <<<< Id -join " " -match $Proc.Id)    {
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch.ps1:31 char:20
    +     if ($SavedProc. <<<< Id -join " " -match $Proc.Id)    {
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    ==============================8<-------------

    Maybe I have a different version of PS:

    [C:\PowerShell]
    PS:119 >$PSVersionTable

    Name                           Value
    ----                           -----
    CLRVersion                     2.0.50727.5466
    BuildVersion                   6.1.7601.17514
    PSVersion                      2.0
    WSManStackVersion              2.0
    PSCompatibleVersions           {1.0, 2.0}
    SerializationVersion           1.1.0.1
    PSRemotingProtocolVersion      2.1

    Thursday, May 23, 2013 5:20 AM
  • JRV:  tried your method also and still come up with this:

    [C:\PowerShell]
    PS:122 >.\Proc-Watch2.ps1
    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch2.ps1:13 char:70
    + If ($SavedProc = (Get-Process notepad -ErrorAction SilentlyContinue). <<<< Id)    {
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    Property 'Id' cannot be found on this object. Make sure that it exists.
    At C:\PowerShell\Proc-Watch2.ps1:22 char:35
    + $ProcList = (get-process notepad). <<<< Id
        + CategoryInfo          : InvalidOperation: (.:OperatorToken) [], RuntimeException
        + FullyQualifiedErrorId : PropertyNotFoundStrict

    The variable '$ProcList' cannot be retrieved because it has not been set.
    At C:\PowerShell\Proc-Watch2.ps1:24 char:28
    + foreach ($Proc in $ProcList <<<< )        {
        + CategoryInfo          : InvalidOperation: (ProcList:Token) [], RuntimeException
        + FullyQualifiedErrorId : VariableIsUndefined

    I am thinking that I have a different version than everybody else,

    Thursday, May 23, 2013 5:39 AM
  • Mike,  I did understand your changes ok, and as I said, I think I must have an older version of PS or something, am going to look into upgrading to 3.0

    Jim.

    Thursday, May 23, 2013 5:40 AM
  • Mike,  I did understand your changes ok, and as I said, I think I must have an older version of PS or something, am going to look into upgrading to 3.0

    Jim.


    Upgrading to 3.0 is definitely great a great idea (and you should definitely do it anyway), but I'm not sure if it's going to solve your problem. I just ran the script I posted in a 2.0 instance and it ran just fine.
    Thursday, May 23, 2013 5:55 AM
  • Here it is I nV2 but it still makes no sense

    #requires -version 2.0
    
    if($SavedProc=(Get-Process notepad -ErrorAction SilentlyContinue)|%{$_.Id}){
         Write-host "Doing nothing."
    }else{
         Write-host "Obtained the process id: $SavedProc"
    }
    
    Start-process notepad
    
    $ProcList=get-process notepad | %{$_.id }
    
    foreach($Proc in $ProcList){
         if ($Proc -eq $SavedProc){
              # do nothing
              Write-host "My saved proc is $Proc"
         }else{
         	stop-process -id $proc -WhatIf
         }
    }


    ¯\_(ツ)_/¯

    • Marked as answer by Jim Gaynor Thursday, May 23, 2013 8:32 PM
    Thursday, May 23, 2013 8:47 AM
  • Original example with guards:

    #requires -version 3.0
     
    If($SavedProc=(Get-Process notepad -ErrorAction SilentlyContinue).Id){
         Write-host "Doing nothing."
    }else{
         Write-host "Obtained the process id: $SavedProc"
    }
     
    Start-process notepad
     
    $ProcList=(get-process notepad).id
     
    foreach($Proc in $ProcList){
         if ($Proc -eq $SavedProc){
              # do nothing
              Write-host "My saved proc is $Proc"
         }else{
         	stop-process -id $proc -WhatIf
         }
    }


    ¯\_(ツ)_/¯

    Thursday, May 23, 2013 8:51 AM
  • Mike I tried your method again and double and triple checked everything, Quotes, Parens, Curly-Braces and had everything exactly the same and still got the same error.
    Thursday, May 23, 2013 8:34 PM
  • JRV: 

    Your entry at about 03:45 this morning which you stated "here it is in V2" which is slightly different that the first time around, WORKS!  Don't ask me why, now I just need to study it a little bit and probably read into some of the PS manuals I have been letting collect dust.  Thank You.

    Thursday, May 23, 2013 8:37 PM
  • Herewith, for posterity, is the final Working Model:

    Set-StrictMode -Version Latest

    $ProcessActive = Get-Process notepad -ErrorAction SilentlyContinue

    if($ProcessActive -eq $null)    {
        # Do nothing
            Write-host "Doing nothing."
        }
        else    {
            $SavedProc = (get-process notepad |select Id)
            Write-host "Obtained the process id: " $SavedProc.Id
    }

     # -ErrorAction SilentlyContinue because when its not running then may return error

    Start-process notepad

    $ProcList = (get-process notepad |select Id)

    foreach ($Proc in $ProcList)        {
        if ($SavedProc.Id -join " " -match $Proc.Id)    {
        # do nothing
        Write-host "My saved proc is" $Proc.Id
        }
        else    {
        #stop-process -id $Proc.Id
        Write-host "Would have stopped process: " $Proc.Id
        }
    }

    ==================8<----------------------------------------

    By The Way,  I tried to upgrade PS to 3.0 and ran into a snag:  I followed all of the instructions for installing Windows Power Shell on W7  firstly removing and previous .NET 3.0, (there was none??), then downloaded and installed MS >NET Framework 4.5 full setup, then download and install the WMF 3.0, I grabbed the Windows 6.1 KB2506143-x64.msu  -- The first snag I got was an error from Windows Update Standalone Installer which said "Installer encountered an error: 0x80070422 - The service cannot be started, either because it is disabled or because it has no enabled devices associated with it."

    Searched forums and found no answers so I decided to poke around on my own;  I found two services disabled, ASP.NET and Windows Update.  Set both of them to Automatic and rebooted my PC.  Following the reboot I then reran the install and now get message "Some updates were not installed" In a box below it lists Update for Windows (KB2506143) - No reason given for this, however.  I keep running into little things like this here and there and they sure do get discouraging.


    • Edited by Jim Gaynor Thursday, May 23, 2013 8:54 PM
    Thursday, May 23, 2013 8:50 PM
  • First thing on your agenda should be to discover why the code you just posted is so bad.  It is full of very bad things that will give you ongoing headaches.

    I mentioned earlier to NOT use constructs that you do not understand like the first line of your script.  Until you understand why don't use strict.


    ¯\_(ツ)_/¯

    Thursday, May 23, 2013 8:55 PM
  • By The Way,  I tried to upgrade PS to 3.0 and ran into a snag:  I followed all of the instructions for installing Windows Power Shell on W7  firstly removing and previous .NET 3.0, (there was none??), then downloaded and installed MS >NET Framework 4.5 full setup, then download and install the WMF 3.0, I grabbed the Windows 6.1 KB2506143-x64.msu  -- The first snag I got was an error from Windows Update Standalone Installer which said "Installer encountered an error: 0x80070422 - The service cannot be started, either because it is disabled or because it has no enabled devices associated with it."

    Searched forums and found no answers so I decided to poke around on my own;  I found two services disabled, ASP.NET and Windows Update.  Set both of them to Automatic and rebooted my PC.  Following the reboot I then reran the install and now get message "Some updates were not installed" In a box below it lists Update for Windows (KB2506143) - No reason given for this, however.  I keep running into little things like this here and there and they sure do get discouraging.


    I highly recommend you get these issues sorted out, this will cause you serious trouble down the road. Glad you were able to get something working though.

    Thursday, May 23, 2013 11:47 PM
  • Mike,

    I looked at this again, and going by what JRV states is a problem in itself, I commented out the Set-StrictMode line and it works just fine.

    Well, it's back to the books for me, I am not quite ready for this..............

    Sure you are, all it takes is time and practice. Mindset is everything.

    EDIT: Okay, something weird. I'm assuming that Jim Gaynor posted this, but it's showing up as being posted by MigrationUser1. I don't see this post appearing in either users activity log though.


    EDIT2: This post doesn't show in my activity log either. Fun forum hiccup.

    Friday, May 24, 2013 1:37 AM