Ignore 'Security Warning' running script from command line - security

I am trying to execute a script from shared folder that I trust:
PowerShell -file "\\server\scripts\my.ps1"
But I get a security warning, and have to press 'R' to continue
Security Warning Run only scripts that
you trust. While scripts from the
Internet can be useful, this script
can potentially harm your computer. Do
you want to run
\server\scripts\my.ps1? [D] Do not
run [R] Run once [S] Suspend [?]
Help (default is "D"): d
Can I ignore this warning? The desired pseudo code I want is:
PowerShell -IGNORE_SECURITY_WARNING -file "\\server\scripts\my.ps1"

This is touched in "PowerShell Execution Policies in Standard Images" on Lee Holmes' Blog and "PowerShell’s Security Guiding Principles" on the Windows Power Shell Blog
.
Summary
Some machines treat UNC paths as the big bad internet, so PowerShell treats them as remote files. You can either disable this feature on those servers (UncAsIntranet = 0,) or add the remote machines to your trusted hosts.
If you want to do neither, PowerShell v2 supports an -ExecutionPolicy parameter that does exactly what your pseudocode wants. PowerShell -ExecutionPolicy Bypass -File (...).

To avoid warnings, you can:
Set-ExecutionPolicy bypass

If you're running into this error from a downloaded powershell script, you can unblock the script this way:
Right-click on the .ps1 file in question, and select Properties
Click Unblock in the file properties
Click OK

Just assign 1 to SEE_MASK_NOZONECHECKS env variable
$env:SEE_MASK_NOZONECHECKS = 1
Start-Process $msi_file_path /qn -Wait | out-null

Try this, edit the file with:
notepad foo.ps1:Zone.Identifier
And set 'ZoneId=0'

None of this worked in my specific instance. What did was changing to a NetBIOS name from the FQDN.
Instead of:
\\server.domain.net\file.ps1
use:
\\server\file.ps1
Using the name bypasses the "automatically detect intranet network" config in IE.
See Option 1 in the blog here:
http://setspn.blogspot.com/2011/05/running-powershell-scripts-from-unc.html

I made this powershell script to unblock all files on a share on my server
Get-ChildItem "\\ServerName\e$\MyDirectory\" -Recurse -File | % {
Unblock-File -Path $_.FullName
}

You want to set the execution policy on your machine using Set-ExecutionPolicy:
Set-ExecutionPolicy Unrestricted
You may want to investigate the various execution policies to see which one is right for you. Take a look at the "help about_signing" for more information.

Did you download the script from internet?
Then remove NTFS stream from the file using sysinternal's streams.exe on command line.
cmd> streams.exe .\my.ps1
Now try to run the script again.

Assume that you need to launch ps script from shared folder
copy \\\server\script.ps1 c:\tmp.ps1 /y && PowerShell.exe -ExecutionPolicy Bypass -File c:\tmp.ps1 && del /f c:\tmp.ps1
P.S.
Reduce googling)

It is very simple to do, open your PowerShell and write the following command if you have number of ps1 files. here you have to change the path with your path.
PS C:\Users> Get-ChildItem -Path "D:\downlod" -Recurse | Unblock-File

Try set-executionpolicy "Policyname" -force switch and the warnings pop-up should not come.

For those who want to access a file from an already loaded PowerShell session, either use Unblock-File to mark the file as safe (though you already need to have set a relaxed execution policy like Unrestricted for this to work), or change the execution policy just for the current PowerShell session:
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

Related

How to "sudo" with Powershell (elevate Powershell, running a command as admin) on Linux?

