Powershell script run as administrator from .exe compiled file - powershell

I am building a simple Powershell script for AD management.
I need to run as Admin this script from the .exe file (portable between Domain Controllers and/or Enviroments). Any suggest to make the exe file to request the Admin Privileges to the end-user (PopUp shield "can this program modify...")?

Consider using ps2exe - https://github.com/MScholtes/PS2EXE
This can create an exe from a ps1 file and add things like required admin privilege's etc.
ps2exe .\source.ps1 .\target.exe -requireAdmin

Related

Looking for cmdlets that shows the location of the personal and system profile ps1 scripts

When I start PowerShell if this message:
Loading personal and system profiles took xxx ms
Where are the personal and system profile ps1 scripts?
Is there any command that shows which exact startup scripts are being loaded upon every start?

Run a script via FTP connection from PowerShell

I have made a script that does a really basic task, it connects to a remote FTP site, retrieves XML files and deletes them afterward.
The only problem is that in the past we lost files because they were added when the delete statement was run.
open ftp.site.com
username
password
cd Out
lcd "E:\FTP\Site"
mget *.XML
mdel *.XML
bye
To prevent this from happening, we want to put a script on the FTP server (rename-files.ps1). The script will rename the *.xml files to *.xml.copy.
The only thing is I have no clue how to run the script through my FTP connection.
Some, but very few, FTP servers support SITE EXEC command. In the very rare case that your FTP server does support it, you can use:
quote SITE EXEC powershell rename-files.ps1
Though, in most cases, you cannot execute anything on the FTP server, if FTP is the only way you can access the server. You would have to use another method to execute the script, like SSH, PowerShell Remoting, etc.
But your problem has other solutions:
rename files using FTP; or
delete only the files that were downloaded.
Both are doable, but you will need better FTP client than Windows ftp.exe.
See for example A WinSCP script to download, rename, and move files.
Or you can do it like:
Run ftp.exe once to retrieve list of files;
Process the list in PowerShell to generate an ftp script with get and del commands for specific files (without wildcard);
Run ftp.exe again with generated script.
Actually you do not need to use ftp.exe in PowerShell. .NET assembly has its own FTP implementation: FtpWebRequest.

None of my scripts run using PowerGUI, but they work directly from PowerShell

I just installed the latest version of PowerGUI (3.6.0.21).
When I try to run any script from inside PowerGUI I get the error message 'The file C:\Sandbox\MyPowerShell.ps1 cannot be loaded. The file C:\Sandbox\MyPowerShell.ps1 is not digitally signed.
I have googled all over for something simple to get around this, but no luck. What do I need to do to get this going?
Also, if I run the script commands from PowerShell directly they work.
AllSigned will not let you run an unsigned script. If you can't change the execution policy then import it as a module.
Save the script as a .psm1 and use Import-Module. It will bypass scripting policy because it's being loaded/run as a module.

How to automatically open cmd as admin

I'm creating a program for my school project, and it is making calls to some batch files.
Some of the commands will not execute except with admin privileges. How do I resolve this please? Any solution in VBS, VB or batch code will be appreciated, as I'm coding in Visual Basic.
Use this batch code:
runas /user:ADMINUSERNAME BATCHFILE.BAT
ADMINUSERNAME is the administrator account for your computer, and BATCHFILE.BAT is the path to your batch file.

Can you load a ps1 file from within a ps1 file?

can I load a ps1 file from within a ps1 file.
The end goal is to make a ps1 file that I put in my profile on all my computers and have a floating profile that I can put paths to pocket utilities in.
I'll probably put this in a code repo, or some outside sharing mechanism.
This is most definitely supported in powershell and I have this exact same setup on my machine
Normal Profile.ps1 Contents
. ~\winconfig\PowerShell\Profile.ps1
The Profile.ps1 in WinConfig\PowerShell is my version controlled profile which has all of my custom fun inside of it. I have a script which simply generates the standard Profile.ps1 in the normal powershell directory whenever I get a new machine.
I have all my scripts primarily on a flash drive (and some backups on computers at home/work). At the flash drive I have my profile script that creates all my custom conversion functions, drives etc.
What I want is to load the profile script from flash disk every time I run PowerShell.
So, the code in my $profile at all the computers I work with looks like this:
#drive name of my flash; obviously different on each computer
$global:psflash = "g:\"
# if my flash disk is available, load my profile script from the flash disk
if (test-path $psflash) {
. (join-path $psflash 'dev\powershell\PsProfile.ps1')
}
The good side effect is that all my scripts can use the global variable $psflash to import other scripts they depend on or other modules in the same way as done in my profile (using join-path) any time later.