Share via


Start-Process call file from the same folder the script is running from

Question

Tuesday, August 27, 2019 2:31 PM

Hi all

I am using deployment system to deploy this to files to remote computers.

1. Restart.exe

2. RestartScript.PS1

The destination folder on the remote computers is inconsistent and can be different for everyone.

I have to call Restart.exe file in the RestartScript.PS1. 

What's the right way to do it?

RestartScript.PS1:

$restart= invoke-command -scriptblock {test-path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"}
if ($restart -eq $false) {Write-Host "false"}
Else {
Start-Process $PSScriptRoot\Restart.exe -ArgumentList "/t:300 /m:180 /r /f"
}

Thanks

All replies (11)

Tuesday, August 27, 2019 5:06 PM ✅Answered

Why do you still use your blown up and unnecessarily complex code?

If (Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') {
    Start-Process "$PSScriptRoot\Restart.exe" -ArgumentList '/t:300 /m:180 /r /f'
}

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Tuesday, August 27, 2019 9:25 PM ✅Answered | 1 vote

Some  basic PowerShell 101 and PowerShell pre-school things:

Current working direstory = $PWD

Directory script is in when it was executed.

$MyInvocation

$scriptpath = Split-Path $MyInvocation.InvocationName

To execute a script in the current folder:

. $scriptpath  # notice the <dot><space>

Learning basic PowerSHell instead of guessing and askin vague questions will save you a lot of time and frustration.

\(ツ)_/


Tuesday, August 27, 2019 2:42 PM

Have you ever heard about the KISS principle?  ;-)

If (Test-Path -Path 'HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired') {
    Restart-Computer
}

Regardless of that: Please format your code as code using the code posting tool provided on the icon bar of hte post editor (second to last icon).

Thanks.

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Tuesday, August 27, 2019 2:53 PM

Hi BOfH

It is not easy in this case.

I want to run the process and not restart the user's computer.


Tuesday, August 27, 2019 3:05 PM

And this process is named "Restart.exe"?  ... just because I'm curious - what does this process do?

So instead the Restart-Computer cmdlet you can place your line with the Start-Process. What's the problem?

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Tuesday, August 27, 2019 3:35 PM

restart.exe pops up a message for the user and notifies them to restart the computer.


Tuesday, August 27, 2019 4:34 PM

So it does not a restart it does a message box, right? Probably I would have named it MsgBox.exe. ;-)

Anyway ... you don't even have to use Start-Process you can simply run it by calling it with its full path or if it's saved in one folder included in your $ENV:Path variable you can even run it just with its name.

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Tuesday, August 27, 2019 4:46 PM

There is no fixed path.

This file is in the same folder with the script and the path of the folder will be different on each computer and computer.

My script now is like this:

$restart= invoke-command -scriptblock {test-path -Path "HKLM:\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update\RebootRequired"}
if ($restart -eq $false) {Write-Host "false"}
Else {

    Start-Process .\Restart.exe -ArgumentList "/t:300 /m:180 /r /f"

}

When I run it by right-clicking and Run with Powershell it works fine and the restart.exe file opens.

But if I run it by right-clicking and Run with Powershell (Admin) the restart.exe file does not open. What could be the reason?

Thanks


Tuesday, August 27, 2019 5:11 PM

 KISS principle

Thanks :)

what about that?

When I run it by right-clicking and Run with Powershell it works fine and the restart.exe file opens.

But if I run it by right-clicking and Run with Powershell (Admin) the restart.exe file does not open. What could be the reason?


Tuesday, August 27, 2019 8:55 PM

 Run with Powershell (Admin)

AFAIK that's not a regular builtin Windows function. So I don't know that.

You may ask the author of that function or the administrator of your computer.  ;-)

Live long and prosper!

(79,108,97,102|%{[char]$_})-join''


Wednesday, August 28, 2019 6:48 AM

Hi,

Was your issue resolved?

If you resolved it using our solution, please "mark it as answer" to help other community members find the helpful reply quickly.

If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

If no, please reply and tell us the current situation in order to provide further help.

Best Regards,

Lee

Just do it.