Avid displaying code in console while running a script in ISE or PS

Welcome Forums General PowerShell Q&A Avid displaying code in console while running a script in ISE or PS

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
3 months, 3 weeks ago.

  • Author
    Posts
  • #131915

    Participant
    Points: 88
    Rank: Member

    Hi, I am running a script in ISE, code has a loop, running the code, inside ISE all code is written to the host which I don't want. After googling, I found some articles indicating that to Prevent ISE Script echo to console, just save the file which it did not, still everything is getting printed.

    I tried running the script in PS, same thing everything is printed. I tried

    Set-PSDebug -Trace 0

    did not help. Thanks your for your help

  • #131918

    Participant
    Points: 1,373
    Helping Hand
    Rank: Community Hero

    ISE is just for developement. Your scripts should run in a normal console. If you want to avoid output you can pipe the output of any cmdlet to Out-Null.

  • #131924

    Participant
    Points: 411
    Helping Hand
    Rank: Contributor

    If you save the file and execute the script then it will only display the file name, otherwise the entire code will be displayed on ISE console.

    But, I prefer to use Visual Studio Code for scripting and its high time to switch to code.

    Thank you.

  • #131933

    Participant
    Points: 88
    Rank: Member

    Not the case Krian, event as I said, even when running the script in PS it is displaying the whole every time inside the loop.

    Question, how do I use, so should I use Out-Null. for every single line? and should it be ">>" or "|" symbol?

    Thanks

  • #131934

    Participant
    Points: 167
    Helping Hand
    Rank: Participant

    Hmm – odd.

    Use the | pipe to supress cmdlet output.

    Which reminds me – I read a good blog post on $null only recently. I've linked to the bit that maybe relevant for you. https://kevinmarquette.github.io/2018-12-23-Powershell-null-everything-you-wanted-to-know/?utm_source=rss&utm_medium=blog&utm_content=rss#redirect-output-to-null

     

  • #131939

    Participant
    Points: 1,317
    Helping Hand
    Rank: Community Hero

    If you are running the script by F5 or F8 in the ISE it's supposed to show up in the console, this shows the code is loaded and thus will execute.
    This will happen whether you use cod inside of out side of a function and run from the ISE. This is the same in VSCode, actually this happens with any development tool.

    To prevent cold from showing up in the console on normal runs, then that code must be a function, that is pre-loaded (which when you do this, it will show up in the console at least once.), then run the code using just the function name

    If you are loading the code and running it at the same time, then you can use Clear-Host to remove the code from the screen and only show the results.

    # Load the function
    function My-Function ($param1, $param2)
    {
       'Hello World' 
    }
    
    # Clear the screen
    Clear-Host
    
    # Run the function
    My-Function
    
    
    # Results
    Hello World
    

    Saving the code as a script...

    # Load the function
    function My-Function ($param1, $param2)
    {
       'Hello World' 
    }
    
    # Run the function
    My-Function
    

    ... as a .ps1 and running the .ps1, will load the code and run the function.
    It will not display the function code in the console.

     
    .\My-Function.ps1
    Hello World
    

    If you are not using a function in the script...

    # Send a message
    'Hello World'

    … in the ISE / VSCode, using F5 (Run)…

     
    D:\Scripts\My-Function.ps1
    Hello World
    

    ISE / VSCode – Selecting the code and using F8 (Run selection)

     
    # Send a message
    'Hello World' 
    Hello World
    

    Running this as a script from the console

     .\My-Function.ps1
    Hello World
    
  • #131943

    Participant
    Points: 88
    Rank: Member

    Thanks for those details, very useful. However, how can this be avoided when we have nested loop?  I tried something like this

    ForEach ($file in $list) {
    
    $csvpathSource = $csvpath + $file.name #| Get-Content $csvpathSource
    $content = Import-Csv -Path $csvpathSource -Delimiter ','
    
    ......
                         $content | ForEach-Object {                     mycode here                     } >> Out-Null
    } >> Out-Null

    The 1st one worked fine but when execution reached the end of the 2nd loop, I got an error indicating "Out-Null is not a known cmdlet

     

  • #131954

    Participant
    Points: 1,317
    Helping Hand
    Rank: Community Hero

    No worries.
    Yet, I am kind of confused what you are trying to do.

    1 – So you have a list of files which are all .csv files, in the outer ForLoop.
    If these are default .csv, the common is assumed, so no need to add it, though it does not hurt anything to do so.

    This …

    $csvpathSource = $csvpath + $file.name #| Get-Content $csvpathSource
    

    … is not really needed, you can just ask for the full name which gets you the full path to the file, if you are bringing them in from a disk read.

    $List = Get-ChildItem -Path D:\Temp\NewFiles -Filter '*.txt'
    
    ForEach ($file in $list) 
    {
        $file.Fullname
    } 
    
    # Results
    
    D:\Temp\NewFiles\TestFile0.txt
    D:\Temp\NewFiles\TestFile1.txt
    D:\Temp\NewFiles\TestFile2.txt
    

    2 -Then that InnerLoop
    take each CSV and read its content into a variable, then do something.

    # ISE/VSCode F8 (select all) run results

     $List = Get-ChildItem -Path D:\Temp\NewFiles -Filter '*.txt'
    
    ForEach ($file in $list) 
    {
        "`nProcessing $file...`n"
    
        Get-Content -Path $file.FullName | 
        ForEach {
            $_ 
        }
    
    } 
    
    Processing TestFile0.txt...
    
    TestFile0
    testnow
    testnow
    testnow
    
    Processing TestFile1.txt...
    
    TestFile1
    testnow
    testnow
    testnow
    
    Processing TestFile2.txt...
    
    TestFile2
    test
    test
    test
    

    # ISE/VSCode F5 run results

     D:\Scripts\My-Function.ps1
    
    Processing TestFile0.txt...
    
    TestFile0
    testnow
    testnow
    testnow
    
    Processing TestFile1.txt...
    
    TestFile1
    testnow
    testnow
    testnow
    
    Processing TestFile2.txt...
    
    TestFile2
    test
    test
    test
    
    

    # ISE/VSCode script run results

     .\My-Function.ps1
    
    Processing TestFile0.txt...
    
    TestFile0
    testnow
    testnow
    testnow
    
    Processing TestFile1.txt...
    
    TestFile1
    testnow
    testnow
    testnow
    
    Processing TestFile2.txt...
    
    TestFile2
    test
    test
    test
    

    If you are saying, you have other in this InnerLoop …

        ForEach {
            $_ 
        }
    

    … then the same tiles apply

    Since we have no idea what is in your csv files, and what you are trying to do with it. We are only left to guess, which does not really help you. If you have code that is doing something with the data that is imported, then you should sanitize and show the code you are using. If your code is correct, only results should be displayed.

The topic ‘Avid displaying code in console while running a script in ISE or PS’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort