Script not running first time in console, does afterwards - powershell

Hoping you can help as I have run out of ideas and at this point I think it is because I don't understand PowerShell properly.
I read this series; https://foxdeploy.com/2016/05/17/part-v-powershell-guis-responsive-apps-with-progress-bars/ and tried to do the same thing, then I discovered that I could not call my script by opening a normal PowerShell console (Not ISE) and doing .\script.ps1
I also noticed that I could not do the same with the example script located here;
https://github.com/1RedOne/BlogPosts/blob/master/GUI%20Part%20V/PowerShell_GUI_Template.ps1
It does run the SECOND time however and every time after that? For example if you do
.\script.ps1 (doesn't work)
.\script.ps1 (works)
.\script.ps1 (works)
etc...
But if you do
powershell .\script.ps1 (doesn't work)
powershell .\script.ps1 (doesn't work)
However if you close ISE, open it again and run script it works first time?
I don't really know what is causing this.
Does anyone have any ideas?
If you would like to see this just copy from github link as that one has same problem as mine.

The issue is due to line 39-41 in the script on GitHub:
$syncHash.Window=[Windows.Markup.XamlReader]::Load( $reader )
[void][System.Reflection.Assembly]::LoadWithPartialName('presentationframework')
As you can see, presentationframework.dll is not being loaded into the appdomain until after XamlReader.Load() is being called.
The entire rest of the script is dependant on the output from this call, so the entire thing fails.
Second time around, the presentationframework assembly has already been loaded, so the call succeeds and the Window is created correctly.

Related

Run a VBScript using Start-Process

I am trying to run a VBScript using some code I already have written for generically running executables. I have used it successfully with EXE & BAT files, but VBS files are giving me headaches.
I found this that does it with CMD and Invoke-Expression, and this that does it with &. But I have found no references to doing it with Start-Process, and when I try something simple like
Start-Process wscript.exe -ArgumentList:"UNC PATH TO HelperTest.vbs"
I get
Execution of the Windows Script Host failed. (Not enough storage is
available to complete ths operation. )
But I can run the super simple VBS file directly no problem. This only happens when the VBS is on a network share. Running it locally works fine. Also, not including a fully qualified path works fine, as in
Start-Process wscript.exe -ArgumentList:"HelperTest.vbs"
So it's not an actual permissions issue on the share. Am I just bumping in to some obscure bug and I am not going to get this to work, or am I doing something wrong and getting confused by a not very helpful error message?

Powershell not executing from CMD (Batch) script

There are quite some questions asked already regarding this issue, but none of the answers resolved mine.
I wrote following batch script:
powershell.exe -executionpolicy bypass -file "./myps.ps1"
I saved the file as autorun.cmd
Whenever I double-click the cmd file, nothing happens, CLI quickly shows up and closes but powershell was not executed.
If I run the same command directly in CLI, it works.
I am really confused on what's going on here. Any help would be appreciated.
Saving it as a .bat extension will fix it. Like you, I was never able to get a .cmd file to work as a batch file as well.
So, hi and welcome to this platform...
I try run this powershel code above, that convert ansi to unicode file, by bat file using your command inside the bat:
Set-Location ".\"
Get-Content 'ansi.txt' | Set-Content -Encoding unicode 'outunicode.txt'
And all running fine, my bat call to execute .ps1 code (copied from you to test propose), above the code inside bat file, like your code:
powershell.exe -executionpolicy bypass -file "./myps.ps1"
All run OK, no error, and the result file OK too!
So, this make me suggest to you, that maybe you need try running the bat file direct in your command line for to be able to see possible error message, and filling if this is a bat/powershell/security/other error, and, if possible, post/share the content .ps1 file, I'm 100% sure, that here are someone (a lot of them) will be capable help you precisely and quickly. Sorry my English.

My PowerShell script only works when running from ISE

I can't post all of the script contenet, but the basic idea is that it downloads JSON and converts it to objects using the ConvertFrom-Json cmdlet. Some objects are filtered out, and the rest are written to an XML/XLS document (in the Excel 2003 format). This file is then attached to an email and sent to various people.
The problem I'm having is that it only works when run from the Powershell ISE. Once I try setting up a scheduled task, calling it from cmd, or even calling it from powershell, the attached file is completely empty. It is as if some functions do not run (the one that loops through and creates all rows).
I can continue to run from ISE for the time being, but the idea of this script is to send out an automatic email that will require no intervention. Any ideas as to what could be causing this?
You need to run the script "dot sourced"
which can be done like this
powershell.exe -noexit -file c:\test.ps1
or
pwershell.exe -noexit ". c:\test.ps1"
See this link under the -File section
http://technet.microsoft.com/en-us/library/hh847736.aspx
Based on the answer from the comments to the original post, it seems that the script was (unexpectedly?) working in the ISE because of the bug/quirk/feature that allows scripts run in the ISE to be aware of variables used in the console window and vice-versa.
This can often cause logic problems or unexpected results when a script runs with a variable already defined (and not initialized carefully in the script).
Ways to avoid this problem:
Try to test your code in as clean an environment as possible.
http://powershell.com/cs/blogs/tips/archive/2015/02/12/getting-a-clean-powershell-environment.aspx
To make sure a script runs in a completely clean test environment, you
could of course restart the PowerShell ISE. A more convenient way is
to open another PowerShell tab: in the PowerShell ISE, choose File/New
PowerShell Tab.
Use Set-StrictMode 2 in your script to catch undefined variables, etc.
https://technet.microsoft.com/en-us/library/hh849692.aspx
Set-StrictMode -Version 2.0
Prohibits references to uninitialized variables (including uninitialized variables in strings).
Prohibits references to non-existent properties of an object.
Prohibits function calls that use the syntax for calling methods.
Prohibits a variable without a name (${}).
I have had this problem be for and for me executing the scrip using single-threaded function from powershell worked.
You could also try some other options, go to this link to find more info.
Example
powershell.exe -noexit c:\test.ps1 -sta
Change the missing variable or function to global.
function global:mycall{}
Start your Script with:
[cmdletbinding()]

While running a powershell script using installshield, Enable-ADFSEndpoint can not succeed

I am trying to invoke a powershell script from installshield, but I am getting errors on the Enable-ADFSEndpoint line.
When I paste this script into powershell itself, it works properly. But when running through install script's LaunchAppAndWait() function, it fails. Here is my installshield code:
Param1=WINDISK +"\\Windows\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe";
Param2="-ExecutionPolicy remotesigned -File \""+ INSTALLDIR +"PSServer\\Support\\adfs_cmdlets.ps1\"";
LaunchAppAndWait(Param1, Param2, LAAW_OPTION_WAIT);
And the script code is as follows:
Add-PSSnapin Microsoft.Adfs.PowerShell
#ENABLE ENDPOINTS
Enable-ADFSEndpoint –TargetAddress "/adfs/services/trust/2005/windows"
Enable-ADFSEndpoint –TargetAddress "/adfs/services/trust/13/windows"
There is of course more script than this, but upon execution through installshield, it isn't getting further.
I am very much so a novice at installshield, so any hints may help and are much appreciated. Again, it is my current thought that the problem lays within installshield, because when my powershell script is invoked through the powershell command prompt, it works properly. Thank you in advance to anyone that may respond.
I also realize it is likely that I have left out important information, and am of course willing to edit it back in upon request.

Powershell.exe running the script in cli, or a wrapper?

I have a third-party application that's extensible by adding exe-files that perform dataconversion etc. I've written an extension in Powershell that does the conversion I want, but I'm unable to get the third-party app to run my ps1, as it will only accept an .exe file as an extension.
The app adds a filename as the first (and only) commandline argument to the extension, so the command it runs looks like:
someprogram.exe somefile.xml
I tried to get it to run Powershell.exe with my script as an argument, but I haven't been able to figure out how and if that's possible. Some stuff I tried like
powershell.exe myscript.ps1
won't work. I tried getting the script to find the correct XML file itself, but still somehow I couldn't get Powershell to run off the commandline and take a script as an argument and run it.
Next I thought about writing a small .exe file that only runs the Powershell script, but is that even possible? If it is, could someone nudge me in the right direction?
Powershell wants to have a qualified path to script files before it will run them. So you need to either use
powershell.exe .\myscript.ps1
if it lies in the current working directory (unlikely and prone to break for this use case) or use the full path to the script:
powershell.exe C:\Users\Foo\Scripts\myscript.ps1
or similar.
You can also try Powershell.exe -Command "& your-app.exe ${your-arguments}