Posts

Showing posts with the label Powershell

Create multiple Azure files shares using Powershell

If you have to create multiple shares for an Azure files instance as part of a migration or new deployment it can be very time consuming. With this script you put the name of the shares in a txt file called "shares.txt" and it will loop through and create them all and assign them to the hot tier. <# .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION This script creates shares from a txt file in Azure and set's their access tier .EXAMPLE .OUTPUTS .NOTES #> # CONFIG START $file = Get-Content -Path "shares.txt" $accessTier = "Hot" $tenant = "<tenant id>" $subscription = "<subscription id>" $resourceGroupName = "<resource group>" $storageAccountName = "<storage account name>" # CONFIG END # CONNECT TO AZURE Connect-AzAccount -Tenant $tenant -SubscriptionId $subscription # GET STORAGE KEY $storageKey = (Get-AzStorageAccountKey ` -ResourceGroupName $resour...

Apply tags to azure resource groups from csv file

This script is used to apply tags as per a csv file. Tags will be applied in uppercase. If a resource group is present in the csv but not in Azure it will be skipped. A log file will be generated in the log folder. A backup of the resource groups previous tags and values will be created in the backup folder. CSV should be formatted as the Example file and saved as tags.csv in c:\temp\tags unless you specify differently under the config section An example csv file can be downloaded here <# .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION This script is used to apply tags as per a csv file. Tags will be applied in uppercase. If a resource group is present in the csv but not in Azure it will be skipped. A log file will be generated in the log folder. A backup of the resource groups previous tags and values will be created in the backup folder. .EXAMPLE CSV should be formatted as below or see the example file and saved as tags.csv in c:\temp\tags unless you specify d...

List object contributors in all Azure subscriptions with Powershell

Image
The below script will loop through all Azure subscriptions excluding Visual Studio subscriptions, it will then look at every object and list/export everyone who has a role that has the word "Contributor" in it. Example below: <### .Synopsis Created by James Lambert www.roonics.com .DESCRIPTION This script will connect to Azure and cycle through all subscriptions listing all the objects and who has a role which has "Contributor" in the name .EXAMPLE Run the script and sign in to Azure .OUTPUTS A file csv file will be created for each subscription named "contribuators_subscription.csv" in c:\temp A total number of contributors will be added to the bottom of the csv .NOTES Keep in mind this will only be able to look at subscriptions you have permissions to. This will also skip the Visual studio subscription using a if the name like 'visual' statement ###> ### Config and clear screen cls $Path = "C:\Temp\" $f...

List printers and IP addresses on a windows server using Powershell

Image
  <### .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION Collect port names and host addresses into hash table .EXAMPLE .OUTPUTS .NOTES ###> $hostAddresses = @{} Get-WmiObject Win32_TCPIPPrinterPort | ForEach-Object { $hostAddresses.Add($_.Name, $_.HostAddress) } Get-WmiObject Win32_Printer | ForEach-Object { New-Object PSObject -Property @{ "Name" = $_.Name "DriverName" = $_.DriverName "Status" = $_.Status "HostAddress" = $hostAddresses[$_.PortName] } }

Stop and disable a Windows service using powershell

Populate "computers.txt" with all the server names you wish this to run on then adjust the script to fit the service you require. <### .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION Stop and disable a Windows service using powershell Populate "computers.txt" with all the server names you wish this to run on then adjust the script to fit the service you require. .EXAMPLE .OUTPUTS .NOTES ###> $SrvNames = Get-Content -Path 'C:\data\scripts\computers.txt' foreach ($Server in $SrvNames) { Get-Service -Name "Rubrik Backup Service" -ComputerName $Server | Stop-Service -PassThru | Set-Service -StartupType disabled }

Split file every xxx lines with Powershell

Image
I recently ran in to an issue whilst using CommVault to restore some archive files from it's HSM. You have to run the gxhsmutility to scan the files to check if any stub files remain, if there is it outputs what is called a map file, you then run the restore again pointing to said map file. The problem with this was that if the map file was over 100,000 lines the recovery would just crash, so I wrote this to split the map file out in to multiple map files I could then use to recover the data. <### .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION This script will get a list of all files in the "Files_to_split folder" then loop through each file and split every XXX numbers of lines (set in the config) .EXAMPLE .OUTPUTS .NOTES ###> ### Config $FilesToSplitDir = "Files_to_split\" $SplitFilesDir = "Split_files\" $SplitFileLines = 100000 $padding = " " cls ### Check if source folder is present if (!(Test-Path ...

Add random number prefix to filename with Powershell

This allows you to add a randomly generated number to the prefix of a bunch of files. In the below example we are adding a random prefix to every *.jpg file. <# .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION This allows you to add a randomly generated number to the prefix of a bunch of files. In the below example we are adding a random prefix to every *.jpg file. .EXAMPLE .OUTPUTS .NOTES #> $files = Get-ChildItem "C:\temp\files" -Filter *.jpg foreach ($file in $files) { $prefix = Get-Random $newFileName = "${prefix}_${file}" Rename-Item $file $newFileName }

Create html galleries of images & videos using Powershell

Image
 This script is designed to be placed in a folder with lots of images and videos including subfolders, create thumbnails of all the images found and uses ffmpeg to create frame sheets for movies then add 100 per HTML page with a "Next" link at the bottom linking to the next gallery page. Make sure to adjust the settings in the config section to suit your needs. Prerequisites Download ffmpeg from https://ffmpeg.org/download.html#build-windows , unzip and copy the contents to c:\ffmpeg Add the ffmpeg folder as a path, as per below. Download and install ImageMagick and ensure the "add to systm path" is ticked during the installation. https://imagemagick.org/index.php Place your images, videos in "Files" directory ffmpeg seems to have issues with certain special characters so ensure there aren't any in the video filenames See screenshots for examples. Script <# .SYNOPSIS Created by James Lambert www.roonics.com .DESCRIPTION Place all media files in ei...

Check if multiple nics are set to register with DNS

  This checks all the nics on a server to see if multiple nics have the "Register this in DNS" which if multiple are selected it can cause connectivity issues. 1. Export a list of the servers you wish to check and save it as a txt file in the same location as the script called "computers.txt" as below: server01 server02 server03 server04 2. Copy and paste the below script in to Notepad and save it as a ps1 file: cls $servers = Get-Content computers.txt foreach($server in $servers) { $nics = Get-WmiObject Win32_NetworkAdapterConfiguration -ComputerName $server -ErrorAction Inquire | Where{$_.IPEnabled -eq "TRUE"} foreach($nic in $nics) { Write-Host $server "-" $nic.IPAddress "-" $nic.FullDNSRegistrationEnabled } } 3. Now run the script. Example output: server01 - 10.10.10.10 - True server01 - 192.168.0.1 - False server02 - 10.10.10.20 - True server03 - 10.16.10.30 - True

Powershell Unable to find module repositories

  When running: Get-PSRepository in Powershell and you get the error: WARNING: Unable to find module repositories. This is normally because you are behind a poxy and you need to set the proxy settings for Powershell, to do this follow these steps: Run notepad $PROFILE Notepad will open and probably tell you the file is not present and would you like to create it, select yes and enter the following in the file, obviously changing the proxy and port to your proxy and port: [system.net.webrequest]::defaultwebproxy = new-object system.net.webproxy('http://YOURPROXY:8080') [system.net.webrequest]::defaultwebproxy.credentials = [System.Net.CredentialCache]::DefaultNetworkCredentials [system.net.webrequest]::defaultwebproxy.BypassProxyOnLocal = $true Save and exit the file Close and re open Powershell Run Register-PSRepository -Default This should now allow you to register the repos.