Powershell – Linux – Invoke Using Command Argument

Background

There are a couple of ways to invoke Powershell in Linux.

Options

Here are a couple of Options:-

  1. -File
  2. -Command

Payload – File

Syntax


pwsh -File [file]

Sample


pwsh -File ./helloWorld.ps1

Payload – Command

Outline

  1. To get username
    • Environment::UserName

Syntax


pwsh -Command "[command]"

Sample

Hello World


pwsh -Command "& Write-Output('Username:- '+[Environment]::UserName); "

Output

Output – Image

pwsh.01.helloWorld.01.20200523.0637PM

Output – Text


>pwsh -Command "& Write-Output('Username:- '+[Environment]::UserName); "
Username:- dadeniji

Explanation

  1. We printed out our username

Environment Variables and $PSVersionTable

Outline

  1. To get username
    • Environment
      • Environment::UserName
      • Environment::MachineName
    • $PSVersionTable
      • $PSVersionTable.Platform
      • $PSVersionTable.PSEdition

Sample


pwsh -Command "& Write-Output('Username:- '+[Environment]::UserName); Write-Output('Machine Name :- ' + [Environment]::MachineName ); Write-Output('OS Platform :- ' + \$PSVersionTable.Platform); Write-Output('Powershell Edition :- ' + \$PSVersionTable.PSEdition); "

Output

Output – Image

pwsh.01.environment.01.20200523.0649PM

Output-Text


Username:- dadeniji
Machine Name :- dblab
OS Platform :- Unix
Powershell Edition :- Core

 

Explanation

  1. We printed out our username
  2. Machine Name
    • [Environment]::MachineName
  3. OS Platform
    •  \$PSVersionTable.Platform
      • Notice that we escaped $, by using $, by using \
        • \$PSVersionTable.Platform
  4. PowerShell Edition
    • \$PSVersionTable.PSEdition
      • Escaped $PSVersion, by using \

 

Source Code Control

GitHub

Gist

DanielAdeniji/powershellInLinux.sh
Link

Summary

You might have to escape control characters to be able to access certain programming functionalities from the command line.

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s