Windows sandbox and Powershell : how to make script executable at startup - windows

I followed tutorial to do so :
in wsb:
<LogonCommand>
<Command>powershell -executionpolicy unrestricted -command "start powershell {-noexit -file C:\scripts\start.ps1}"</Command>
</LogonCommand>
in start.ps1 :
Set-ExecutionPolicy Unrestricted -Force
Set-ExecutionPolicy -scope CurrentUser Unrestricted
but when I try to run a script in sandbox, it still complains script is not executable.

Related

Batch script to run Powershell script: Flashing window

System: Windows 10
Powershell Version: 5.1
Purpose: Run a powershell script from a batch file
Parameters: Directory, Filename, Server, Username, Password all being passed in as string encompassed in "" when called from command to handle the situation when there is a space (such as the Directory which currently has a space in)
The Powershell script works perfectly, it creates the credential and starts the RDP connection without issues. When calling from a batch file however the Powershell window flashes and closes immediately. Command Prompt window is run as Administrator.
I've tried using:
- pause
- -noexit
- code from https://blog.danskingdom.com/allow-others-to-run-your-powershell-scripts-from-a-batch-file-they-will-love-you-for-it/
if ($Host.Name -eq "ConsoleHost")
{
Write-Host "Press any key to continue..."
$Host.UI.RawUI.FlushInputBuffer() # Make sure buffered input doesn't "press a key" and skip the ReadKey().
$Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyUp") > $null
}
Clicking on the appearing Powershell window (To try get the select function to pause the window).
None of which have worked.
The code in question is:
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";
Outside of this code no changes have been made to the system, the four speech marks were marked as required to pass through parameters and can be found under the blog post link above at the bottom just before the comments.
There is a high chance i'm using this incorrectly, I am a novice to batch and even newer to powershell. The batch scripts are being made as internal as possible, they need to be able to be used by a base install of Windows. They will eventually be migrated onto versions of Windows Server 2008 and up.
Is there anything that needs to be done with command prompt to allow it to run Powershell code?
Is the powershell code correct for the purpose i'm intending to use it for?
Is there any way, besides the ones listed, to view error, log information or pause the powershell window when run from a batch script?
Any input would be really appreciated!
Edit:
-NoExit variations:
Parent Call
PowerShell **-NoExit** -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";
Nested call
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '**-NoExit** -NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";
Parent and Nested
PowerShell **-NoExit** -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process PowerShell -ArgumentList '**-NoExit** -NoProfile -ExecutionPolicy Bypass -File """"%PowerShellScriptPath%"""" """"%server%"""" """"%username%"""" """"%password%""""' -Verb RunAs}";
Variables set inside batch script as:
SET server=%~1
To remove the quotation marks I have also tried using:
SET server=%1
SET server=%server:"=%

How to automatically add a computer to a domain from a Powershell script using a non-administrator user

I have a number of computers that will need to be added to our domain. The issue is the user that these computers log in with are not administrator users. We do have administrator users on the machines though.
I am trying to have a Powershell script run from a batch script, but nothing I do is giving me the correct results. It seems that even if the Powershell script runs as an administrator, because the batch script is not run as an administrator (it is run automatically on startup), I get a credential error. I've tried running a self-elevating PS script, but to no avail.
Here is one of the batch file lines I've tried:
#echo off
SET thisdir=%~dp0
SET pspath=%thisdir%domain_add.ps1
PowerShell -NoProfile -ExecutionPolicy Bypass -Command "& {Start-Process Powershell -argumentlist '-NoProfile -ExecutionPolicy Bypass -File ""%pspath%""' -verb runas}";
I've even tried using the batch file to start a powershell script, which then calls the add domain PS script:
$user = '.\alohaadmin'
$passfile = 'alohapass.txt'
$password = Get-Content $passfile | ConvertTo-SecureString
$cred = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user, $password
Start-Process 'powershell.exe' -Credential $cred -ArgumentList '-noexit','-file "domain_add.ps1"'
The issue seems to be with the initial batch file not being run as an admin.

Executing file from Admin CMD via CLI

I am trying to execute a file via the CMD with Administrative privileges.
How can I open a cmd via command line with Administrative privileges.
I have to execute a script within a script.
powershell -ExecutionPolicy ByPass -noprofile -command "&{ start-process powershell -ArgumentList '-noprofile -File C:\scripts\install_ims.ps1' -verb RunAs}"
I have tried this in a bat file, but it doesn't work when executed within the script.
This should work fine for your purposes.
powershell -Command "Start-Process <filename> -Verb RunAs"
This is copy-pasted from some Batch files that I finally added to GitHub in the last few days if you have questions, that's probably the best place to go. https://github.com/Benny121221/Batch-BS

Wrapping a PowerShell Program in a Batch File and Using "Set-Location" Returns Root

The following code worked perfectly-
$ScriptLocation = Get-Location
Set-Location "$ScriptLocation"
...until I created a batch file to kick the script off. I understand that by opening the script via the batch file the location will be the root directory.
My question is, how can I set the current directory to the directory where the script is when launching the script from a batch file?
My batch file has the following code-
#ECHO OFF
SET ScriptDirectory=%~dp0
SET ScriptPath=%ScriptDirectory%FilePush_V0.1.ps1
PowerShell -NoProfile -ExecutionPolicy Unrestricted -Command "& {Start-Process PowerShell -ArgumentList '-NoProfile -ExecutionPolicy Unrestricted -File ""%ScriptPath%""' -Verb RunAs}";
set-Location $PSScriptRoot
cd..
See the following for your version of powershell.
whats-the-best-way-to-determine-the-location-of-the-current-powershell-script

ruby exec powershell windows

When I run the following in ruby:
`powershell.exe -executionpolicy unrestricted -command get-module -listavailable`
...the result I get is different from if I just run cmd and execute:
powershell.exe -executionpolicy unrestricted -command get-module -listavailable
How can I fix this, so I can yield the same result?
Yes. The fix apparently is the sysnative alias. Use:
C:/windows/sysnative/windowspowshell/v1.0/powershell.exe
Instead of syswow64 or system32 or none.
For more info see: http://msdn.microsoft.com/en-us/library/windows/desktop/aa384187(v=vs.85).aspx

Resources