Powershell script in Notepad works but not as .ps1

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

[pre]

$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’))

[/pre]

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)

sorry typo - I created the original script in Notepad!

Can you post the error message ?

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

[pre]

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:
<Event xmlns=“http://schemas.microsoft.com/win/2004/08/events/event”>
<System>
<Provider Name=“PowerShell” />
<EventID Qualifiers=“0”>403</EventID>
<Level>4</Level>
<Task>4</Task>
<Keywords>0x80000000000000</Keywords>
<TimeCreated SystemTime=“2018-11-12T13:17:54.887094900Z” />
<EventRecordID>3492</EventRecordID>
<Channel>Windows PowerShell</Channel>
<Computer>JasonXXXXX</Computer>
<Security />
</System>
<EventData>
<Data>Stopped</Data>
<Data>Available</Data>
<Data> 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=</Data>
</EventData>
</Event>

[/pre]

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.

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.

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!

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

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

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

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!

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

Thanks Kvprasoon, will try that now!

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

What is your execution policy ?

Do a Get-ExecutionPolicy

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

Did you check this ?

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!

 

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.

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)