This topic contains 21 replies, has 5 voices, and was last updated by
November 12, 2018 at 1:29 pm #124076
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?
$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)
November 12, 2018 at 1:31 pm #124077
sorry typo – I created the original script in Notepad!
November 12, 2018 at 1:42 pm #124088
Can you post the error message ?
November 12, 2018 at 1:58 pm #124097
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=
November 12, 2018 at 2:43 pm #124104
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.
November 12, 2018 at 3:02 pm #124113
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.
November 12, 2018 at 3:11 pm #124125
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!
November 12, 2018 at 3:14 pm #124133
running inside ISE – works as expected, it's just dropping the .ps1 into Powershell standard that's got the problem...Confuseddotcom!
November 12, 2018 at 3:18 pm #124137
Is there any code in this which is copied from some other source ? hidden special chars will give problems.
November 12, 2018 at 3:20 pm #124140
How exactly are you running the ps1 file? A .lnk shortcut with "powershell -file c:\users\me\script.ps1"?
November 12, 2018 at 3:22 pm #124145
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!
November 12, 2018 at 3:27 pm #124149
can you add Start-Transcript c:\temp\temp.log at the very beginning and execute once.
November 12, 2018 at 3:37 pm #124151
Thanks Kvprasoon, will try that now!
November 12, 2018 at 3:39 pm #124152
Sadly, doesn't even log an event, just shuts donw poershell – far too quickly for me to grab anything useful!
November 12, 2018 at 4:15 pm #124175
What is your execution policy ?
Do a Get-ExecutionPolicy
November 12, 2018 at 4:21 pm #124182
hi, it's 'unrestricted', as I checked that too, just in case it was 'restricted'.
November 12, 2018 at 4:45 pm #124184
November 12, 2018 at 4:49 pm #124187
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!
November 12, 2018 at 5:31 pm #124199
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:
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.
November 12, 2018 at 5:07 pm #124190ParticipantTopics: 2Replies: 466Points: 1,058Rank: Community Hero
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.
November 12, 2018 at 5:19 pm #124191
This will probably get off topic, but I've taken an interest in encoding.
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
Default encoding is utf8 no bom (there's a way to reliably identify it)
November 13, 2018 at 11:33 pm #124535ParticipantTopics: 4Replies: 11Points: 34Rank: 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
The topic ‘Powershell script in Notepad works but not as .ps1’ is closed to new replies.