Opening multiple powershell scripts same time for user - powershell

I am developing a SQL health check using powershell. So it checks the statuses of databases and outputs text to console whether anything needs attention. i have the first ps1 to load the various checks but i need them to start loading the below ps1's in parralel. I need to work this on powershell v2. Do i need to use start-job for each line
Here is my code
$Output = #()
$BackupHistory = #()
$script = $myInvocation.MyCommand.Definition
$scriptPath = Split-Path -parent $script
Start-Process -FilePath powershell.exe -ArgumentList "-noexit","$scriptpath\SQLHealthVLF.ps1 $Env" -Wait -WindowStyle Maximized
Start-Process -FilePath powershell.exe -ArgumentList "-noexit","$scriptpath\SQLHealthBackup.ps1 $Env" -Wait -WindowStyle Maximized
Start-Process -FilePath powershell.exe -ArgumentList "-noexit","$scriptpath\SQLHealthIOAlerts.ps1 $Env" -Wait -WindowStyle Maximized
Start-Process -FilePath powershell.exe -ArgumentList "-noexit","$scriptpath\SQLDisksUsage.ps1 $Env" -Wait -WindowStyle Maximized
What currently happens is one loads, then i need to close it and then the next one loads. This doesn't give the script consistency.

Related

run multiple bat-files as admin without entering credentials for every bat-file

I have several batch files that I need to run sequentially; every one must be run as admin. This will force me to enter my credentials for every bat-file:
start-process C:\test\test.BAT -verb runas
start-process C:\test\test1.BAT -verb runas
start-process C:\test\test2.BAT -verb runas
Is there a way to run every bat-file, but to enter my credentials just the very fist time?
A nested Start-Process should work. Lasse V. Karlsen suggestion would be the recommended way to go.
Start-Process powershell -Verb RunAs -ArgumentList #'
Start-Process 'C:\test\test.bat'
Start-Process 'C:\test1\test.bat'
Start-Process 'C:\test2\test.bat'
'# -Wait
Or with a loop:
Start-Process powershell -Verb RunAs -ArgumentList #'
'C:\test\test.bat','C:\test1\test.bat','C:\test2\test.bat' |
ForEach-Object { Start-Process $_ }
'# -Wait

How to pass argument to Start-Process

I'd like to run this command
net start "PTV LOXANE xDataServer 1.4.1.067" using Start-Process in powershell with admin rights.
My problem is how to give the quote to ArgumentList.
I've tried this but it doesn't work
Start-Process net -ArgumentList "stop \"PTV LOXANE xDataServer 1.4.1.067\"" -Verb runas -wait -NoNewWindow -PassThru
I've found how to do it. You must double the quotes:
Start-Process net -ArgumentList "start ""PTV LOXANE xDataServer 1.4.1.067""" -wait -PassThru -Verb runas
Now I've got a second question. How can I run this command when calling powershell ?
This doesn't work:
powershell -Command 'Start-Process net -ArgumentList "start ""PTV LOXANE xDataServer 1.4.1.067""" -wait -PassThru -Verb runas'

Calling two new instances and waiting for return

I am having difficulty writing a script that calls two other powershell functions (in different windows) and then waits for them to terminate before proceeding.
I started by using the below (example command) where the command will take an hour or so to complete:
invoke-expression 'cmd /c start powershell -Command {GetSeriesDate -- '2016-01-01'}'
But this doesnt seem to wait for the new window to terminate before continuing.
I also looked into start-job and wait-job but couldnt find a way to make it work, especially with creating two jobs in parallel and waiting for both to complete.
Does anyone have any advice?
UPDATE: Thanks to Bassie I can use the following:
Start-Process -FilePath "cmd.exe" -ArgumentList "/c start powershell -Command {GetSeriesDate -- '2016-01-01'}"
Start-Process -FilePath "cmd.exe" -ArgumentList "/c start powershell -Command {GetSeriesDate -- '2016-01-02'}" -Wait
But while this runs both at the same time it only waits for the latter, and adding a wait option to the first command will cause them to run sequentially... Anyway to get the wait function to include both commands as an AND instead of OR?
You could use Start-Process:
Start-Process -FilePath "cmd.exe" -ArgumentList "/c timeout 5" -Wait
Start-Process -FilePath "cmd.exe" -ArgumentList "/c timeout 5"
Start-Process -FilePath "cmd.exe" -ArgumentList "/c timeout 5"
Here we are calling Start-Process on cmd and then running whatever command we want from that window (in this case I am just sleeping for 5 seconds with timeout to illustrate the -Wait switch).
The second line will not execute until after the 5 seconds, but the third line will not wait (it will run as soon as the second line call is complete).
So in your case you could try:
Start-Process -FilePath "cmd.exe" -ArgumentList "/c start powershell -Command {GetSeriesDate -- '2016-01-01'}" -Wait
Start-Process -FilePath "cmd.exe" -ArgumentList "/c Whatever you need to do next"
Update
If you need both commands to run at the same time, and also wait for both to complete, you can combine this with Start-Job:
$job1 = {Start-Process -FilePath "cmd.exe" -ArgumentList "/c start powershell -Command {GetSeriesDate -- '2016-01-01'}" -Wait}
$job2 = {Start-Process -FilePath "cmd.exe" -ArgumentList "/c Whatever you need to do next"}
Start-Jon -ScriptBlock $job1
Start-Jon -ScriptBlock $job2
Get-Job | Wait-Job | Out-Null
Write-Host "HELLO"
The above will wait until both jobs are complete before write HELLO to the host.
If you want to run them concurrently while also waiting for completion your best bet is to wrap the start-process with a -wait param in a scriptblock and then launch it as a job, then do a get-job | wait-job | receive-job to gather the data (you can also use a while loop for the waiting if you prefer)
Script would look something like below -
$SB1 = {
Start-Process -FilePath "cmd.exe" -ArgumentList "/c timeout 5" -Wait
}
$SB2 = {
Start-Process -FilePath "powershell.exe" -ArgumentList "/c timeout 5" -Wait
}
Start-Job -ScriptBlock $SB1
Start-Job -ScriptBlock $SB2
get-job | wait-job | receive-job

