Now, of course, I can wrap a .ps1 script in say something like a Windows
Script Host script like say (VBScript).....
Set WshShell = WScript.CreateObject("WScript.Shell")
WshShell.Run "MyPowerShellScript.ps1",0
and with the correct .ps1 file association set up it will run invisibly, but
it would be nice to be able to do this with the need for any wrapper at all.
ie something along the lines of
powershell -Invisible <Path to script>
I don't know if the powershell team follow this newsgroup, but that would be
my powershell suggestion of the day, or perhaps the functionality's there
already, and I've just missed it?
--
Jon
just like:
powershell "& 'c:\......\test.ps1'" > $null
powershell "& 'c:\......\test.ps1'" | Out-Null
best wishes
"Jon" <Email_...@SomewhereOrOther.com> wrote:
news:eUf3iXR...@TK2MSFTNGP06.phx.gbl...
ie if I ran a script such as this one (by double-clicking on a .ps1 file),
then all that would be seen would be the MessageBox and no black box....
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Windows.Forms.MessageBox]::Show("Hello")
I suspect that I'll create a simple replacement .exe, whose sole purpose
will be to launch the powershell.exe and the script invisibly, and associate
.ps1 files with that instead. So all is not lost :-) , but it would be
simpler if it were just an extra switch in launching powershell.exe, in
which case I could add it to the registry association for the .ps1
extension.
--
Jon
"Edengundam" <feng...@163.com> wrote in message
news:%23AF%234vU3H...@TK2MSFTNGP03.phx.gbl...
I'm monitoring this DL for a while and really wonder why you are the first
in months to explicitly asking for that – I also miss this functionality.
Since PS is a hostable scripting environment we basically would need a
Windows-based host for PS, analogous to wscript.exe for VBScript.
powershell.exe, as we get it today from MS, is a Console-based host, similar
to cscript.exe for VBScript, so any attempt to get rid of the black box using
some kind of hiding wrapper is just a clumsy workaround but not a clean and
straightforward solution.
Basically any talented and experienced C# programmer (unfortunately that’s
not me) could do that for the PS community since the required interfaces are
available and well documented, but maybe the demand is currently not
expressed as clear as it should be.
Gerd
I'm currently using the 'permanent wrapper' style solution, in that I've
re-associated .ps1 files with an executable that relaunches powershell.exe
invisibly, with the arguments passed to it . I also use a toggle switch
between the 2 ways of running (similar to toggling between the 2 hosts
cscript.exe and wscript.exe).
This works fine, although I had a few hiccups initially with forms not
appearing, but with a few minor modifications to the code (using the 'Run'
method of [System.Windows.Forms.Application] (there may be better ways)) I
managed to resolve these.
I'm not really up to writing a completely new substitute host for powershell
either, which I'm sure would involve a lot of hard work and headaches, but
hats off to anyone who does.
It would be nicer imho if this functionality were implemented from the top
down, in an analogous way to the WScript.exe / CScript.exe model, which
would enable 'black boxless' powershell scripts to be distributed far more
easily, and in turn I'm sure would also contribute to a massive explosion in
the popularity of powershell.
--
Jon
"Gerd Schneider" <GerdSc...@discussions.microsoft.com> wrote in message
news:3C9B0596-D7C4-4205...@microsoft.com...
In the meantime, here is a script that will let you hide the console window.
Do
set-console -hidden
to hide the console and
set-console
to make it visible again. Typical test sequence is:
set-console -hidden ; start-sleep 5 ; set-console
Extract the script from this message and save it as Set-Console.ps1 in your
path.
-bruce
----------------------------cut here--------------------
#
# Sample script showing how to show and hide
# console windows from PowerShell. It uses in-line
# CSharp code to create a ConsoleHelper class to show
# and hide the console.
#
param ([switch] $hidden)
#
# CSharp code for the ConsoleHelper class
#
$code = @'
using System;
using System.Runtime.InteropServices;
public class ConsoleHelper
{
private const Int32 SW_HIDE = 0;
private const Int32 SW_SHOW = 5;
[DllImport("user32.dll")]
private static extern Boolean ShowWindow(
IntPtr hWnd,
Int32 nCmdShow);
[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool AllocConsole();
[DllImport("Kernel32.dll")]
private static extern IntPtr GetConsoleWindow();
public static void HideConsole()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_HIDE);
}
}
public static void ShowConsole()
{
IntPtr hwnd = GetConsoleWindow();
if (hwnd != IntPtr.Zero)
{
ShowWindow(hwnd, SW_SHOW);
}
}
}
'@
#
# Only build the assembly if the type doesn't exist
#
# if this statement fails, then the trap handler will
# compile the code
[ConsoleHelper] > $null
$ch = [ConsoleHelper]
trap {
# Get an instance of the CSharp code provider
$cp = new-object Microsoft.CSharp.CSharpCodeProvider
# And compiler parameters...
$cpar = New-Object System.CodeDom.Compiler.CompilerParameters
$cpar.GenerateInMemory = $true
$cpar.GenerateExecutable = $false
$cpar.OutputAssembly = "custom"
$cr = $cp.CompileAssemblyFromSource($cpar, $code)
# display any errors (there should be none...)
if ( $cr.Errors.Count)
{
$codeLines = $code.Split("`n");
foreach ($ce in $cr.Errors)
{
write-host "Error: $($codeLines[$($ce.Line - 1)])"
$ce | out-default
}
Throw "Compile failed..."
}
else
{
# don't report the exception
continue
}
}
#
# Now use the helper to show and hide the window...
#
if ($hidden)
{
$ch::HideConsole()
}
else
{
$ch::ShowConsole()
}
----------------------------cut here---------------------
--
Bruce Payette [MSFT]
Windows PowerShell Technical Lead
Microsoft Corporation
This posting is provided "AS IS" with no warranties, and confers no rights.
"Jon" <Email_...@SomewhereOrOther.com> wrote in message
news:eUf3iXR...@TK2MSFTNGP06.phx.gbl...
Thanks also for the script. Works great. A number of interesting techniques
in there to learn from - will be exploring that in detail.
Also looking forward to version 2. That's great news
Actually I'm excited about powershell which I may not have conveyed in my
original post, and with a few minor modifications like this, even more so.
It has a massive potential imho, and is quite likely to make technologies
such as VBScript / CScript (which I know and love) to ultimately seem like
childs' play by comparison.
So keep up the good work! Cheers.
--
Jon
"Bruce Payette [MSFT]" <bruc...@microsoft.com> wrote in message
news:uqlQhw23...@TK2MSFTNGP05.phx.gbl...
It's already there: :-)
start-process powershell.exe -Arguments '-noprofile
C:\users\hillr\foo.ps1' -NoWindow -NoShellExecute