Powershell - How to use Start-Process to call file from share/pass args in single line

Viewed 77

To preface this, I am self teaching and brand new to scripting in general, let alone powershell. After a cumulative 12 hours, my Google fu has run out.

I had a series of programs tailored to different models of computer we support that ran a staged series of installers from a fileshare. The program would check to see if the tech deploying the software was running it as admin, if not, it used a Start-Process line to elevate and run again.

It worked flawlessly, but we wanted to see if we could remove the need for the tech to enter r to run the scripts from the share.

In trying to figure out how to add -executionpolicy bypass to the arg list for Start-Process, I've hit a wall.

It now errors on trying to call to the fileshare to retrieve the parent script, before getting to the point where I can troubleshoot the bypass can of worms.

Below is my rough framework, remember I'm self taught by googling and using tutorials point.

$principal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if($principal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) 
    {
#usually I have a get-childitem | foreach-object loop here that runs the files from the folder one by one in a specific order,
#it also checks to see if msiexec is running or not before trying to load and install files using a if-else>do-while combo
    }
else 
    {
        Start-Process -FilePath "powershell" -ArgumentList "$('-File "\\server\dir\foo".ps1')$($MyInvocation.MyCommand.Name)$('""')" -Verb runAs
    }#this calls to a script that is a 1:1 copy of the code in the if{} block

This returns an error from the -File parameter that says it can't call the file because it doesn't exist. What am I doing wrong? How do I pass -executionpolicy bypass as an additional arg without breaking it further? Is there a better way to do this? Is there a neater way to automate this?

Please help me geniuses of StackOverflow before I start gnawing on my keyboard.

0 Answers
Related