Powershell start-process hidden wmplayer file with spaces - powershell

powershell Start-Process -WindowStyle Hidden 'C:\Program Files\Windows Media Player\wmplayer.exe' c:\windows\media\notify.wav
run ok.
powershell Start-Process -WindowStyle Hidden 'C:\Program Files\Windows Media Player\wmplayer.exe' c:\windows\media\windows background.wavrun bad.
powershell Start-Process -WindowStyle Hidden 'C:\Program Files\Windows Media Player\wmplayer.exe' 'c:\windows\media\windows background.wav'run bad.
powershell Start-Process -WindowStyle Hidden 'C:\Program Files\Windows Media Player\wmplayer.exe' "c:\windows\media\windows background.wav"run bad.
powershell "& 'C:\Program Files\Windows Media Player\wmplayer.exe' 'c:\windows\media\windows background.wav'" run ok but it's no hidden.
¿How fix the spaces in the arguments, please?

Do you need to do it with wmplayer.exe? The application will not be visible but it will stay loaded as a process to find in taskmanager or Get-Process. The powershell way is more in line with:
powershell (New-Object System.Media.SoundPlayer('c:\windows\media\Windows Battery Critical.wav')).PlaySync()

No visible player:
powershell (New-Object Media.SoundPlayer 'c:\windows\media\windows background.wav').PlaySync()
powershell $PLAYER = New-Object Media.SoundPlayer; $PLAYER.soundlocation='C:\windows\media\windows background.wav'; $PLAYER.PlaySync()
powershell Add-Type -AssemblyName presentationCore; $mediaPlayer = New-Object system.windows.media.mediaplayer; $mediaPlayer.open('c:\windows\media\windows background.wav'); $mediaPlayer.Play(); start-sleep 3
The sound file does not support spaces: *********************
powershell Start-Process -WindowStyle Hidden 'C:\Program Files\Windows Media Player\wmplayer.exe' c:\windows\media\notify.wav & timeout 3 >nul & taskkill /f /im wmplayer.exe>nul
Visible player:
"C:\Program Files\Windows Media Player\wmplayer.exe" "c:\windows\media\windows background.wav" & timeout 3 >nul & taskkill /f /im wmplayer.exe>nul
W10:
start "" "c:\windows\media\windows background.wav" & timeout 3 >nul & taskkill /f /im music.ui.exe>nul
start "explorer.exe shell:" "c:\windows\media\windows background.wav" & timeout 3 >nul & taskkill /f /im music.ui.exe>nul
W11:
start "" "c:\windows\media\windows background.wav" & timeout 3 >nul & taskkill /f /im microsoft.media.player.exe>nul
start "explorer.exe shell:" "c:\windows\media\windows background.wav" & timeout 3 >nul & taskkill /f /im microsoft.media.player.exe>nul

Related

PowerShell registry permission on new computer

I am trying to set up new computers and as it is a new computer it won't allow me to run the PowerShell script as admin. As I can't run it as administrator I can't REG ADD my AutoAdminLogon, DefaultUserName and DefaultPassword. How can I work around this to get my script to allow this and run as admin.
Add-Content -Path "C:\Install Logs\Install.log" -Value "Set up auto login as admin - $(Get-Date)"
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v AutoAdminLogon /t REG_SZ /d 1 /f
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultUserName /t REG_SZ /d AdminIT /f
REG ADD "HKLM\Software\Microsoft\Windows NT\CurrentVersion\Winlogon" /v DefaultPassword /t REG_SZ /d Password /f
Only work around so far that I have found is to make a .bat file and in that force the PowerShell script to run as Admin.
PowerShell.exe -Command "& {Start-Process PowerShell.exe -ArgumentList '-ExecutionPolicy Bypass -File ""%~dpn0.ps1""' -Verb RunAs}"

How do I use robocopy to run a powershell elevated copy with spaces in the directory names?

If you find a better question that encompasses my situation, please let me know. I haven't found my case yet.
I tried running an elevated powershell command with:
powershell -command "somecommand -Verb runas"
as
powershell -command "robocopy \\hostess\blab\foo 'C:\Program Files\foo ' /s /e /r:0 /z -Verb runas"
The robocopy part works by itself but then adding powershell causes:
ERROR : Invalid Parameter #7 : "-Verb"
The verb is a parameter of powershell executable and not part of the command parameter
powershell -command "robocopy \\hostess\blab\foo 'C:\Program Files\foo ' /s /e /r:0 /z" -Verb runas
see also https://ss64.com/ps/syntax-elevate.html

Run command If fails then run the other one if success then end

