Executing a scriptblock via startprocess - powershell

Would any of you possibly know why this is not working?
Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit -Command & `"{$outvar1 = 4+4; `"out: $outvar1`"}`"" -Wait
The ultimate purpose for this is so that i can run a script block as another user with the addition of the -Credential option. But i can not get this simple script block to work yet.
Many thanks.
Chris.

Here is somthing that is working:
PS C:\> Start-Process $PSHOME\powershell.exe -ArgumentList "-NoExit","-Command `"&{`$outvar1 = 4+4; `"write-output `$outvar1`"}`"" -Wait
-ArgumentList is an array of strings
$outvar is interpreted so I use `$outvar

Related

Getting MSI exit code when installing remotely using PowerShell

I use the following example to install some software on a remote computer
I would like to get the exit code back from the MSI so I can determine if successful or if not what the error was
I assumed $result would contain the information I need but I am obviously missing something
Ideas please?
$Result = Invoke-Command -computername MYREMOTEPC -ScriptBlock { Start-Process "msiexec" -ArgumentList "/i C:\tmp\MYSOFTWARE.msi /quiet /norestart" -Wait -Passthru }
Enclose the Start-Process -Passthru command in (...).ExitCode:
$Result = Invoke-Command -computername MYREMOTEPC -ScriptBlock {
(
Start-Process "msiexec" -ArgumentList "/i C:\tmp\MYSOFTWARE.msi /quiet /norestart" -Wait -PassThru
).ExitCode
}
-PassThru instructs Start-Process to output a System.Diagnostics.Process instance representing the launched process, and given that -Wait is also used, its .ExitCode property can be accessed right away.

How to pass args to aspnet_regiis via powershell

I have 2 scripts:
Launch.ps1
Deploy.ps1
Launch simply runs deploy as administrator:
clear
$scriptPath = split-path -parent $MyInvocation.MyCommand.Definition
$scriptPathToRun = "$scriptPath\Deploy.ps1"
Start-Process -Verb runAs PowerShell -ArgumentList '-noexit','-File', $scriptPathToRun
I am trying to pass arguments to aspnet_regiis, I have tried the following:
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList '–ga', 'domian\serviceAccount'
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList '–ga domian\serviceAccount'
Start-Process -NoNewWindow "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" -ArgumentList #('–ga', 'domian\serviceAccount')
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" '–ga domian\serviceAccount'
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" '–ga', 'domian\serviceAccount'
& "$env:windir\Microsoft.NET\Framework64\v4.0.30319\aspnet_regiis" #('–ga', 'domian\serviceAccount')
In all these attempts, aspnet_regiis is run but it appears no args are passed to it because the output is just a listing of available aspnet_regiis parameters.
Can someone point out what I'm missing? Thanks.
The simplest answer is probably to just run the command using the call/invocation (&) operator:
& "$env:SystemRoot\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis" -ga domain\serviceAccount
If you really wanted to use Start-Process, you should be able to write it this way:
Start-Process "$env:SystemRoot\Microsoft.Net\Framework64\v4.0.30319\aspnet_regiis" "-ga","domain\serviceAccount" -NoNewWindow
The first token on that command line is the executable to run (i.e., -FilePath). The -ArgumentList parameter is an array (i.e., "-ga","domain\serviceAccount").

Using Invoke-Command to run Start-Process in an elevated session

As a precursor to running an installation file on several remote servers, I need to update the Powershell setting MaxMemoryPerShellMB. This requires running a PS session as Administrator on the remote server. I have been trying to run Invoke-Command which then runs a ScriptBlock consisting of a Start-Process command which includes the -Verb RunAs parameter. Nothing seems to work, however.
I have tried with various quoting schemes, single, double, triple, but nothing seems to work.
I've tried running the Start-Process from an Enter-PSSession, with the same results.
Following is the code I'm testing now:
$creds = Get-Credential -Username 'DOMAIN\userID' -Message "Enter Username and Password to access the remote servers."
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList """Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024""" -Verb RunAs -Wait
}
Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
I should be able to RDP to the remote server and run Get-Item WSMan:\localhost\Shell and have it show the updated value, but the value isn't changed.
When running the code it pauses for a second when the Invoke-Command runs, but other than that, there is no feedback in Powershell.
On the remote server I see the following two Kerberos errors in the System Event log.
0x19 KDC_ERR_PREAUTH_REQUIRED,
0xd KDC_ERR_BADOPTION
Any help is greatly appreciated.
> powershell.exe -?
...
EXAMPLES
...
PowerShell -Command "& {Get-EventLog -LogName security}"
-Command
...
To write a string that runs a Windows PowerShell command, use the format:
"& {<command>}"
where the quotation marks indicate a string and the invoke operator (&)
causes the command to be executed.
So you could try to call Set-Item in the following way:
$ScriptBlock = {
Start-Process -FilePath Powershell.exe -ArgumentList "-Command"," &{ Set-Item WSMan:\localhost\Shell\MaxMemoryPerShellMB 1024 }" -Verb RunAs -Wait -PassThru
}
$process = Invoke-Command -ComputerName testsvr01 -Credential $creds -ScriptBlock $ScriptBlock
$process.ExitCode
I'm also returning a process object via -PassThru on which you might check the `ExitCode``
Hope that helps

