running ps1 through cmd file

This topic contains 0 replies, has 1 voice, and was last updated by  Forums Archives 5 years, 8 months ago.

  • Author
    Posts
  • #5304

    by mapache at 2012-11-23 02:16:26

    Hi All,

    As the topic says i'm running a script from a cmd file.
    I have two problems here:
    1- I have a progress bar (write-progress) but when it launched from the cmd it appears in the prompt window and not in the interface window.
    I understand cause i'm calling the powershell.exe so ithough that maybe calling the powershell_ise.exe it works. No just opened the file and not execute it.
    There is a way that i can do it? or maybe create a executable file? i'm running 32 bits powershell on win7

    2- i have savefiledialog, if executed by the cmd file it blocks when i click in the save button but in a collegue computer it works... strange?
    and is working fine when executed by the powershell_ise.

    Any sugestions?

    Thanks in advance
    Mapache

    by nohandle at 2012-11-23 12:01:49

    hi,
    let me check if we use the same terms here: cmd is the window you get when you run cmd. cmd is not the window you get when you run powershell.exe. right?

    1] If you run script in powershell ise and in powershell console you use another host. use $host.name and check for yourself. Both of them has specific features and other things implemented differently, like the write progress you mentioned. As far as I know there is no parameter that runs the script in the ise directly. Although the ISE can be manipulated by scripts by psISE variable, I dont know of any easy way to accomplish what you are trying to do. If you really need a graphical prograss bar, create it by windows forms or better by Windows presentation foundation and write the basic functions for it.
    2] can you describe it in more detail? in which host it works for you, in which it does not, do you recieve any error?

    by mapache at 2012-11-26 00:12:48

    Hi,
    first of all thanks for the answer.

    and yes that's it, i just call an cmd file to run the powershell.
    About the 1] i really don't need but I will try to do it like you said and i let you know.

    2] this is my code for the savefiledialog

    $b = new-object -comobject wscript.shell
    $intAnswer = $b.popup("Would you like to save the results?", 0,"Information",4)
    If ($intAnswer -eq 6) {
    $save = New-Object -TypeName Windows.Forms.SaveFileDialog;
    $save.CreatePrompt = $true;
    $save.OverwritePrompt = $True;
    $save.SupportMultiDottedExtensions = $true;
    $save.ShowHelp = $true;
    $save.DefaultExt = "csv"
    $save.Filter = "CSV (*.csv) | *.csv* | All files (*.*)|*.*";
    $save.FilterIndex = 2;
    $save.title = "Save Csv";
    }
    if($save.ShowDialog() -eq 'OK'){
    $table1 | Export-Csv $save.FileName -noTypeInformation
    }

    I'll try to describe it step by step.

    In powershell_ise.exe the savefiledialog works perfect for tree tested machines.
    In powershell.exe only works for one machine.

    I don't receive any errors it simply blocks. i don't know how to debug through powershell.exe.
    Both 3 machines are the same.
    any idea?

    Thanks
    Mapache

    by Infradeploy at 2012-11-27 12:48:02

    like:
    powershell -file c:\scripts\trythis.ps1

    by mikefrobbins at 2012-11-27 14:58:16

    Add this at the top of your code:
    [System.Reflection.Assembly]::LoadWithPartialName("System.windows.forms") | Out-Null
    Without it I receive the following error when running your code from PowerShell.exe but not the ISE:
    New-Object : Cannot find type [Windows.Forms.SaveFileDialog]: make sure the assembly containing this type is loaded.

    With it I receive the same results in both.

    by mapache at 2012-11-28 00:11:20

    Thanks for the reply.
    Infradeploy: I did that and GridView still blocks without error or warning.

    mikefrobbins: i'm doing that the problem is when you click to save the file(Blocks).

    Okay i will post here a litle test for you to try and to see if you get the same (error?????)
    test.ps1
    [void] [System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
    $table1 = @("tests", "test2", "test3", "test4")

    $b = new-object -comobject wscript.shell
    $intAnswer = $b.popup("Would you like to save the results?", 0,"Information",4)
    If ($intAnswer -eq 6) {
    $save = New-Object -TypeName Windows.Forms.SaveFileDialog;
    $save.CreatePrompt = $true;
    $save.OverwritePrompt = $True;
    $save.SupportMultiDottedExtensions = $true;
    $save.ShowHelp = $true;
    $save.DefaultExt = "csv"
    $save.Filter = "CSV (*.csv) | *.csv* | All files (*.*)|*.*";
    $save.FilterIndex = 2;
    $save.title = "Save Csv";
    ##$save.ShowDialog();
    }

    if($save.ShowDialog() -eq 'OK'){
    $table1 | Export-Csv $save.FileName -noTypeInformation
    }

    test.cmd –> to execute the cmd
    %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe .\test.ps1
    pause()

    when you execute the cmd the dialogbox will block(happens to me) but when lunched from the ise works fine.

    Thanks again
    Mapache

    by Infradeploy at 2012-11-28 06:05:09

    UAC maybe? I do not have another explanation.

    by mikefrobbins at 2012-11-28 07:02:08

    Sounds like your getting beyond this point, but try this just to make sure it's not a script execution policy issue:
    %SystemRoot%\syswow64\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass .\test1.ps1

    by mapache at 2012-11-28 07:34:35

    nope, doesn't work 🙁 i'm desperate

    it's working in one machine.
    and the others are the same model, same OS...

    i don't have the minimum idea 🙁

You must be logged in to reply to this topic.