Jump to content

Powershell Execution Policy


 Share

Recommended Posts

I have a couple powershell scripts I have to run on multiple computers every few weeks so I am trying to make a script that I can run with AutoIT to...

 

First check the Powershell Execution Policy and set to Unrestricted if needed. I know the command to get the policy is Get-ExecutionPolicy but not how to get the info that comes back. I know how to set the policy with Set-Execution Policy Unrestricted but not how to pass a "Y" for YES and then a ENTER press.

Second I need to Import the ps1 or psm's that I need to run. Not sure yet still hung up on 1.

Next execute the scripts.

Finally Set the execution policy back to the default.

 

#RequireAdmin


$sCMD = "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe Set-ExecutionPolicy Unrestricted"
$pid = Run($sCMD, @SystemDir, @SW_SHOW)

I tried the above but I am hung up at how to pass the "Y" and "ENTER" keys. I tried making a variable for both Y and Enter then using Send("$VAR1 & &VAR2") but that did nothing.

 

Any advice about how to do this and if the rest of my plan is even possible would be much appreciated. Also how I can get the information that comes back from the Get-Execution Policy would be helpful.

Edited by Nunos
Link to comment
Share on other sites

And you don't want to just bypass it?

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

Link to comment
Share on other sites

Bypass the Execution Policy?

 

I guess I didn't know that was possible I thought it was made so you couldn't run powershell modules and scripts on a machine that they weren't written on so I was looking to set it to Unrestricted do my work and then set it back so that end users can't start running scripts as easily.

Honestly I am very new to Powershell and I know only a little about AutoIT but I am trying to learn.

Link to comment
Share on other sites

look at post #5 here:

 

Edited by iamtheky

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

Link to comment
Share on other sites

That error "cannot find a positional parameter that accepts argument 'snippets'"  is due to spaces in your path (namely the directory "Little Snippets to test").  Put it quotes or just take the spaces out of the path.

Edited by iamtheky

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

Link to comment
Share on other sites

well i feel smarter than ever, we will call it a draw.  And you are welcome, if you get your script working and some of these .ps1 scripts do some cool stuff please post them to the thread I linked above, especially if they are some kick ass offensive powershell scripts.

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

Link to comment
Share on other sites

Do you know if you can pass more than one argument in through to a powershell module?

#include <AutoItConstants.au3>
#RequireAdmin

$sFile = "C:\WINDOWS\system32\notepad.exe"
$sPinToTaskbar = "PinToTaskbar"

$sCommands = 'powershell -ExecutionPolicy ByPass import-module ' & @ScriptDir & '\PinnedApplications.psm1 ; Set-PinnedApplication -Action ' & $sPinToTaskbar & ' -FilePath ' & $sFile
$iPID = Run("cmd /k " & $sCommands, "", @SW_SHOW , $stdout_child)

$sOutput = ""

 While 1
        $sOutput &= StdoutRead($iPID)
        If @error Then
            ExitLoop
        EndIf
    WEnd


msgbox(0, '' , $sOutput)

 

###########################################################################"
 # 
 #
 # NAME: PinnedApplications.psm1
 # 
 # AUTHOR: Jan Egil Ring, Crayon
 #
 # DATE  : 06.08.2010 
 # 
 # COMMENT: Module with the ability to pin and unpin programs from the taskbar and the Start-menu in Windows 7 and Windows Server 2008 R2.
 #
 # This module are based on the Add-PinnedApplication script created by Ragnar Harper and Kristian Svantorp:
 # http://blogs.technet.com/kristian/archive/2009/04/24/nytt-script-pin-to-taskbar.aspx
 # http://blog.crayon.no/blogs/ragnar/archive/2009/04/17/pin-applications-to-windows-7-taskbar.aspx
 #
 # Johan Akerstrom`s blog: http://cosmoskey.blogspot.com
 #
 # For more information, see the following blog post:
 # http://blog.crayon.no/blogs/janegil/archive/2010/02/26/pin-and-unpin-applications-from-the-taskbar-and-start-menu-using-windows-powershell.aspx
 #
 # VERSION HISTORY:
 # 1.0 17.04.2009 - Initial release by Ragnar Harper and Kristian Svantorp
 # 1.1 26.02.2010 - Update by Jan Egil Ring. Added the capability to unpin applications.
 # 1.2 06.08.2010 - Update by Johan Akerstrom. Added full MUI support.
 # 
 ###########################################################################"