If you want to run a command as admin on PowerShell on Windows, you can write something like this:
Start-Process foo -Verb RunAs -ArgumentList '...'
But it is not possible on Linux. Powershell will return you the following error: Start-Process: The parameter '-Verb' is not supported for the cmdlet 'Start-Process' on this edition of PowerShell. Indeed, According to the Powershell online documentation, "the [-Verb] parameter does not apply for non-Windows systems", so it is not possible to run Powershell as admin like this on Linux.
My question is simple: how to elevate Powershell privileges on Linux? How can I simulate BASH's sudo ... with Powershell on Linux? I know that you can make a workaround with bash and some thing like sudo pwsh -Command '...', but I am looking for a "100% PowerShell" way to do this.
Thanks in advance for your responses.
PS: By "PowerShell", I mean "PowerShell 7", the latest one.
There is no powershell-ish way of doing this, because elevation feature depends on OS. In both Windows and Linux you actually start a new process under a different security context (and under separate user session), but in Windows there is built-in elevation mechanism using verbs. Linux does not support verbs, and you have to use sudo or su.
If you go little bit deeper, Start-Process -Verb under Windows creates appropriate [System.Diagnostics.ProcessStartInfo] with .Verb filled and runs process using [System.Diagnostics.Process]::Start($ProcessStartInfo).
So, under Linux you internally have the same except instead of setting .Verb, you set FileName to sudo and move old value to arguments.
Like there is no any universal method to drive both bicycle and spaceship.
Use sudo pwsh -c:
PS> (sudo pwsh { gci -r /opt/tomcat/*.sh | ? name -like 'startup*' }).fullname
/opt/tomcat/bin/startup.sh

Powershell: why is it so complicated to run a script?

I want to implement some alias-like command that could allow me to navigate through a long directory path by typing less.
In this answer I learned that it's not possible to create aliases for commands like "cd c:\some\dir", but it's possible to create a function, save it into a script and then run it.
So I created a script, saved it in directory C:\ps_scripts, made sure this directory is listed in PATH, then opened PS from C:\ps_scripts and tried to issue command .\script.ps1 and nothing happened - no error, no output, no navigation to the desired directory path. script.ps1 contains the following code:
function fp { set-location "C:\Users\user\puppet\modules\fp\files\configs" }
Then I searched for solution here and tried to run the script by appending powershell -noexit "& as advised in the accepted answer, but got error term '$' is not recognized.
And my execution policy is set to RemoteSigned.
What could be the issue?
You should use appropriate PowerShell profiles for such things, like functions, commands and settings.
You can get the profile path for applicable scope - User (current or all) and host scope (current host or all hosts).
E.g. To get the profile path for current user for all hosts (Windows PowerShell console, ISE), you can type this in PowerShell -
Write-Output $PROFILE.CurrentUserAllHosts
which will tell you the path of profile file that will be used for the scope.
Create a file with same path and name as per output of the command and put your function in that file. This will be automatically loaded for any PowerShell session by current user, and you can use that function without running the script prior manually. This behaves similar to .bashrc file in Linux.
Commands to setup your function in the profile
Run these exact commands in your PowerShell and then restart the PowerShell, it will start working then, and you can use fp after this to call that function from that user profile.
New-Item -Path $PROFILE.CurrentUserAllHosts -Force
Add-Content -Path $PROFILE.CurrentUserAllHosts -Value 'function fp { set-location "C:\Users\user\puppet\modules\fp\files\configs" }'
exit
You put a function definition into the script. You must now call/execute this function somewhere. Your function fp does the same thing as cd C:\Users\user\puppet\modules\fp\files\configs when it is called.
If you want your function to be available at the command prompt, you have to prefix its name with global:, as described here: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_functions?view=powershell-7#function-scope

The PowerShell window is always cleaned after a script has been executed

I run a PowerShell script form the PowerShell and whatever method I choose it always cleans my current PowerShell window which is pretty annoying. That doesn't happen with Linux PowerShell.
This is how I run a PowerShell script:
powershell.exe -File "/path/to/file.ps1
or
powershell -c ". /path/to/file.ps1"
No matter how I call the script, the current PowerShell window is cleaned and the only thing I can see is the output of the script that I called. All previously displayed text is deleted.
Maybe there are options to prevent this behavior?
See also my screenshots as a visual representation of the problem:
My PowerShell window before I ran the command -
My PowerShell window after I ran a command -
Adding -NoProfile solves the problem:
powershell -NoProfile -c 'Write-Host OK'

How to pass application path to task in Azure batch

I am using Azure batch. I have an exe which I need to execute on compute nodes.
I am using this path in the azure portal in my task window "cmd /c %AZ_BATCH_APP_PACKAGE_MyAppCreateRG%\CreateRG.exe -args HelloRG eastUs"
But I am getting an error: The system cannot find the path specified.
For your issue, the error you got is the core point of your problem. You can create a task with the command cmd /c "echo %AZ_BATCH_APP_PACKAGE_MyAppCreateRG%" to show the exact path if it exists or not.
Just like if you want to get the environment variable PATH in windows, you should use the command echo %PATH%. And the result will like below:
So, if it could not show the path you want, it means the environment variable doesn't exist and you should set it up first and check then.
I suggest if you want to execute an exe, you should check if the path of it is right and if the exe already exist for you.
It appears that you have mis-quoted your command to execute. Try:
cmd.exe /c "%AZ_BATCH_APP_PACKAGE_MyAppCreateRG%\CreateRG.exe -args HelloRG eastUs"
As an alternative i am able to execute powershell script instead of exe
powershell C:\Scripts\CreateRG.ps1 -resourceGroup "MyRG" -location "eastUs"

Script Change .sh for Windows Server 2008

I try to do a migration to Windows Server 2008 and i am new with script concept.
I have a .sh file to launch which contains :
cd RADIOROOT/PLAYERS
killall player_1
su - robotstream --command='/RADIOROOT/PLAYERS/player_1 -t /RADIOROOT/PLAYERS/player_1.conf' & > dev/null
First, how can i launch this .sh file on my IIS ? Do i have to change it in a powershell script ?
I tried to change it to a .ps1 file with some changes but not working. Anyone know how to do it ?
Then, do you have any website about changing this sort of script in order to make it work ?
Thanks for your help.
Assuming everything is the same on the Windows server as it was on your *nix server (same paths, robotstream takes the same arguments, etc) this should do the same thing in PowerShell:
cd C:\RADIOROOT\PLAYERS
Stop-Process player_1
$command = "robotstream --command='/RADIOROOT/PLAYERS/player_1 -t /RADIOROOT/PLAYERS/player_1.conf'"
Invoke-Expression $command
Save that with a .ps1 extension, and then run it from a PowerShell console.
As far as a website about converting SH scripts to PowerShell, you'll need to learn Bash (so you can understand the script you're converting), and PowerShell (so you know how to write the new script). There are plenty of good websites out there for both. Just Google "powershell tutorial".
Hope that helps. Feel free to ask for clarification.

Resources