Installing Software Using PowerShell Invoke Command

$cs = New-PSSession -ComputerName MACHINE -Credential DOMAIN\admin
Copy-Item -Path C:\Scripts\smart -Destination C:\smart -ToSession $cs
msiexec /i "C:\Smart\SMART.msi" NB_PROD_KEY=NC-2ADA2-F9RKE-AKAIA-BBB ACTIVATE_LICENSE=1 INSTALL_INK="" LAT_CONTENT="" PRINT_CAPTURE="" INSTALL_DOCCAM_DRIVERS="" CUSTOMER_LOGGING=1 /qnT="" INSTALL_SPU=2 CUSTOMER_LOGGING=0 /qn
Hi,
I'm struggling to get the syntax that runs with the MSI working above - I've worked with switches inside script blocks which invoke commands beforfe successfully but, not with those parameters which are from the program vendors help file.
I also tried:
Start-Process "msiexec.exe" -Argumentlist "/i "C:\smartmsi\SMART.msi" `
NB_PROD_KEY=NC-2ADA2-F9RKE-AKAIA-BBB ACTIVATE_LICENSE=1 INSTALL_INK="" LAT_CONTENT="" PRINT_CAPTURE="" INSTALL_DOCCAM_DRIVERS="" CUSTOMER_LOGGING=1 /qn
Totally confused how to install using the vendors commands within POwerShell, how can i nest each argument if it's not a switch?
I also tried using Splatter:
$params = '/i', "C:\smartmsi\SMART.msi",
'NB_PROD_KEY=NC-2ADA2-CEAM7-F9RKE', 'ACTIVATE_LICENSE=1',
'/qn'
& msiexec.exe #params
$LastExitCode
No joy - this app will install remotely as a regular install.
Thanks in advance
UPDATE:
Now, i've also tried this:
invoke-command -Session $session -ScriptBlock {
Start-Process -FilePath C:\windows\system32\msiexec.exe `
-ArgumentList "/i `"C:\smart\SMARTSuite.msi`" `"NB_PROD_KEY=NC-2ADA2`" ACTIVATE_LICENSE=1 INSTALL_INK=`"`" LAT_CONTENT=`"`" PRINT_CAPTURE=`"`" INSTALL_DOCCAM_DRIVERS=`"`" CUSTOMER_LOGGING=1 /qn"
}
Still not working. Installer appears for a second then drops off.
You have to escape `" if you want them to be interpreted inside a string which already uses double quotes else you break the string chaining :
Start-Process -FilePath msiexec -ArgumentList "/i `"C:\smartmsi\SMART.msi`" NB_PROD_KEY=NC-2ADA2-F9RKE-AKAIA-BBB ACTIVATE_LICENSE=1 INSTALL_INK=`"`" LAT_CONTENT=`"`" PRINT_CAPTURE=`"`" INSTALL_DOCCAM_DRIVERS=`"`" CUSTOMER_LOGGING=1 /qn"
You don't have to escape double quotes if the string is surrounded by simple quotes

argument list command not correct

I used the below that works for wusa.exe but it is not working for pkgMGr.exe - I get an error of "The pkgmgr.exe command is incorrect"
I think it is because of the
WORKS:
Start-Process c:\windows\system32\wusa.exe -ArgumentList "$ItalianHyphenationHotfix /quiet /norestart /log" -Wait
NOT WORKING:
Start-Process c:\windows\system32\pkgmgr.exe -ArgumentList "/ip /m: $ItalianKB2841134Hotfix /quiet /norestart /l:C:\buildlog\complogs\LangPack" -Wait
Any ideas how I can fix this? I tried to put the /ip /m before the -Argument list but that did not work as well. I think it is because of these two commands that is causing the issue.
-ArgumentList should be a string array, so you need to format your parameters as such. Try:
Start-Process c:\windows\system32\pkgmgr.exe -ArgumentList "/ip", "/m:`"$ItalianKB2841134Hotfix`"", "/quiet", "/norestart", "/l:`"C:\buildlog\complogs\LangPack`"" -Wait