1
votes

I'm working on an automaton script for use by our end users in SCCM. Everything is working the way i want it to except for the section for launching outlook with the /cleanviews switch in a hidden window.

Current script section

$path = "C:\Program Files (x86)\Microsoft Office\root\Office16"

$path2 = "C:\Program Files\Microsoft Office\root\Office16"

Test-path $path
if($True){

Start-Process -workingdirectory $path OUTLOOK.EXE /cleanviews -WindowStyle Hidden -ErrorAction Ignore}

Else {
Start-Process -workingdirectory $path2 OUTLOOK.EXE /cleanviews -WindowStyle Hidden -ErrorAction Ignore}

Start-Sleep -s 15

get-process OUTLOOK -ErrorAction ignore | stop-process

Start-Sleep -s 5

I've also tried removing the -working directory portion, and moving -windowstyle from the end to directly after start-process. So far the application is launching in a normal window

There is another script section after this portion and the reason i don't want our users to see the second window during the cleanviews switch is because they could easily freak out with it closing again and or interrupt the script.

suggestions?

1

1 Answers

0
votes

just to start, the

Test-path $path
if($True){}

bit makes no sense and will always return true, i'm assuming this was put in for testing?

Second, some applications will ignore the -WindowStyle part, you can however do this:

Start-Process -FilePath "C:\Program Files\Microsoft Office\root\Office16\OUTLOOK.EXE"

$Win32ShowWindowAsync = Add-Type –memberDefinition @” 
    [DllImport("user32.dll")] 
    public static extern bool ShowWindowAsync(IntPtr hWnd, int nCmdShow); 
“@ -name “Win32ShowWindowAsync” -namespace Win32Functions –PassThru

$Show = 1 ; $Hide = 0 ; $TimeOut = 0
do{
    Get-Process 'outlook' | % {
        $Hidden = $Win32ShowWindowAsync::ShowWindowAsync($_.MainWindowHandle,$Hide)
    }
    Start-Sleep -Milliseconds 100
    $TimeOut++
} while (!$Hidden -or $TimeOut -gt 100)

which is pretty ugly, but PowerShell doesn't have a native way to hide an existing process window.