Jump to content

Simple powershell script


jkemp
 Share

Recommended Posts

Looking to run a simple powershell script.  Wanting to use variables so that I can simply change cmdlets as I need or add them in.  There are 3 different commands I need to run, each running after the previous command completes.

Here is what I have now, though when it runs the cmdlet does not work.  It does work when run from powershell when I open it from the run command or start menu.  The other issue I have is that the other two cmdlets will basically run right on top of each other. :

#Region ;**** Directives created by AutoIt3Wrapper_GUI ****
#AutoIt3Wrapper_Res_requestedExecutionLevel=requireAdministrator
#EndRegion ;**** Directives created by AutoIt3Wrapper_GUI ****
#include <AutoItConstants.au3>
$pws1 = "Powershell.exe"
$Cmd1 = 'Get-AppXPackage -AllUsers | Foreach {{}Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"{}}' & '{Enter}'
$Cmd2 = '"Set-Service UserManager -Startuptype "Automatic"'
$Cmd3 = "Stop-Service tiledatamodelsvc"

    ;Run Powershell as a local admin

    Local $pws = RunAs("username", @ComputerName, "password", $RUN_LOGON_NOPROFILE, $pws1)

    WinWait("Windows ", "", 10)
     WinActive ("Windows ")
        Send ($Cmd1)
        sleep (10000)
        Send ("exit" & "{Enter}")
        While WinExists ("Windows " = 1)
        sleep (100)
    WEnd

Link to comment
Share on other sites

You can just run a ps1 file manually or add them to a single command for example:

#RequireAdmin

Local $sCommand = 'PowerShell.exe '
;~  Reinstall and Re-Register All Windows Apps for All Accounts
;~  $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}'
;~  Using the following for testing purposes only
    $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Write-Host "$($_.InstallLocation)\AppXManifest.xml"}'
    $sCommand &= ' ; '
    $sCommand &= 'Set-Service UserManager -Startuptype "Automatic" '
    $sCommand &= ' ; '
    $sCommand &= 'Stop-Service tiledatamodelsvc'
;~  Last two lines are just so you can see the output
    $sCommand &= ' ; '
    $sCommand &= 'Read-Host -Prompt "Press Enter to exit"'
    Run($sCommand)

 

Link to comment
Share on other sites

On 2/1/2018 at 6:18 PM, Subz said:

You can just run a ps1 file manually or add them to a single command for example:

#RequireAdmin

Local $sCommand = 'PowerShell.exe '
;~  Reinstall and Re-Register All Windows Apps for All Accounts
;~  $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}'
;~  Using the following for testing purposes only
    $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Write-Host "$($_.InstallLocation)\AppXManifest.xml"}'
    $sCommand &= ' ; '
    $sCommand &= 'Set-Service UserManager -Startuptype "Automatic" '
    $sCommand &= ' ; '
    $sCommand &= 'Stop-Service tiledatamodelsvc'
;~  Last two lines are just so you can see the output
    $sCommand &= ' ; '
    $sCommand &= 'Read-Host -Prompt "Press Enter to exit"'
    Run($sCommand)

 

Any tips on getting this to run in an elevated state?  I get it to run as is but with the cmdlet runs I get access denied.

Link to comment
Share on other sites

what is the exact error? please post that.

If its coming from powershell i imagine '-executionpolicy bypass' will fix the command.  If it is an OS error there is a different issue.

 

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

1 hour ago, Subz said:

Did you include the #RequireAdmin at the top of the script?  This is all I required to run the script

There are enterprise scenarios in which a local elevated token would not suffice for changing a service state and would require that you runas a DA account, but until we know what error it is (and who is throwing it) questions will abound.

Edited by iamtheky
just poor sentences all around

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

On 2/6/2018 at 10:10 AM, iamtheky said:

what is the exact error? please post that.

If its coming from powershell i imagine '-executionpolicy bypass' will fix the command.  If it is an OS error there is a different issue.

 

