Simplify deployment with Powershell Remoting

Sure, you can use Powershell Desired State Configuration, but let's say you don't want to. Use PSRemoting.


Create a domain using Azure Active Directory Domain Services. Create a user, put them in AAD DC Administrators group in Azure AD. Change their password.


Note: AADDS copies all users and groups from Azure Active Directory. In order to authenticate to the new domain, the password must be changed. This is a security feature. How would you feel if Azure was able to copy your password? Not good, eh?


Once that's complete, let's join a VM to the new domain. Create a VM, ensure it can route/connect to the same network the AADDS servers were provisioned in.


Open Azure Cloudshell and choose Powershell.


Save powershell as .ps1 file and upload to Cloudshell.


Execute and follow prompts.


Connect-AzAccount -UseDeviceAuthentication
$vmName = Read-host "VM Name?"
$rgName = Read-host "Resource Group Name?"
Write-Host "Enabling PSRemoting and disabling Cert checking..." -ForegroundColor Green
Install-Module pswsman -Force
Disable-WSManCertVerification -All
Enable-AZVMPSRemoting -Name $vmName -ResourceGroupName $rgName -Protocol https -OsType Windows
Write-Host "Adding VM to Domain and rebooting..." -ForegroundColor Green
Invoke-AzVMCommand -Name $vmName -ResourceGroupName $rgName -ScriptBlock {
    Add-Computer -DomainName "domain.com" -restart -force -confirm;
    } -Credential (Get-Credential)

Export Azure File Shares files to CSV

Just like it sucks to search directories and file names, it sucks to output files and their sizes.

Note: You'll need Subscription ID, Resource Group Name, Storage Account Name, and Azure Share Name

# Set mandatory parameters
param 
(
    [Parameter(Mandatory=$true)]
    [string] $subscriptionId,
    [Parameter(Mandatory=$true)]
    [string] $resourceGroupName,
    [Parameter(Mandatory=$true)]
    [string] $storageAccountName,
    [Parameter(Mandatory=$true)]
    [string] $storageShareName
)
# Repeat Loop start
Do {
Connect-AzAccount
# Set Azure context
$context = Set-AzContext -SubscriptionId $subscriptionId
# Get Storage Context
Write-Host "Searching for files in $storageShareName in $storageAccountName :" -ForegroundColor Green
Write-Host "=================================================================" -ForegroundColor Green
$key = Get-AzStorageAccountkey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$storContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $key.value[0]
$getAzSF = Get-AzStorageFile -ShareName $storageShareName -context $storContext
# Iterate through directories, find files and export Dir, Name and Length
	$(foreach ($dir in $getAzSF)
		{
			Get-AzStorageFile -ShareName $storageShareName -context $storContext -Path $($dir.name) | Get-AzStorageFile | Select-Object -Property @{L='Directory';E={$($dir.name)}}, Name, Length
		})| Export-Csv -Path .\$storageShareName.csv
    Write-host "Data exported to CSV!"
    Write-host ""
$repeat = Read-Host "Repeat?"
}
While ($repeat -eq "Y")
# Repeat Loop end
Write-Host ""
Write-Host "EXITING... " -ForegroundColor Yellow -BackgroundColor Black
Write-Host ""
# Disconnect
Disconnect-AzAccount > $null
Write-Host "ACCOUNT HAS BEEN DISCONNECTED" -ForegroundColor Yellow -BackgroundColor Black
# end

Searching Azure File Share to match string

It sucks. There's a search directory box in Explorer, so wtf Azure?! Can't search by anything except full directory name even with wildcards. Until now. Search for Directories and Files that match your string.

Note: You'll need Subscription ID, Resource Group Name, and Storage Account name of where you want to search.

# Set mandatory parameters
param 
(
    [Parameter(Mandatory=$true)]
    [string] $subscriptionId,
    [Parameter(Mandatory=$true)]
    [string] $resourceGroupName,
    [Parameter(Mandatory=$true)]
    [string] $storageAccountName
)
# Repeat Loop start
Do {
Connect-AzAccount
# Set Azure context
$context = Set-AzContext -SubscriptionId $subscriptionId
# Search string
$searchString = Read-Host -Prompt "Please enter string to search for"
# Get Storage Context
Write-Host "Searching Directories for $searchString in $storageAccountName :" -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
$key = Get-AzStorageAccountkey -ResourceGroupName $resourceGroupName -Name $storageAccountName
$storContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $key.value[0]
# Get Share Names
$shareNames = Get-AzRmStorageShare -ResourceGroupName $resourceGroupName -StorageAccountName $storageAccountName
foreach ($sName in $shareNames)
{
        $getAzSF = Get-AzStorageFile -ShareName $sName.name -context $storContext
# Iterate through directories, find Dir matches
        foreach ($dir in $getAzSF)
		{
			if ($dir.name -match $searchString)
			{
				Write-host "Path:  $($dir.name) " -ForegroundColor Yellow
		    }
        }
Write-Host "Searching Files for $searchString in $storageAccountName :" -ForegroundColor Green
Write-Host "====================================================" -ForegroundColor Green
# Iterate through directories, find File matches
foreach ($dir in $getAzSF)
		{
			$fileName = Get-AzStorageFile -ShareName $sName.name -context $storContext -Path $($dir.name) | Get-AzStorageFile | Select-Object -Property @{L='Directory';E={$($dir.name)}}, Name, Length
            if ($filename.name -match $searchString)
                {
                    Write-host "Path:  $($dir.name) " -ForegroundColor Yellow
                    Write-host "File Name: $($filename.name) " -ForegroundColor Yellow
                }
    }
$repeat = Read-Host "Repeat?"
}
}
While ($repeat -eq "Y")
# Repeat Loop end
Write-Host ""
Write-Host "EXITING... " -ForegroundColor Yellow -BackgroundColor Black
Write-Host ""
# Disconnect
Disconnect-AzAccount | Out-Null
Write-Host "ACCOUNT HAS BEEN DISCONNECTED" -ForegroundColor Yellow -BackgroundColor Black
# end