Learn how to automate your vSphere infrastructure.

Category: No scripts

Create your own Aliases

Aliases in PowerCLI are shortcuts or alternative names for PowerShell cmdlets and functions. They provide a convenient way to execute common commands with shorter and more intuitive names. Aliases make PowerCLI scripting faster and more user-friendly by reducing the need for typing lengthy cmdlet names. While they enhance productivity, it’s important to use them judiciously and consider readability when sharing scripts with others, as excessive use of aliases can make code less clear and maintainable.

Aliases that I use on my daily work are:

  • Create an alias called count for the Measure-Object cmdlet. Instead of accessing the .count property, you can pipe your command to the count alias

Creating your PS profile

PowerShell profiles might sound like a tech insider’s secret, but they’re a valuable tool for anyone who uses PowerShell regularly. In this brief blog, we’ll uncover why PowerShell profiles are important and what you can achieve with them.

What are PowerShell Profiles?

A PowerShell profile is a script that runs when you start a PowerShell session. Think of it as your personalized setup for PowerShell. It allows you to define custom configurations, functions, and aliases that streamline your PowerShell experience.

Why are They Important?

  1. Customization: With a PowerShell profile, you can tailor your environment to your needs. Create custom aliases for frequently used commands, load specific modules automatically, or define your prompt to display essential information.
  2. Automation: PowerShell profiles enable you to automate tasks you frequently perform. You can load modules, connect to servers, or set environment variables automatically upon starting a session, saving time and effort.
  3. Consistency: Profiles ensure a consistent experience across sessions and machines. Your customizations and configurations follow you wherever you use PowerShell.

In essence, PowerShell profiles empower you to make PowerShell your own. They transform it from a powerful command-line tool into a personalized, efficient, and consistent environment. Don’t miss out on the advantages – start crafting your PowerShell profile today to unlock your full PowerShell potential!

Some important things that you can include into profiles are:

  • VIProperties – which will help you to read information which deep into the API objects
  • Aliases – create aliases that help you to be more productive.
  • Custom functions that belong only to you.

PowerShell supports several profile files that are scoped to users and PowerShell hosts. You can have any or all these profiles on your computer.

For example, the PowerShell console supports the following basic profile files. The profiles are listed in order that they’re executed. More info.

  • All Users, All Hosts – $PSHOME\Profile.ps1
  • All Users, Current Host – $PSHOME\Microsoft.PowerShell_profile.ps1
  • Current User, All Hosts – $HOME\Documents\WindowsPowerShell\Profile.ps1
  • Current user, Current Host – $HOME\Documents\WindowsPowerShell\Microsoft.PowerShell_profile.ps1

Whatever you put into this file “$HOME\Documents\WindowsPowerShell\Profile.ps1” it will be loaded for all hosts (PS and ISE) for the current user.


PowerCli configuration

Configuring PowerCLI settings using the Set-PowerCLIConfiguration cmdlet is a straightforward process that can greatly enhance your PowerCLI experience. It has different options which can be read by reading the help of it. However, two most important in my point of view are DefaultVIServerMode and Scope.

  • DefaultVIServerMode: Specifies the server connection mode. The new configuration takes effect immediately after you run the cmdlet. The following values are valid: Single, Multiple.
    • Single – If no target servers are specified, cmdlets run only on the last connected server.
    • Multiple – If no target servers are specified, cmdlets run on the servers in the variable.
  • Scope: Specifies the scope of the setting that you want to modify. The parameter accepts Session, User and All Users values.
    • Session – the setting is valid for the current VMware PowerCLI session only and overrides any User and All Users settings.
    • User – the setting is valid for the current Windows user only, overrides All Users settings, and is applied only if a Session setting cannot be detected.
    • All Users – the setting is valid for all users and is applied only if Session and User settings cannot be detected.

With the DefaultVIServerMode one should be careful as you may be connected to multiple vCenters on different environments (Test, or Prod) and think that you’re connected only to one server and run something which could affect Production as well. If someone know what he/she is doing or if he/she writes scripts which point exactly to a vCenter by utilizing -Server parameter of each cmdlet, then Multiple value would make more sense as it is easier to work with multiple environments.

This information can be read using Get-PowerCLIConfiguration cmdlet.


Deploy Gitea on your server

Gitea is an open-source, web-based platform designed for hosting and managing Git repositories. It serves as a lightweight and self-hosted alternative to popular Git repository hosting services like GitHub and GitLab.

Deploying it is an easy process. However, you have to maintain it by installing updates and backing it up.

  1. Download the latest windows version from here. Gitea Download Page For example: gitea-1.20.4-windows-4.0-386.exe
  2. Create a folder under Program Files and place the already downloaded file in that folder, and rename the file to gitea.exe so when you change versions you don’t need to re-create the service again.
  3. Create a Windows service and start it, so that Gitea will start together with windows.
New-Service -Name "Gitea" -BinaryPathName "C:\Program Files\gitea\gitea.exe" -StartupType "Automatic" -Description "Gitea"

Gitea then can be accessed via your browser from the network. http://fqdn:3000 or http://ip:3000


Installing PowerCli

PowerCLI, developed by VMware, is a command-line interface (CLI) tool that allows administrators and developers to manage and automate various tasks in VMware environments. It provides a powerful set of cmdlets for managing virtual infrastructure, making it an essential tool for anyone working with VMware technologies.

In this article, we will explore one method of installing PowerCLI to help you get started with managing your VMware infrastructure efficiently.

Installing PowerCLI via PowerShell Gallery

One of the most straightforward ways to install PowerCLI is by utilizing PowerShell’s built-in package management features and the PowerShell Gallery. Here’s how you can do it:

  1. Open a PowerShell terminal with administrative privileges.
  2. Run the following command:
Install-Module -Name VMware.PowerCLI

After the installation is completed, it can be verified by looking at which modules are installed by executing the following command:

Get-Module -ListAvailable -Name VMware*

© 2024 PowerCli scripting

Theme by Anders NorenUp ↑