Sorry for the delayed responses, this is something I am working on, between my regular workload, to prepare for Win10 deployment.  I am learning a new language, not to mention first programming, here so its taking me a little. Shortly after my last post I discovered that I had left out the #requireadmin line.  So right now I am working on getting the shell window to stay open long enough for me to determine if everything is working properly.  At the moment the shell window opens stuff happens and it closes. Thank you for your assistance.

Link to comment
Share on other sites

On 2/1/2018 at 6:18 PM, Subz said:

You can just run a ps1 file manually or add them to a single command for example:

#RequireAdmin

Local $sCommand = 'PowerShell.exe '
;~  Reinstall and Re-Register All Windows Apps for All Accounts
;~  $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}'
;~  Using the following for testing purposes only
    $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Write-Host "$($_.InstallLocation)\AppXManifest.xml"}'
    $sCommand &= ' ; '
    $sCommand &= 'Set-Service UserManager -Startuptype "Automatic" '
    $sCommand &= ' ; '
    $sCommand &= 'Stop-Service tiledatamodelsvc'
;~  Last two lines are just so you can see the output
    $sCommand &= ' ; '
    $sCommand &= 'Read-Host -Prompt "Press Enter to exit"'
    Run($sCommand)

 

Right now as far as I can tell the last 2 lines are not working, as it close right away and I am not given the option to press enter.

Link to comment
Share on other sites

https://www.autoitscript.com/autoit3/docs/libfunctions/_ScreenCapture_Capture.htm

https://www.autoitscript.com/autoit3/docs/functions/Run.htm

add some screencapture directly after your run($sCommand) hopefully you can catch the error then

Run($sCommand)
for $i=1 to 10
    sleep(50)
    _ScreenCapture_Capture("check" & $i &  ".jpg")
next

 

Link to comment
Share on other sites

its a quotes issue for that error.  everything looks to fire up until the last command where it appears to run without having the -Prompt argument in quotes.  So that error is telling you that while it could run "Read-Host -Prompt Press" you cannot have "Enter" as an argument to press, which is what it would be if it all that shit was unquoted.

experiment 1)

leave all quotes alone and remove just the words in that sentence leaving the first word, Press.

If that works then check to see if the powershell commands worked as desired....

experiment 2, kind of testing the same thing)

make a variable that has a double quoted "Press Enter To Exit" wrapped in single quotes 

$var = '"Press Enter To Exit"'

And then do

'Read-Host -Prompt ' & $var

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Link to comment
Share on other sites

The semi-colon (;) is like the ampersand (&) in cmd.exe it just joins commands together into one line.  With regards to the last line, it's an issue with the double quotes as iamthekey pointed out.  The code below should now work:

nb: You will need to uncomment the line below ";~  Reinstall and Re-Register All Windows Apps for All Accounts" to reinstall/re-register all of the Windows Apps, the code below will only display all Windows Apps "Installlocation" property, change the UserManager Startuptype to Automatic and stop the tiledatamodelsvc service.

#RequireAdmin

Local $sCommand = 'PowerShell.exe '
;~  Reinstall and Re-Register All Windows Apps for All Accounts
;~  $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Add-AppxPackage -DisableDevelopmentMode -Register "$($_.InstallLocation)\AppXManifest.xml"}'
;~  Using the following for testing purposes only
    $sCommand &= 'Get-AppXPackage -AllUsers | Foreach {Write-Host "$($_.InstallLocation)\AppXManifest.xml"}'
    $sCommand &= ' ; '
    $sCommand &= 'Set-Service UserManager -Startuptype "Automatic" '
    $sCommand &= ' ; '
    $sCommand &= 'Stop-Service tiledatamodelsvc'
;~  Last two lines are just so you can see the output
    $sCommand &= ' ; '
    $sCommand &= "Read-Host -Prompt 'Press Enter to exit.'"
    Run($sCommand)

 

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...