Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating and saving function in powershell for global use

I would like to create a global (if it is the right term) function easy to type which will call the server to start. The command is like this:

aws ec2 start-instances --instance-ids i-xxxxxxxxxxxxx

This is my result:

function aws_start {aws ec2 stop-instances --instance-ids i-xxxxxxxxxxxxx}

I save it in C:\Program Files\WindowsPowerShell\Modules\AWS_start as AWS_start.ps1 but nothing happened and I cannot call it.

Btw I was trying the alias provided by https://docs.aws.amazon.com/cli/latest/userguide/aws-cli.pdf (page 147) but the output is:

Unable to parse config file: C:\Users\user\.aws\cli\alias

I am more curious about how to solve it from point of view of the powershell! It could be helpful for other purposes!

Thank you in advance!

Update 1:

Issue for AWS module is still same:

Unable to parse config file: C:\Users\user\.aws\cli\alias

My alias looks like this:

#AliasName   #CommandToReplace
startstuff = ec2 start-instances --instance-ids i-xxxxxxxxxxxx
stopstartstuff  = ec2 stop-instances --instance-ids i-xxxxxxxxxxx  
like image 446
Lukáš Tůma Avatar asked May 28 '26 09:05

Lukáš Tůma


1 Answers

If you would like to just have a function that is available for yourself, you can add it to your profile by copying and pasting your function in to the file that is returned by typing $PROFILE in to PowerShell. You can run notepad $PROFILE on Windows, open $PROFILE on MacOS or use your favourite editor on Linux. Your profile is loaded every time you open PowerShell

If you want the function to be available for more people, then you could go down the route of making it a module, but there is an Amazon PowerShell module already. It is easier if you just have a few functions, to import them in to your session by dot-sourcing them.

. .\path\to\myFile.ps1

After this, I'm not quite clear on what the issue you're having is. As you seem to be working on Windows, I assume you have followed these steps from the linked document:

md %USERPROFILE%\.aws\cli
echo [toplevel] > %USERPROFILE%/.aws/cli/alias

Then you set the options you would like to use with an alias name in the file. Something like:

#AliasName   #CommandToReplace
startstuff = ec2 start-instances --instance-ids i-xxxxxxxxxxxxx

Which I assume you should be able to call, as long as the file is formatted correctly with.

aws startstuff

You are getting the parsing error above because I assume the alias file is not configured properly. If you would like help with that, please post the contents of the file created in your user profile.

If you would like to create a similar effect with PowerShell using the cli tool. You could create a couple of helper functions in your profile or a .ps1 file saved for multiple users as discussed above. Although, as I said, there is an existing AWS Module for PowerShell.

You might want a function that can invoke EC2 commands for you. Here is an untested sample as I do not have aws cli installed.

function Invoke-Ec2Cli {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [System.String]
        $Command,

        [Parameter(Mandatory = $true)]
        [System.String]
        $Arguments
    )
    Process {
        # Create an array for the arguments
        $AWSArgs = @()

        # Add command and arguments
        $AWSArgs = $AWSArgs + "ec2"
        $AWSArgs = $AWSArgs + $Command
        $AWSArgs = $AWSArgs + $Arguments -split "\s+"

        # Run the command
        & aws $AWSArgs
    }
}

Which will act as a base for other things that you want to do with EC2. Then you might want a function that calls the base and allows you to just specify to start some instances.

function Start-Ec2Instance {
    [CmdletBinding()]
    Param (
        [Parameter(Mandatory = $true)]
        [System.String]
        $InstanceIDs
    )
    Process {
        # Invoke Ec2 Cli
        Invoke-Ec2Cli -Command start-instances -Arguments "--instance-ids $InstanceIDs"
    }
}

You should then be able to start your instances like this.

Start-Ec2Instances -InstanceIDs "i-xxxxxxxxxxxxx"
like image 90
Ash Avatar answered May 31 '26 06:05

Ash



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!