Unable to start process from powershell - windows-services

Folks
I am trying to invoke a batch script from a power shell file and the invocation works fine if executed manually.
Start-Process C:\USR\test.bat
However i created a service in C# which is able to delete and write logs using the powershell script however it simply ignores this step and nothing happens. Is it because this script is invoked by a windows service ?
if (Test-Path \\xxxsharepathfullper\FileWatcher\target\watcher.mon) {
echo "File removed" >> C:\USR\logger.txt
Start-Process C:\USR\test.bat
Remove-Item \\xxxsharepathfullper\FileWatcher\target\watcher.mon
}
else {
}
Execution policy is unrestricted

Check access rights on a share, maybe your admin user don't have rights to access it.

You can try run the process as administrator and log any error:
Start-Process -FilePath "C:\USR\test.bat" -RedirectStandardError "testError.txt" -verb RunAs

Related

Install exe file silently using CMD and PowerShell

I am trying to install an exe created using Inno Setup.
When I try to install it using PowerShell with
Start-Process -wait C:\updatefile.exe /VERYSILENT
all folders are created at proper places, registry values are created, but console hangs and does not return unless Ctrl+C is pressed.
But if I install exe using command prompt with
start /wait C:\updatefile.exe /VERYSILENT
everything goes properly and command prompt returns.
What could be cause of this anomaly?
I need this exe to install through Dockerfile, but as PowerShell install is hanging, container does not install exe. Even with cmd version in Dockerfile, container does not create install folders.
After checking logs I found that exe service is being started by installer in end. If that is reason for hanging, is there some argument I can use to exit PowerShell?
The -Wait switch of PowerShell's Start-Process cmdlet also waits for child processes of the launched process to exit[1], whereas cmd.exe's internal start command does not.
As you note in a later comment, a service executable is launched by your installer at the end; given that services run indefinitely, Start-Process also waiting for that (child) process to terminate indeed explains the hang.
Here is a simple workaround:
cmd /c C:\updatefile.exe /VERYSILENT
When an executable is called via cmd /c, cmd.exe implicitly waits until the executable's process - and only it - terminates, even if it is a GUI application, just like start /wait would.
Given the behavior described in footnote [1], another workaround is to use the -PassThru switch with Start-Process (instead of -Wait) and pipe the result to Wait-Process:
Start-Process -PassThru C:\updatefile.exe /VERYSILENT | Wait-Process
[1] See GitHub issue #15555, which also discusses that Wait-Process acts differently in that only waits for the process itself to terminate.
As zett42 points out, the behavior of Start-Process -Wait is now at least briefly mentioned in the docs as well (emphasis added): "waits for the specified process and its descendants".
try
$arguments = "/Verysilent"
Start-Process -wait C:\updatefile.exe -ArgumentList $arguments

PowerShell command not executed?

In the following PowerShell v2.0 script, the command & is not executed :
$SqlPackageExe = "C:\Program Files (x86)\Microsoft SQL Server\120\DAC\bin\sqlpackage.exe"
Write-Host "Before SqlPackage"
& "$SqlPackageExe" /Action:Publish $SqlPackageParameters
Write-Host "After SqlPackage"
On the console output, I see :
Before SqlPackage
After SqlPackage
I have no error, no log, nothing of the SqlPackage.exe. What's happen ?
My command "& "$SqlPackageExe" /Action:Publish $SqlPackageParameters" works a first time, a second time (database is deployed), and then stops working.
If I close the PowerShell session and open a new PowerShell session, the command works several times, then stops working.
Thank you for your help
You need to redirect StdError and StdOut, see the answer that discusses " -RedirectStandardError -RedirectStandardOutput":
Powershell: Capture program stdout and stderr to separate variables

Script to add network printer to all users x64 bit print server