Can't start PowerShell script file with credentials of other user

I have a GUI that has been created with PowerShell Studio and exported as a PS1-file. I'm now trying to launch this GUI by calling it with another user's credentials.
When I run the code it doesn't even give an error message. PowerShell pops-up and closes again in seconds and nothing is launched. Follwoing this thread, I think I followed the correct syntax.
$Script = 'S:\Prod\Script center\GUI Script.ps1'
$Credentials = Get-Credential
$powershellArguments = "-file '$Script'", "-noexit", "-verb runas"
Start-Process powershell -Credential $Credentials -ArgumentList $powershellArguments
These ones doesn't work either:
Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process powershell -verb runas -File 'S:\Prod\Script center\GUI Script.ps1'}"
Start-Process powershell -Credential $Credentials -ArgumentList "-noprofile -command &{Start-Process $script -verb runas}"
And this one is asking me the credentials, although they are already saved in the variable $Credentials. However, the powershell console launched is not launched as the user in the Credentials :(
$cmd = 'powershell.exe'
$arguments = "-NoExit", "-NoProfile", "-WindowStyle Maximized", '-NoLogo', "Credential $Credentials", "File '$script'"
Start-Process $cmd -ArgumentList $arguments -Verb runAs
I'm sure it's not related to the GUI script, because this works perfectly fine:
& 'S:\Prod\Script center\GUI Script.ps1'
Any help is greatly appreciated.
Maybe your error is only on argument single quotes $powershellArguments = "-file '$Script'"; double quotes should be used.
Start-Process -FilePath "powershell" -Credential $cred -ArgumentList #("-file 'cred.ps1'") # doesn't work
Start-Process -FilePath "powershell" -Credential $cred -ArgumentList #("-file ""cred.ps1""") # works

Powershell running as a another user with elevated privileges

I have two scripts located in C:\setup: script.ps1 and script1.ps1.
I want to be able to run the script1.ps1 from withing script.ps1 as another user and with elevated privileges but I cannot make it work. The new powershell window opens but closes immediately ...
here is the script:
$cspath = $MyInvocation.MyCommand.Path
$sfolder = Split-Path $cspath
$spath = Join-Path $sfolder "\Script1.ps1"
$sa = "domain\user"
$sap = "userpassword"
$sasp = ConvertTo-SecureString -String $sap -AsPlainText -Force
$sac = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $sa, $sasp
Start-Process $PSHOME\powershell.exe `
-Credential $sac `
-ArgumentList "-Command Start-Process $PSHOME\powershell.exe -ArgumentList `"'$spath'`" -Verb Runas" -Wait
Any help will be appreciated ...
It looks like you might need to adjust your parameters for powershell.exe. Instead of using -ArgumentList, which I don't think is valid, you should use the -File parameter. Also, you will want to use the -ExecutionPolicy Bypass parameter to ensure that the script execution policy is not interfering.
Finally, I would recommend removing the single quotes from around the script path, as the Windows command interpreter does not understand single quotes to surround parameters.
Give this a try:
$ArgumentList = '-Command Start-Process -FilePath $PSHOME\powershell.exe -ArgumentList "-ExecutionPolicy Bypass -File \"{0}\"" -Verb Runas' -f $sPath;
Start-Process $PSHOME\powershell.exe `
-Credential $sac `
-ArgumentList $ArgumentList -Wait
Update
It seems that some quoting rules were at play here as well, since we are embedding one command inside of another. I wrote and tested a fully function script on PowerShell v4.0.
Here are the contents:
# Create test directory and script file
[void](New-Item -Path c:\test -ItemType Directory -Force);
Set-Content -Path c:\test\test1.ps1 -Value 'Add-Content -Path $PSScriptRoot\blah.txt -Value (Get-Date);';
# Get credential and define script path
$Credential = Get-Credential;
$ScriptPath = 'c:\test\test1.ps1';
# Define the command line arguments
$ArgumentList = 'Start-Process -FilePath powershell.exe -ArgumentList \"-ExecutionPolicy Bypass -File "{0}"\" -Verb Runas' -f $ScriptPath;
Start-Process -FilePath powershell.exe `
-Credential $Credential `
-ArgumentList $ArgumentList -Wait -NoNewWindow;
I can confirm that I get a UAC prompt, and the target script successfully executes.
Since you're concerned about the new session window closing, I'm guessing you want command line output.
Start-Process is working as intended. It will run the script passed in through -ArgumentList and exit the session. This means it will not hold to display command line output - the session will terminate immediately after the process completes.
If you want a persistent session, use New-PSSession. Otherwise, you could export the data you're gathering to a file.