Powershell script in Notepad works but not as .ps1

Welcome Forums General PowerShell Q&A Powershell script in Notepad works but not as .ps1

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

 
Participant
3 weeks, 5 days ago.

  • Author
    Posts
  • #124076

    Participant
    Points: 46
    Rank: Member

    Hi, I have a powershell script, that I created in word, which opens O365 and gets mailbox sizes etc automatically, then closes.  To run it, I normally copy the text into powershell straight from Notepad, and it all works as expected.  I thought I'd change it to a .ps1 extension, and just pull it directly into powershell, but I'm getting 403 event errors, and the powershell window opens and closes so rapidly I can't get a handle on the error?

    Any ideas?

     

    thanks

    Jason

    
    $Path = "F:\Data\2 – Reports\XXXXX"
    $Password = Get-Content $Path\O365Pwd.txt | ConvertTo-SecureString
    $User = Get-Content $Path\O365User.txt
    $Credential = New-Object System.Management.Automation.PsCredential($User,$Password)
    
    Import-Module MsOnline
    
    Connect-MsolService -Credential $Credential
    $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection
    
    Import-PSSession $Session
    $H = hostname
    $Date = ((Get-Date).ToString('dd-MM-yyyy'))
    
    

    this is part of the code (I have the O365 modules installed, and as stated, if i copy direct from Notepad into powershell works fine)

  • #124077

    Participant
    Points: 46
    Rank: Member

    sorry typo – I created the original script in Notepad!

  • #124088

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    Can you post the error message ?

  • #124097

    Participant
    Points: 46
    Rank: Member

    I don't actually get to see the error, the window closes too quickly...but I have the following from EventViewer:

    
    Log Name: Windows PowerShell
    Source: PowerShell
    Date: 12/11/2018 13:17:54
    Event ID: 403
    Task Category: Engine Lifecycle
    Level: Information
    Keywords: Classic
    User: N/A
    Computer: Jason-XXXX
    Description:
    Engine state is changed from Available to Stopped.
    
    Details:
    NewEngineState=Stopped
    PreviousEngineState=Available
    
    SequenceNumber=15
    
    HostName=ConsoleHost
    HostVersion=5.1.17134.228
    HostId=152fe65e-9439-433e-a9ab-2d17e5bb7a1c
    HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe F:\Data\2 – Reports\XXXX\4 – XXXX O365 Mailbox List Report.ps1
    EngineVersion=5.1.17134.228
    RunspaceId=6f2a43f8-2764-4d49-9158-c31bddc726fc
    PipelineId=
    CommandName=
    CommandType=
    ScriptName=
    CommandPath=
    CommandLine=
    Event Xml:
    
    
    
    403
    4
    4
    0x80000000000000
    
    3492
    Windows PowerShell
    JasonXXXXX
    
    
    
    Stopped
    Available
     NewEngineState=Stopped
    PreviousEngineState=Available
    
    SequenceNumber=15
    
    HostName=ConsoleHost
    HostVersion=5.1.17134.228
    HostId=152fe65e-9439-433e-a9ab-2d17e5bb7a1c
    HostApplication=C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe F:\Data\2 – Reports\XXXX\4 – XXXX O365 Mailbox List Report.ps1
    EngineVersion=5.1.17134.228
    RunspaceId=6f2a43f8-2764-4d49-9158-c31bddc726fc
    PipelineId=
    CommandName=
    CommandType=
    ScriptName=
    CommandPath=
    CommandLine=
    
    
    
    
  • #124104

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    Can't see anything which can close the shell, can you call the script from an existing console or debug it in PowerShell ISE or VSCode.

  • #124113
    js

    Participant
    Points: 315
    Helping Hand
    Rank: Contributor

    Try putting pause at the end. Are you running it on another computer? The convertto-securestring won't work without a key in that case.

  • #124125

    Participant
    Points: 46
    Rank: Member

    hi, it's all from the same computer (my laptop) with access to the same pwd files etc.  even adding a pause doesn't stop the windows closing fairly rapidly! so hard to diagnose.

     

    Thanks for all your help – I'll try ISE and see if that works!

  • #124133

    Participant
    Points: 46
    Rank: Member

    running inside ISE – works as expected, it's just dropping the .ps1 into Powershell standard that's got the problem...Confuseddotcom!

  • #124137

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    Is there any code in this which is copied from some other source ? hidden special chars will give problems.

  • #124140
    js

    Participant
    Points: 315
    Helping Hand
    Rank: Contributor

    How exactly are you running the ps1 file? A .lnk shortcut with "powershell -file c:\users\me\script.ps1"?

  • #124145

    Participant
    Points: 46
    Rank: Member

    Hi Js,

    I've opened the notepad script, then saved it as a .ps1.  After that I just dragged it into Powershell's shell.  I have also right clicked and 'run with powershell'...both do not run.  But if I drag it into ISE, it works as expected!

  • #124149

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    can you add Start-Transcript c:\temp\temp.log at the very beginning and execute once.

  • #124151

    Participant
    Points: 46
    Rank: Member

    Thanks Kvprasoon, will try that now!

  • #124152

    Participant
    Points: 46
    Rank: Member

    Sadly, doesn't even log an event, just shuts donw poershell – far too quickly for me to grab anything useful!

  • #124175

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    What is your execution policy ?

    Do a Get-ExecutionPolicy

  • #124182

    Participant
    Points: 46
    Rank: Member

    hi, it's 'unrestricted', as I checked that too, just in case it was 'restricted'.

  • #124184

    Participant
    Points: 814
    Helping Hand
    Rank: Major Contributor

    Is there any code in this which is copied from some other source ? hidden special chars will give problems.

    Did you check this ?

  • #124187

    Participant
    Points: 46
    Rank: Member

    Hi,

     

    No hidden code, this is a script that I created from scratch, and as stated earlier, if I just copy from notepad, works fine, also in ISE.  reall head scratching on this...wanted to just drag drop and run!

     

    • #124199
      js

      Participant
      Points: 315
      Helping Hand
      Rank: Contributor

      It works for me dragging a ps1 file to a lnk file that points to "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe", but the window will close when it's done. Putting in pause worked to keep the window open. Here's an example accent.ps1 script in the default "ansi" encoding:

      'información'
      pause
      

      You can also add -noexit to the end of the powershell command in the shortcut.

      Or you can drag the file to an open powershell window; it will paste the fullname of the script, and you can press return and run it without the window closing.

  • #124190

    Participant
    Points: 266
    Helping Hand
    Rank: Contributor

    I think you'll probably find that Notepad usually saves txt files in ANSI encoding, whereas PowerShell will expect UTF8 of some form... this usually isn't a problem, but it can occasionally cause oddities like this.

    • #124191
      js

      Participant
      Points: 315
      Helping Hand
      Rank: Contributor

      This will probably get off topic, but I've taken an interest in encoding.

      Powershell 5.1:
      I believe the default encoding is "ansi" (Windows-1251), definitely not ascii.
      Except out-file (including ">" and ">>") and tee encode by default in "unicode" (utf16).
      Cannot decode utf8 no bom ("no signature") (at least special characters)
      Can decode utf8 with bom

      Powershell 6:
      Default encoding is utf8 no bom (there's a way to reliably identify it)

  • #124535

    Participant
    Points: 32
    Rank: Member

    What happens if you open PowerShell, set-location to the location of the script you've saved and invoke it from the prompt?

    PS C:\MyDir> .\MyScript.ps1

     

You must be logged in to reply to this topic.