Can't run powershell as administrator - powershell

I am trying to run as powershell script as administrator on a windows server 2012 r2. The problem is that as soon as I start the script as admin it shutdown with out running it. The script works if I start powershell ISE and run the scripte as admin from there. Is there a setting I am missing?

Maybe execution of scripts is disabled. Try running this first.
set-executionpolicy -unrestricted

I set the cmd on Windows Server 2012 to always run as admin this way:
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\System32\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
REG ADD "HKLM\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\SysWOW64\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
REG ADD "HKU\.DEFAULT\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\System32\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
REG ADD "HKU\.DEFAULT\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\SysWOW64\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
REG ADD "HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\System32\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
REG ADD "HKCU\SOFTWARE\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers" /v "c:\Windows\SysWOW64\cmd.exe" /t REG_SZ /d "RUNASADMIN" /f
Maybe it will work with powershell too.

Related

Deploying Registry Keys Script via Intune

I have written a powershell script to set specific registry keys as a part of the installation of Open VPN. This configures OpenVPN GUI to look at the C:\Program Files\OpenVPN\OpenVPN folder to get it's configuration, amung other configurations.
Here's the script
#Set Registry for Open VPN GUI
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "allow_edit" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "allow_password" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "allow_proxy" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "allow_service" /T REG_SZ /D "0" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "config_dir" /T REG_SZ /D "C:\Program Files\OpenVPN\OpenVPN\config" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "config_ext" /T REG_SZ /D "ovpn" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "connectscript_timeout" /T REG_SZ /D "15" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "disconnect_on_suspend" /T REG_SZ /D "0" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "disconnectscript_timeout" /T REG_SZ /D "10" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "editor" /T REG_SZ /D "C:\WINDOWS\notepad.exe" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "exe_path" /T REG_SZ /D "C:\Program Files\OpenVPN\bin\openvpn.exe" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "log_append" /T REG_SZ /D "0" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "log_dir" /T REG_SZ /D "C:\Program Files\OpenVPN\OpenVPN\log" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "log_viewer" /T REG_SZ /D "C:\WINDOWS\notepad.exe" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "preconnectscript_timeout" /T REG_SZ /D "10" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "priority" /T REG_SZ /D "NORMAL_PRIORITY_CLASS" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "service_only" /T REG_SZ /D "0" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "show_balloon" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "show_script_window" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "silent_connection" /T REG_SZ /D "0" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /V "passphrase_attempts" /T REG_SZ /D "3" /F
When i run the script locally, it does exactly what i need it to do, when deployed via intune it creates the reg keys in different location
When run locally it creates the keys here HKLM\SOFTWARE\OpenVPN-GUI
When deployed via Intune, the Keys are created here HKLM\SOFTWARE\WOW6432Node\OpenVPN-GUI
I undestand from the name that intune will to deploy it as a 32bit app so this could be my problem.
Is there any way round this?
You can either re-launch the script using the 64-bit version of powershell.exe:
if([System.Environment]::Is64BitOperatingSystem -and -not [System.Environment]::Is64BitProcess){
Start-Process $ENV:WINDIR\sysnative\WindowsPowershell\v1.0\PowerShell.exe -File "$PSCommandPath"
exit
}
# rest of script
... or instruct reg.exe to target the 64-bit view of the registry (/REG:64):
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /REG:64 /V "allow_edit" /T REG_SZ /D "1" /F
REG ADD "HKLM\SOFTWARE\OpenVPN-GUI" /REG:64 /V "allow_password" /T REG_SZ /D "1" /F
# etc....

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}"

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

Windows 10 - How to delete temp files for network computer for each users profile

Hi I am trying to create a .bat script which deletes all the temp files in each user's profile and other temp folders
All of the computers (Windows 7, Windows 10) are networked.
Here is what I have done so far
set /P remotepc="What is the Remote PC Name? "
del \\%remotepc%\c$\Temp /S /Q /F
del \\%remotepc%\c$\Temp /S /Q /A:H
FOR /D %%p IN (\\"%remotepc%\c$\Temp\*") DO rmdir "%%p" /s /q
del \\%remotepc%\c$\Windows\Temp /S /Q /F
del \\%remotepc%\c$\Windows\Temp /S /Q /A:H
FOR /D %%p IN ("\\%remotepc%\c$\Windows\Temp\*") DO rmdir "%%p" /s /q
del \\%remotepc%\c$\Windows\Prefetch /S /Q /F
del \\%remotepc%\c$\Windows\Prefetch /S /Q /A:H
FOR /D %%p IN ("\\%remotepc%\c$\Windows\Prefetch\*") DO rmdir "%%p" /s /q
del \\%remotepc%\c$\Users\%USERNAME%\AppData\Local\Temp /S /Q /F
del \\%remotepc%\c$\Users\%USERNAME%\AppData\Local\Temp /S /Q /A:H
FOR /D %%p IN ("\\%remotepc%\c$\Users\%USERNAME%\AppData\Local\Temp\*") DO rmdir "%%p" /s /q
timeout /t 30 /nobreak
pause
In the last section %USERNAME% field only picks up the user currently logged into local machine from where the command is run.
example on Computer "Main" user is "Bravo", when I run the bat file it does this:
C:\Users\bravo\Desktop>del \\network-pc\c$\Users\bravo\AppData\Local\Temp /S /Q /F
The system cannot find the path specified.
C:\Users\sasar\Desktop>del \\network-pc\c$\Users\bravo\AppData\Local\Temp /S /Q /A:H
The system cannot find the path specified.
It runs the first three set of commands on the remote PC and clears off the Temp and Prefetch folder
My main concern is what command do I use in which I should be able to run this command from my computer which deletes all the temp files and skips any file that is in use for all the users on that networked PC.
This will save loads of time regarding doing a cleanup on each users profile one by one.
If there is a way in PowerShell please let me know how to in PS Script.

User account control can not be disabled from command prompt

I want to disable UAC on each client computers remotely via an application which executes commands on computer. When I execute the command below:
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe query HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD
I got this result.
HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System
EnableLUA REG_DWORD 0x0
But when I check UAC manually, I see the UAC is active. What is the reason of this? Can you help to solve this problem? Thank you.
I solved this problem by restarting the computer after running script.
C:\Windows\System32\cmd.exe /k %windir%\System32\reg.exe ADD HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\Policies\System /v EnableLUA /t REG_DWORD /d 0 /f