Powershell doubt


  1. Posts : 501
    windows 10 (x64) Home 20H2 19042.844
       #1

    Powershell doubt


    I have created a script in D:\Data\Lvg\gadgets\Laptops\Docs\powershell called myfirstfunction.ps1. I copied the same file as mysecfunc.ps1 in C:\. As you can see in following pic, .ps1 in C:\ works when I run it. But myfirstfunction.ps1 does not run either from C:\ or D:\Data\Lvg\gadgets\Laptops\Docs\powershell .
    Powershell doubt-psdoubt.png
    Any reason?
      My Computer


  2. Posts : 16,912
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #2

    Do you mean it does not run or the PowerShell window opens and then closes?

    Your script looks like it's running in the PowerShell ISE UI that you show a diagram of but there is nothing to keep the window open after your script completes.

    There are lots of choices for this -
    1 Just add a final line pause just as you might with a batch file
    2 Add a PowerShell command to achieve the same thing - there are lots of choices

    For example, you could use this

    Code:
    Function Get-Hello ($param1)
    {
        Clear-Host
        Write-Output "Hello, $param1!"
        Write-Host "Press any key to close this PS window
        $x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
    }
    Get-Hello("World")
    The reason you are seeing differences in behaviour, "As you can see in following pic, .ps1 in C:\ works when I run it", is that PowerShell ISE is running the script with that environment staying open [just as a command might run in a Command prompt without closing its window].

    By the way, PowerShell ISE will run the function correctly and your .ps1 will run everything correctly. The ISE does not like the ReadKey method so you'll see error responses only when testing within ISE. Using pause instead of both the write-host & $x lines does not suffer from this ISE problem.

    Denis
      My Computer


  3. Posts : 16,912
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #3

    I can see that I missed part of your question.

    When, in the ISE, you can type in
    .\
    you should see a list of the files in the current folder so that you can select your desired .ps1 file.
    - If you do not see the list then the .ps1 file would seem to be in a different folder instead.
    - But the response in that case would be an error response telling you that it cannot recognise it.
    - I have just been playing around and I cannot recreate the exact symptoms shown in your diagram.
    - I suggest that you test the .ps1 with the ISE closed just to prove that it does run properly. Then you'll be able to focus on its interaction with the ISE as the main problem.

    Denis
      My Computer


  4. Posts : 16,912
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #4

    This is a better pause function for PS to add in when you have solved the central ISE problem

    Code:
    Function PSPause ($message)
    {
        # Check if running Powershell ISE
        if ($psISE)
        {
            Add-Type -AssemblyName System.Windows.Forms
            [System.Windows.Forms.MessageBox]::Show("$message")
        }
        else
        {
            Write-Host "$message" -ForegroundColor Yellow
            $x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        }
    }
    PSPause("Press any key to finish")
    I copied it from https://stackoverflow.com/questions/...ey-to-continue

    Denis
      My Computer


  5. Posts : 501
    windows 10 (x64) Home 20H2 19042.844
    Thread Starter
       #5

    Try3 said:
    This is a better pause function for PS to add in when you have solved the central ISE problem

    Code:
    Function PSPause ($message)
    {
        # Check if running Powershell ISE
        if ($psISE)
        {
            Add-Type -AssemblyName System.Windows.Forms
            [System.Windows.Forms.MessageBox]::Show("$message")
        }
        else
        {
            Write-Host "$message" -ForegroundColor Yellow
            $x = $host.ui.RawUI.ReadKey("NoEcho,IncludeKeyDown")
        }
    }
    PSPause("Press any key to finish")
    I copied it from https://stackoverflow.com/questions/...ey-to-continue

    Denis
    Thanks Try3.
    Though initially it was not working, when I restarted laptop, it started working in all places.
      My Computer


  6. Posts : 16,912
    Windows 10 Home x64 Version 22H2 Build 19045.4170
       #6

    That's interesting.

    Last week I found that some PowerShell test scripts were rejected on one of my computers [it started to bring up a security challenge for local scripts even though the execution policy was RemoteSigned]. The next day I restarted that computer then, later on, I repeated the test scripts to make sure I had recorded the problem accurately. All the test scripts ran properly. I know of no reason why our PowerShell scripts should yield varying responses before / after reboots.

    Curious.

    Denis
      My Computer


 

  Related Discussions
Our Sites
Site Links
About Us
Windows 10 Forums is an independent web site and has not been authorized, sponsored, or otherwise approved by Microsoft Corporation. "Windows 10" and related materials are trademarks of Microsoft Corp.

© Designer Media Ltd
All times are GMT -5. The time now is 13:37.
Find Us




Windows 10 Forums