Windows PowerShell to install NativeScript on Windows 10 - powershell

I try to install NativeScript on Windows 10 from Admin PowerShell console.
https://docs.nativescript.org/start/ns-setup-win
I type this command and obtain the following error:
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))"

You're running the command from PowerShell when you need to run it from CMD command prompt.
Run this from CMD:
#powershell -NoProfile -ExecutionPolicy Bypass -Command "iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))"
Here, #powershell means "don't echo the command and run powershell.exe". It means something completely different in PowerShell itself, where #powershell means "search the path and execute a program with the literal name #powershell before the extension" or "use the variable $powershell as a splat variable" depending on your PowerShell version.
If you're running it in Powershell, you'll need to run just:
iex ((new-object net.webclient).DownloadString('https://nativescript.org/setup/win-avd'))
As always, be extremely careful about running these commands that download arbitrary code and execute it immediately.

Related

Running powershell wihtin standard cmd with parameters?

I have downloaded the dotnet installer PS1 script file from here.
I wish to run it, and MS recommended:
dotnet-install.ps1 -Channel 7.0 -Runtime aspnetcore
Okay, but I have to run this in a non-powershell "cmd", so I'll have to specify "powershell":
powershell ./dotnet-install.ps1 -Channel 7.0 -Runtime
UnauthorizedAccess
powershell ./dotnet-install.ps1 -Channel 7.0 -Runtime -ExecutionPolicy RemoteSigned
UnauthorizedAccess
powershell -ExecutionPolicy RemoteSigned ./dotnet-install.ps1 -Channel 7.0 -Runtime
MissingArgument
How can I write this command to be able to run?
And how can I write if "dotnet-install.ps1" contains whitespace in its path?
To run the installation script from cmd.exe with elevation (as admin) - which will invariably prompt for confirmation / ask for administrator credentials - try the following:
powershell.exe Start-Process -Verb RunAs powershell.exe '-ExecutionPolicy Bypass -NoExit -File ./dotnet-install.ps1 -Channel 7.0 -Runtime aspnetcore'
If your .ps1 script file path contains spaces, enclose it in \"...\" (sic); e.g.,
\"C:\path with spaces\dotnet-install.ps1\"
Note that the elevated script invariably runs in a new window; the -NoExit CLI option in the nested powershell.exe call keeps that window open after the script terminates, which allows you to inspect output.
You may add -Wait to the Start-Process call above to make the calling cmd.exe shell wait for new window to close.
See also:
powershell.exe, the Windows PowerShell CLI.
PowerShell's Start-Process cmdlet, which allows launching processes with elevation via -Verb RunAs.

Powershell: Invoke a session as Administrator

I am trying to run my script as a ADMIN user and install the certificates, but it only works if I run it from the command line as a Administrator. If I trigger my bat file from the other script it is installing the certificates as a local user.
SET PowerShellScriptPath='<FilePath>\File.ps1'
SET CertPath=%~dp0NewCSC.cer
start PowerShell.exe -ExecutionPolicy Bypass -Command "& {Set-ExecutionPolicy Bypass -Force -Scope LocalMachine}" 
timeout 05
powershell.exe -NoProfile -ExecutionPolicy Bypass -Command "& %PowerShellScriptPath% %CertPath%"
Is it coz of I am using Powershell.exe to invoke for 2 times for every line its taking it as a local user? please help me out here.

Run Powershell script from WebClient.DownloadString on the Command Prompt

On the Command Prompt, I want to run a PowerShell script that is stored at a URL.
Here is what I have tried:
powershell -c "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"
powershell -Command "& iex (New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"
powershell -NoProfile -Command "iex ((New-Object System.Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1'))"
powershell.exe -exec Bypass -C "IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1')"
I have ran each of them for 5 minutes and nothing really showed the results I wanted. It displays no error but nothing really happen after waiting.
I want to know why the above scripts does not work as intended?
I will achieve the result I want by typing this instead:
echo IEX (New-Object Net.WebClient).DownloadString('http://192.X.X.X/Sherlock.ps1') | powershell -NoProfile -Command -
My question is similar to:
Run Powershell script from URL without temporary file
Other references:
https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_powershell_exe?view=powershell-5.1
https://gist.github.com/jivoi/c354eaaf3019352ce32522f916c03d70
There is more than one method, but here's a quick one-liner that should do the trick from the command prompt:
powershell -ExecutionPolicy Bypass -Command "[scriptblock]::Create((Invoke-WebRequest "https://gist.githubusercontent.com/ChrisKibble/afea9880a184cd2b2445e5d8408715af/raw/41cbbf042af07136132f09395e4664ffab33e310/gistfile1.txt").Content).Invoke();"
This creates a script block based on the content of a file hosted at a URL.
As to why yours don't work, it's tough to say without debugging it or doing some process monitoring, but my first guess would be something wrong with your PS1 file (try something simple like just a Write-Host).

Powershell: Run script as administrator causes infinite execution

I am completely new to Powershell. Found a script online for updating Windows, and want to run it as administrator. Have a script that starts a new session and calls that script from a network share:
PowerShell.exe -noprofile -command "&{Start-Process PowerShell -ArgumentList
'-noprofile -file \\path\to\networkshare\00_WindowsUpdate.ps1' -Verb RunAs}"
The problem is that it keeps running the script and opening new windows infinitely. I have searched the Internet and could not find anything specific to my issue. How do I run my script to call the Windows Update script once and prevent it from executing infinitely?
Why are you using PowerShell, to call PowerShell, to run PowerShell?
In .cmd:
RUNAS /noprofile /user:domain\user "powershell -ExecutionPolicy Bypass -Command '& \\Path\update.ps1'"

Executing PowerShell from Windows Task Scheduler

I am trying to execute a very simple PowerShell script from Windows Scheduled Tasks.
My PowerShell script is this:
$currentTime = (Get-Date).ToString('yyyy-MM-ddTHH.mm.ss')
$contentToDump = "JLS"
$path = "$home\Desktop\resutfile_" + $currentTime + ".txt"
Add-Content $path $contentToDump
So it simply writes the string "JLS" to a file on my desktop with the current time as part of the name.
If I execute it using Windows PowerShell ISE it works fine. It also works fine when executing using the command prompt.
But when I hook up at Scheduled task it doesn't work. The task returns with a valid ("0x0")-result but no file is generated.
My configuration of the task is this:
Action: Start a program
Program/script: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -Command “&{c:\Users\jls\desktop\untitled2.ps1}”
Start in (optional): C:\users\jls\desktop
The task is set to run as "me" with highest privileges and it is running locally on my machine where I am admin.
What am I missing?
You need to replace the -Command with -File in your Add Arguments
Add Arguments: -NoProfile -NoLogo -NonInteractive -ExecutionPolicy Bypass -File c:\Users\jls\desktop\untitled2.ps1