Wednesday, September 16, 2020

#100: How to execute a string in Powershell?

 Rarely, but sometimes we need to run the command from an expression that we create. I stumbled upon a situation where I was to read the content of a config file and run it. 

There could be other ways to do it, but invoke-expression can be used with simplicity. 


Invoke-Command

SYNTAX

    Invoke-Command [-ScriptBlock] <scriptblock> [-NoNewScope] [-InputObject <psobject>] [-ArgumentList <Object[]>]

    [<CommonParameters>]


EXAMPLE: 

Read content of a file that has EXPRESSION=get-help ls 

$str=gc "c:\temp\cmd_list.txt" | where { $_ -match "EXPRESSION=" } | foreach { $_.split("=")[1] 

invoke-command $str

      

      

I hope it was quite easy and simple. 

Happy scripting !!!

Saturday, June 13, 2020

#98: How to know WIFI password?


PROBLEM:

It is quite obvious to forget password and keep trying password again and again. It happens with WIFI also and gives a real hard time. 


SOLUTION:

To get WIFI password, you can try below command, quick and simple. 

(netsh wlan show profiles) | Select-String "\:(.+)$" | %{$name=$_.Matches.Groups[1].Value.Trim()$_} | %{(netsh wlan show profile name="$name" key=clear)}  | Select-String "Key Content\W+\:(.+)$" | %{$pass=$_.Matches.Groups[1].Value.Trim()$_} | %{[PSCustomObject]@{ ProfileName=$name;Password=$pass }}


NOTE:
With this method, you get your WIFI profiles password, not your neighbour's ðŸ˜„.

Happy scripting!!!

#97: How to Install Azure Powershell ?

PROBLEM: 

Azure portal provides a nice interactive interface to get details about configuration or drop/create resources. Sometimes, we need to connect Azure with Powershell to automate certain tasks. It can be useful if you are making your own inventory of Azure VMs, Disks allocated or performing a check whether certain resources has been created. Once resources dropped can never be recovered, to avoid human mistakes, it is always advisable to make your own repository to have a capability to recreate resources. 

Azure CLI provides different options to get the inventory details and it also provides several capabilities that Azure Powershell might not have. In this article, I will over the details about Azure Powershell AZ modules. 

How to connect to Azure Powershell from you desktop?

SOLUTION:

Assuming you have an Azure account (Free or Paid or Pay-As-You-Go).

1. Install Azure Powershell

To install, Azure Powershell make sure your machine is able to connect to Azure. If it is connected, there could still be a situation where it will not connect to Azure due to firewall blocking or server not allowing to download or perform certain tasks. 

1) You can connect to http://portal.azure.com first.     

  • If it does not connect, try to add in Internet Explorer trusted servers list that must have little low checks. 
  • Gradually add links that it denies.
  • If you are not sure what link is blocked. You can press F11 and try to see the blocked URL in console. 

2)  IE, Edge and Chrome allows connecting. But I prefer Chrome due to obvious reasons. 

3) Once connection is fine, check the modules installed on machine using below command:

get-module -ListAvailable | where { $_ -match "Az." }

-OR-
You can check very easily in Powershell ISE.



[4] If not found, you can run command like below: 

Install-Module -Name Az -AllowClobber -Scope AllUsers

2. Connect Azure 

Run the command like below: 
Connect-AzAccount
A pop-up will come. Enter your credentials. 


3. Try few commands to confirm.

Get-AzResourceGroup
Get-AzRM 
Get-AzStorageAccount 


That's all! You can find so many Powershell scripts over internet. Try them. 
From installing VM to shutting down, everything that you can do with GUI can be done with Azure Powershell. 

I will provide many script that I wrote for my purposes.\

Thanks !!!


#112: How to handle xml document in Powershell?

 In PowerShell, you can handle XML data using various cmdlets and methods provided by the .NET Framework. Here's a basic guide on how to...