I use to use this script for Windows XP but since we're doing the switchover to 7 I tried to use it on the new images. It seems like it tries but it doesn't work. I have created a 64bit print server and these machines are 64bit which is the only difference. I read up on this and saw there was a GPO that needed to be set to allow this to work. Which was - Computer Configuration > Administrative Templates > Printers > Allow Print Spooler to accept client connections.
I have tried everything and can't get this to work, it doesn't give me an error or anything. It gives me the prompts for PC name and Printer name, then says 'Adding printer' from the echo command, and just sits there. I can run the command by itself and it doesn't work either....... Please help!
The main thing is that the printer needs to be added from a print server and to the computer for all users as their default.
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
ECHO Adding Printer...
\\ghostserver\installs\pstools\psexec \\%PC% -n 3 cmd /c rundll32
printui.dll,PrintUIEntry /y /ga /c\\%PC% /n\\PRINTSERVER\%PRINTER%
ECHO Restarting Print Spooler...
start /wait sc \\%PC% stop spooler
start /wait sc \\%PC% start spooler
Do you want to install or map the printer from the network ?
First thing to try : map the printer manually on a 7 x64 client. If it fails, your problem is not the batch.
Also try the simple rundll32 printui.dll PrintUIEntry /in n\\PRINTSERVER\%PRINTER% with a non-admin non-elevated account on the client to validate the print server configuration.
Is the "Disallow installation of printer using kernel-mode drivers" GPO disabled ? (Have to be)
Check the firewall settings, UAC/elevation configuration, admin access. Run a gpupdate /force and restart the client.
Check the event log on both the client and print server for any errors.
With an admin account (both print server and client), try to push the installation from the print server.
Have you tried to force adding the provider ? /j "LanMan Print Services"
If you have 2008 servers or DCs, you can use Print Management or Group Policy Preferences to deploy printers (easier than bat+psexec+printui.dll).
If you really want to do it via login script, there are also a bunch of tools in Vista/7/8 for print management in %WINDIR%\System32\Printing_Admin_Scripts, like this one.
Side note : start /wait is inefficient since sc.exe doesn't wait any response of the service. So, if you stop and start without a pause, chances are the service will not be stopped before the restart and skip the second order. You have to simulate a pause (ping 127.0.0.1 -n 5 >nul 2>&1) between stop & start or use a safer script to check the state of the service.
Thank you so much! That fixed it.
Here's my add script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /in /y /ga /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause
Here's my remove script:
#echo off
echo PC Name
set /p PC=
echo Printer Name
set /p PRINTER=
echo Adding Printer...
\\servername\installs\pstools\psexec -s -i -accepteula \\%pc% rundll32 printui.dll PrintUIEntry /gd /n\\PRINTSERVER\%PRINTER%
echo Restarting Print Spooler...
start sc \\%pc% stop spooler
pause
start sc \\%pc% start spooler
pause

Can we register PSSessionConfiguration on remote machine?

Is it possible to register a PS session configuration for machine A from machine B.
Cmdlet "Register-PSSessionConfiguration" works locally only??
First you need to enable PowerShell Remoting on the PC You Want to Access Remotely
In the PowerShell window, type the following command, and then hit Enter:
Enable-PSRemoting -SkipNetworkProfileCheck -Force
Set-Item WSMan:\localhost\Client\TrustedHosts *
Restart-Service WinRM
Test-WsMan $env:computername
If you’re on a home network where you want to go ahead and trust any PC to connect remotely, you can type the following cmdlet in PowerShell (again, you’ll need to run it as Administrator).
Set-Item wsman:\localhost\client\trustedhosts *
Or
Set-Item wsman:\localhost\client\trustedhosts 10.0.0.22
Or
Set-Item wsman:\localhost\client\trustedhosts '10.0.0.22,10.0.0.23'
Register a PSSessionConfiguration
To connect 'Computer1' let us register a session configuration on the server using a domain account.
Just type the following cmdlet and then hit Enter:
Invoke-Command -ComputerName Computer1 -ScriptBlock {Register-PSSessionConfiguration -Name SessionName -RunAsCredential 'domain\mydomainaccount' -Force }
Now I'll use this session configuration the next time I want to run a command on the remote computer.
Invoke-Command -ComputerName 'Computer1' -ScriptBlock { Get-ChildItem-Path \\Computer1\c$ } -ConfigurationName SessionName

PowerShell Start-Process with user in local Administrator does not start elevated PowerShell cmd

I have created a password file for the account with Administration privileges with the following script:
$passwd = Read-Host "Enter password" -AsSecureString
$encpwd = ConvertFrom-SecureString $passwd
$encpwd > "C:\Users\b_steya\Documents\password_b_steya.bin"
Then, in another powershell script I try to run an elevated PowerShell cmd
$encpwd = Get-Content "C:\Users\b_steya\Documents\password_b_steya.bin"
$passwd = ConvertTo-SecureString $encpwd
$cred = new-object System.Management.Automation.PSCredential 'main\b_steya',$passwd
Start-Process PowerShell -Cred $cred -ArgumentList '-noexit','-File','C:\Users\b_steya\Documents\TestAddIISAppPool.ps1'
When executing the second script, I get the following error message:
Import-Module : Process should have elevated status to access IIS
configuration data.
What am I doing wrong? The user "main\b_steya" is added to the local Administrators group of the machine, on wich the script is executed.
Best regards
Yannik
When your second script is executed is it done so from an administrator (runas administrator) powershell session? Even though the user is part of the local admin group you still often need to have the initial process elevated.

Resources