I'm trying to find a way to uninstall maya2019 from all studio workstation. We have 3 versions of maya2019 with different uninstall commands:
"C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {D4BE10F2-3E2D-4120-863A-765623D53264} /M MAYA /LANG en-us /q
"C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {77067FD9-800C-48B4-803D-569642ADABC5} /M MAYA /LANG en-us /q
"C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {1DB1AEB7-EDBD-4BB1-87DB-26C72576DA42} /M MAYA /LANG en-us /q
I need to make a script that runs the commands, if one fails then it runs the the next one down, if it succeed the it exits with complete and stops.
Right now I have this:
if "C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {D4BE10F2-3E2D-4120-863A-765623D53264} /M MAYA /LANG en-us /q; then
echo success && exit
else
if "C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {77067FD9-800C-48B4-803D-569642ADABC5} /M MAYA /LANG en-us /q; then
echo success && exit
else
if "C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe" /P {77067FD9-800C-48B4-803D-569642ADABC5} /M MAYA /LANG en-us /q; then
echo success && exit
but I'm not sure if I'm going in the right direction?
Thanks for your help
As suggested by Theo in the comments on the question, a more efficient solution would be to query the registry at HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall for uninstallation command lines based on the GUIDs in your commands.
This answer provides pointers as to how to do that.
If that is not an option, here'a PowerShell solution:
foreach ($guid in '{D4BE10F2-3E2D-4120-863A-765623D53264}',
'{77067FD9-800C-48B4-803D-569642ADABC5}',
'{1DB1AEB7-EDBD-4BB1-87DB-26C72576DA42}') {
$exe = 'C:\Program Files\Autodesk\Maya2019\Setup\Setup.exe'
$ps = Start-Process -PassThru -Wait $exe "/P $guid /M MAYA /LANG en-us /q"
if ($ps.ExitCode -eq 0) { "Success"; exit 0 }
}
Write-Warning "Uninstallation failed."
exit $ps.ExitCode
Start-Process -PassThru -Wait starts the installer process, waits for its termination, and then returns a System.Diagnostics.Process instance representing the terminated process, whose exit code (.ExitCode) can then be examined. An exit code of 0 signals success, any other value failure.

Batch file to be converted in powershell script

I am new on powershell, I want to convert the following batch script into powershell. The reason why I want this conversion is because this script will be run on a server from my task scheduler and due to the fact that the cmd is not working with UNC paths I think that could be a good workaround.
This script is checking if there are 20 zip files in a folder and in case they are found then starts a python script to unzip them (mandatory)
for /f %%a in ('dir /b W:\XXX\XXX\*.zip ^| find /c /v ""') do (
if /i %%a EQU 0 EXIT
if /i %%a NEQ 20 timeout /t 300 /nobreak
if /i %%a NEQ 20 Powershell.exe -executionpolicy remotesigned -File W:\XXX\XXX\powershellerrormail.ps1
if /i %%a NEQ 20 EXIT
if /i %%a EQU 20 (python W:\XXX\XXX\SSC_Unzipping.py)
pause)
EXIT

Run Powershell script via batch file with elevated privileges

I need to run a Powershell script to create AD user via a batch file. The thing is I need to run this PS script with elevated privileges (domain admin account). I have tried to script a '.bat' file which encloses all this information but I have been unsuccessful so far. Here is the script :
echo off
cls
echo Sign in with your ADM ID
set /p username=
powershell -noprofile -command "&{ start-process powershell -ArgumentList '-
noprofile -file C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-
Aduser_test.ps1' -verb RunAs}"
I have tried with line /netonly /user:adm#domain but It won't work.
Do you guys have any idea?
Thanks in advance.
I have finally ended up with this :
runas.exe /netonly /noprofile /user:domainadm#domain "powershell.exe -
noprofile -File "C:\Users\...\Desktop\Powershell_scripts\New-
ADuser\.ps1" -verb RunAs"
It works like a charm now!
Hope it will help anyone in need. ;)
you can start powershell with another credentials
#echo off
cls
echo Sign in with your ADM ID
set/P user="* user: "
rem set/P pass="* password: "
set "psCmd=powershell -Command "$pwd = read-host '* password' -AsSecureString; $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pwd); [System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /F "usebackq delims=" %%P in (`%psCmd%`) do set "pass=%%P"
powershell -executionpolicy bypass -Command "$p='%pass%'|convertto-securestring -asplaintext -force;$c=new-object -typename system.management.automation.pscredential('%user%',$p);start-process 'powershell' '-Command "C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-Aduser_test.ps1"' -credential $c -passthru -wait; read-host;"
exit/B
or simply
#echo off
cls
powershell -executionpolicy bypass -Command "start-process 'powershell' '-Command "C:\Users\...\Desktop\Powershell_scripts\New-ADuser\New-Aduser_test.ps1"' -credential $c -passthru -wait; read-host;"
exit/B
that will prompt for credentials