function Set-PinnedApplication
{
<# 
.SYNOPSIS 
This function are used to pin and unpin programs from the taskbar and Start-menu in Windows 7 and Windows Server 2008 R2
.DESCRIPTION 
The function have to parameteres which are mandatory:
Action: PinToTaskbar, PinToStartMenu, UnPinFromTaskbar, UnPinFromStartMenu
FilePath: The path to the program to perform the action on
.EXAMPLE
Set-PinnedApplication -Action PinToTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe"
.EXAMPLE
Set-PinnedApplication -Action UnPinFromTaskbar -FilePath "C:\WINDOWS\system32\notepad.exe"
.EXAMPLE
Set-PinnedApplication -Action PinToStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe"
.EXAMPLE
Set-PinnedApplication -Action UnPinFromStartMenu -FilePath "C:\WINDOWS\system32\notepad.exe"
#> 
    [CmdletBinding()]
    param(
      [Parameter(Mandatory=$true)][string]$Action, 
      [Parameter(Mandatory=$true)][string]$FilePath
    )
    if(-not (test-path $FilePath)) { 
        throw "FilePath does not exist."  
    }
   
    function InvokeVerb {
        param([string]$FilePath,$verb)
        $verb = $verb.Replace("&","")
        $path= split-path $FilePath
        $shell=new-object -com "Shell.Application" 
        $folder=$shell.Namespace($path)   
        $item = $folder.Parsename((split-path $FilePath -leaf))
        $itemVerb = $item.Verbs() | ? {$_.Name.Replace("&","") -eq $verb}
        if($itemVerb -eq $null){
            throw "Verb $verb not found."           
        } else {
            $itemVerb.DoIt()
        }
        
    }
    function GetVerb {
        param([int]$verbId)
        try {
            $t = [type]"CosmosKey.Util.MuiHelper"
        } catch {
            $def = [Text.StringBuilder]""
            [void]$def.AppendLine('[DllImport("user32.dll")]')
            [void]$def.AppendLine('public static extern int LoadString(IntPtr h,uint id, System.Text.StringBuilder sb,int maxBuffer);')
            [void]$def.AppendLine('[DllImport("kernel32.dll")]')
            [void]$def.AppendLine('public static extern IntPtr LoadLibrary(string s);')
            add-type -MemberDefinition $def.ToString() -name MuiHelper -namespace CosmosKey.Util            
        }
        if($global:CosmosKey_Utils_MuiHelper_Shell32 -eq $null){        
            $global:CosmosKey_Utils_MuiHelper_Shell32 = [CosmosKey.Util.MuiHelper]::LoadLibrary("shell32.dll")
        }
        $maxVerbLength=255
        $verbBuilder = new-object Text.StringBuilder "",$maxVerbLength
        [void][CosmosKey.Util.MuiHelper]::LoadString($CosmosKey_Utils_MuiHelper_Shell32,$verbId,$verbBuilder,$maxVerbLength)
        return $verbBuilder.ToString()
    }

    $verbs = @{ 
        "PintoStartMenu"=5381
        "UnpinfromStartMenu"=5382
        "PintoTaskbar"=5386
        "UnpinfromTaskbar"=5387
    }
    
    if($verbs.$Action -eq $null){
        Throw "Action $action not supported`nSupported actions are:`n`tPintoStartMenu`n`tUnpinfromStartMenu`n`tPintoTaskbar`n`tUnpinfromTaskbar"
    }
    InvokeVerb -FilePath $FilePath -Verb $(GetVerb -VerbId $verbs.$action)
}

Export-ModuleMember Set-PinnedApplication

Not sure where it is hanging up but I need to pass in the where to pin it ie... taskbar and or start menu then the path to the application to pin. I might still have some issues with my quotes because I still don't understand when and where I need to use them.

Link to comment
Share on other sites

that should be fine if you think it is spaces in ScriptDir doing you in you could always

$ScriptDirectory = '"' & @ScriptDir & '"'

then use $ScriptDirectory

and I would definitely msgbox the contents of run prior to executing it, that way you can show us exactly what is being run if it still fails.

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

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...