Sysinternals – Permissions, LoggedOn, Endpoints

How to Get the permission on folders:
PowerShell:
Get-ChildItem | Get-ACL
Path | Owner | Access

or more in depth use:

GUI based : Run AccessEnum against the drive or folder – (SysInternals tool) and save to text file (Run as administrator or a specific user)

Who is logged on via the resource shares:
Launch cmd and run PSLoggedon (SysInternals tool)
Displays :
1) Users logged on locally
2) Users logged on via resource shares

List TCP and UDP Endpoints connected
Run TCPView application (SysInternals tool) and save to text file

Ever need to identify the before and after changes in Active Directory
Use : ADExplorer (SystInternals tool)

Download Sysinternals 
https://docs.microsoft.com/en-gb/sysinternals/downloads/sysinternals-suite

Suggested top 10 sysinternals tools
https://www.techrepublic.com/blog/10-things/10-sysinternals-tools-you-shouldnt-be-without/
See an advert of interest, CLICK IT!  This site is funded by AD clicks.

Get-AdUser -Filter {Multiple Filters Complex } -Properties | Export to CSV

#Import AD modules

import-module servermanager
Add-WindowsFeature -Name “RSAT-AD-PowerShell” -IncludeAllSubFeature

#List AD user accounts and show DisplayName, Email, Title and export to CSV

Get-ADUser -Filter * -Properties DisplayName, EmailAddress, Title | select DisplayName, EmailAddress, Title | Export-CSV “C:\temp\Email_Addresses.csv”

#List AD user accounts and show DisplayName, Email, Title and export to CSV. Advanced filter to show ENABLED accounts only

Get-ADUser -Filter {Enabled -eq $true} -Properties DisplayName, SamAccountName, EmailAddress, Enabled, DistinguishedName | select DisplayName, SamAccountName, EmailAddress, Enabled, DistinguishedName | Export-CSV “C:\temp\Email_Addresses_allusers.csv”

#List AD user accounts and show DisplayName, Email, Title and export to CSV. Advanced filter to show ENABLED accounts only and email address ending @test.com

Get-ADUser -Filter {(Enabled -eq $true) -And (EmailAddress -Like “*@test.com”)} -Properties DisplayName, SamAccountName, EmailAddress, Enabled, DistinguishedName | select DisplayName, SamAccountName, EmailAddress, Enabled, DistinguishedName | Export-CSV “C:\temp\Email_Addresses_testdomain.csv”

Get a list of active computers which have logged on to the domain in the last 7 days

# Trying to work out is servers, laptops or desktops have been decommissioned
# Try this script
# Get a list of active computers which have logged on to the domain in the last 7 days

$Date = (Get-Date).AddDays(-7)
Get-ADComputer -Filter {LastLogonDate -gt $Date} | Select distinguishedName

# https://social.technet.microsoft.com/Forums/windows/en-US/4d412730-5937-48c2-bf17-0dc9db013241/list-active-computers-in-ad?forum=winserverDS
# Credit to Richard Mueller – MVP Enterprise Mobility (Directory Services)

Azure and Containers

What is a container?
A container is a live and running copy of an image which may have been customised.
An image is a read only copy of an image before it was running as a container

How do you implement containers in Azure

Two options, containers we deploy ourselves and containers Microsoft manage
Container can be running on Windows 2016 or Linux OS
CPU and Ram assigned to each individual container

Containers Limited security risk?
Microsoft offers Hyper-V running containers for those concerned
Azure container covers this way.
Others offer shared application containers.

Notes around Docker?
A docker file is like a script to build the container which takes a source and makes an app on an image, which makes a container as its running.

Docker has other tools: Docker toolbox, Docker client and Kitematic (GUI client)

How to Install Docker for Windows

https://docs.docker.com/docker-for-windows/install/

Quick install guide :
1) Navigate to https://docs.docker.com/docker-for-windows/install/#download-docker-for-windows
2) 
On the Install Docker for Windows page, click Get Docker for Windows (Stable).
3) When prompted whether to run or save Docker for Windows Installer.exe, click Run.
4) Once the installation completed, click Close and log out.
<https://github.com/MicrosoftLearning/20533-ImplementingMicrosoftAzureInfrastructureSolutions/blob/master/Instructions/20533D_LAB_AK_07.md>

Note
When you make a mistake deploying a docker-machine .. Ie.. Forget to enter a region… But the machine builds and you enter an error state.
Start again by removing the docker-machine

Launch CMD as admin : docker-machine rm “machine name”

 

Kubernetes
Kubernetes a management tools to for Docker. An alternative Docker Swarm for large scale
Deploy Kubernetes cluster for Linux containers

From <https://docs.microsoft.com/en-us/azure/container-service/kubernetes/container-service-kubernetes-walkthrough>

https://docs.microsoft.com/en-us/azure/aks/intro-kubernetes

DCOS getting started with Kubernetes

https://kubernetes.io/docs/getting-started-guides/dcos/

Set Up Your Microsoft Azure Environment With PowerShell

Step 1 : Install Command Line Tool For PowerShell

https://azure.microsoft.com/en-in/downloads/

Step 2: Launch PowerShell as Administrator

Type in the following

# get the Azure RM module installed first

Install-Module AzureRM

# import the module for use

Import-Module AzureRM

 

Step 3: Getting started with IaaS & PowerShell scripts

#Create a resource group

New-AzureRmResourceGroup -Name Project1ResourceGroup -Location “West Europe”

#Create a new subnet and store in a variable

$Project1Subnet1 = New-AzureRmVirtualNetworkSubnetConfig -Name Project1Subnet1 -AddressPrefix “10.0.1.0/24”

#Create new network and add the subnet stored in variable

$virtualNetwork = New-AzureRmVirtualNetwork -Name ProjectNetwork -ResourceGroupName Project1ResourceGroup -Location “West Europe” -AddressPrefix “10.0.0.0/16” -Subnet $Project1Subnet1

#add additional subnet to the network

Add-AzureRmVirtualNetworkSubnetConfig -Name Project2Subnet2 -VirtualNetwork $virtualNetwork -AddressPrefix “10.0.2.0/24”

$virtualNetwork | Set-AzureRmVirtualNetwork

 

GitHub

Sign up to GitHub.. Create your own repository https://github.com/

Git Hub Desktop to grab a bunch of files… Full Git hub desktop to sync https://desktop.github.com/

Add VM Custom Annotation and Create a Report on Annotations

Add Custom Attributes for Notes Annotation
………………

A request to add custom attributes for Virtual Machines when using the fat client. (Web client in 5.1 and 5.5 requires a plugin, see “vsphere-web-client-plugin-for-custom”) 6.0 doesn’t see the attributes in the Web Client, 6.5 does, see the 6.5 KB.

Fields required : Applications, Company Name, Owner, Role, VM Cost

Code Below

Connect-VIServer VC6.test.domain
New-CustomAttribute -Name “Company Name” -TargetType VirtualMachine
New-CustomAttribute -Name “VM Cost” -TargetType VirtualMachine
New-CustomAttribute -Name “Role” -TargetType VirtualMachine
New-CustomAttribute -Name “Owner” -TargetType VirtualMachine
New-CustomAttribute -Name “Applications” -TargetType VirtualMachine
disconnect-VIServer VC6.test.domain -Confirm:$false

………………..

Add the details required

………………..

Bulk Virtual Machines Deployment and Zero Clicks Part 1

Add additional code code to add annotation in to the bulk script

$companyname = $item.companyname
$applications = $item.applications
$owner = $item.owner
$role = $item.role
$cost = $item.cost

#Get the Specification and set the Nic Mapping
New-OSCustomizationNicMapping -Spec $custspec -IpMode UseStaticIp –Position 1 -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns

#Create VM using Template with the adjusted Customization Specification
New-VM -Name $vmname -Template $template -Datastore $datastore -VMHost $vmhost -ResourcePool $resourcepool | Set-VM -OSCustomizationSpec $custspec -Confirm:$false

#Set the Network Name
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $vlan -Confirm:$false

#Set the CPU and Memory
Get-VM -Name $vmname | Set-VM -MemoryGB $ram -NumCPU $cpu -Confirm:$false

#Set some custom attribute fieds
#New-CustomAttribute -Name “VM Cost” -TargetType VirtualMachine
#New-CustomAttribute -Name “Role” -TargetType VirtualMachine
#New-CustomAttribute -Name “Owner” -TargetType VirtualMachine
#New-CustomAttribute -Name “Applications” -TargetType VirtualMachine

#Set annotation value for custom attributes
Set-Annotation -Entity $vmname -CustomAttribute “CompanyName” -Value “$companyname”
Set-Annotation -Entity $vmname -CustomAttribute “Applications” -Value “$applications”
Set-Annotation -Entity $vmname -CustomAttribute “Owner” -Value “$owner”
Set-Annotation -Entity $vmname -CustomAttribute “Role” -Value “$role”
Set-Annotation -Entity $vmname -CustomAttribute “VM Cost” -Value “$cost”

 

Reports

RV Tools can be used to produce an MS Excel file to output a list of virtual machines and custom annotations RV Tools download

………………

Alternative Report function used
https://psvmware.wordpress.com/tag/get-vm-annotation/

Function Code Below
(greg-get-annotations tested successfully in our lab)……………….

function greg-get-annotations {
<# .DESCRIPTION Greg-get-annotations function stores information about annotation fields for vms in given cluster or in all clusters in VC. It stores the result in an arraylist $vms, you can either create a csv report from this object or display it on screen greg-get-annotations |export-csv -NoTypeInformation c:\file1.csv will export it to csv file etc… greg-get-annotations |format-table VMname,Cluster,CreatedOn,Notes will just display on screen a table with annotations that include : vm name, its cluster and field “CreatedOn” and Notes   .PARAMETER clustername Specifies the clustername against wchi report will be built   .EXAMPLE greg-get-annotations -clustername ‘cluster01’|Export-Csv c:\annotation-report.csv Will procude report on vms that resides in ‘cluster01’ and store it in csv file   .EXAMPLE greg-get-annotations -clustername ‘cluster01’|ft * Will procude report on vms that resides in ‘cluster01’ output it to screen   .EXAMPLE greg-get-annotations |Export-Csv c:\annotation-report.csv Will procude report on vms that resides in all clusters and output it to screen   .EXAMPLE greg-get-annotations Without specified -clustername switch, it will do report regarding all clusters in VC   .NOTES AUTHOR: Grzegorz Kulikowski LASTEDIT: 05/30/2011     #>
param ([string]$clustername)
if(!($clustername)){$clusters=Get-Cluster}else{$clusters=Get-Cluster $clustername}
$VMs=New-Object Collections.ArrayList
foreach ($cluster in $clusters)  {
foreach ($vmview in (get-view -ViewType VirtualMachine -SearchRoot $cluster.id)) {
$vm=New-Object PsObject
Add-Member -InputObject $vm -MemberType NoteProperty -Name VMname -Value $vmview.Name
Add-Member -InputObject $vm -MemberType NoteProperty -Name Notes -Value $vmview.Config.Annotation
Add-Member -InputObject $vm -MemberType NoteProperty -Name Cluster -Value $cluster.Name
foreach ($CustomAttribute in $vmview.AvailableField){
Add-Member -InputObject $vm -MemberType NoteProperty -Name $CustomAttribute.Name -Value ($vmview.Summary.CustomValue | ? {$_.Key -eq $CustomAttribute.Key}).value
}
$VMs.add($vm)|Out-Null
}
}
return $VMs
}

greg-get-annotations |Export-Csv c:\annotation-report.csv

……………………………………..

CSV Out Put

 

 

Install Azure PowerShell Modules

Just some quick notes and links for setting up Azure to be managed by PowerShell

https://docs.microsoft.com/en-us/powershell/azure/install-azurerm-ps?view=azurermps-4.0.0

Get-Module PowerShellGet -list | Select-Object Name,Version,Path

https://docs.microsoft.com/en-us/powershell/azure/install-azurerm-ps?view=azurermps-4.0.0#how-to-get-powershellget

# Install the Azure Resource Manager modules from the PowerShell Gallery
Install-Module AzureRM

Answer ‘Yes’ or ‘Yes to All’ to continue with the installation.

Import-Module AzureRM

Get-Module * -list

Bulk Virtual Machines Deployment and Zero Clicks Part 1

A recent project revisited deploying virtual machines via PowerCli. Its fair to say this isn’t a new tool but sometimes over looked.

Part 1 /  Part 2

My requirements were to deploy :

  • 100+ virtual machines (within a few hours)
  • domain join all machines
  • license the OS
  • various virtual machine specifications
  • various Windows OS versions.
  • to two different data centers within a linked vCenter setup
  • to resource pools
  • to different data stores
  • to different networks

 

The constraints:

  • vSphere 6.0 update 2
  • no budget for third party automation tools
  • small window of opportunity to deploy the VMs

 

On the plus side there was:

o    Loads of available CPU and RAM
o    Large datastores presented
o    Subnets prepared
o    Stretched VLANs across Data Centers

 

The tools I used to the task

  • Excel (CSV)
  • Notepad++
  • PowerCLi

 

The CSV file example

# Example Bulk_VMs_Deploy.csv

Template Datastore VMhost Custspec VMname IPaddress Subnet Gateway
2012_Template Storage1 ESXi.domain 2012_Spec test2003VL1 192.168.0.191 255.255.255.0 192.168.0.1
PDNS SDNS ResourcePool RAM CPU VLAN Size Format
192.168.0.10 127.0.0.1 resource1 2 2 VM Network 10 thin

 

The Script

# Automate the deployment of customised virtual machines deployed in vSphere 6.0. Tested against u2
#
# Prereq’s
# 1) Populate the a CSV file called Bulk_VMs_Deploy.csv
# 2) Create a Windows Server template
# 3) Create a customization spec within vSphere for Windows
# 4) Run Bulk_VMs_Deploy.ps1 script via PowerCli as administrator (CSV file must be stored in the same location where the script is run from)
#
#https://blogs.vmware.com/PowerCLI/2015/03/powercli-6-0-introducing-powercli-modules.html
if ( !(Get-Module -Name VMware.VimAutomation.Core -ErrorAction SilentlyContinue) ) {

###### IMPORTANT, Check this file path is correct##########
. “C:\Program Files (x86)\VMware\Infrastructure\PowerCLI\Scripts\Initialize-PowerCLIEnvironment.ps1”
}
Connect-VIServer VC6.test.domain
#connect to a VC. This also works with Linked VC’s
$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv
foreach ($item in $vmlist) {

#set variables to read from CSV
$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$sdns = $item.sdns
$resourcepool = $item.resourcepool
$cpu = $item.cpu
$ram = $item.ram
$vlan = $item.vlan
$size = $item.size
$format = $item.format

#Get the Specification and set the Nic Mapping
New-OSCustomizationNicMapping -Spec $custspec -IpMode UseStaticIp –Position 1 -IpAddress $ipaddr -SubnetMask $subnet -DefaultGateway $gateway -Dns $pdns,$sdns

#Create VM using Template with the adjusted Customization Specification
New-VM -Name $vmname -Template $template -Datastore $datastore -VMHost $vmhost -ResourcePool $resourcepool | Set-VM -OSCustomizationSpec $custspec -Confirm:$false

#Set the Network Name
Get-VM -Name $vmname | Get-NetworkAdapter | Set-NetworkAdapter -NetworkName $vlan -Confirm:$false

#Set the CPU and Memory
Get-VM -Name $vmname | Set-VM -MemoryGB $ram -NumCPU $cpu -Confirm:$false

#Additional Disk
#Get-VM -Name $vmname | New-HardDisk -CapacityGB $size -StorageFormat $format -Confirm:$false

#Remove the NicMapping
Get-OSCustomizationSpec $custspec | Get-OSCustomizationNicMapping | Remove-OSCustomizationNicMapping -Confirm:$false

#PowerOn VM
Start-VM $vmname

}
#Disconnect from VC.
disconnect-VIServer VC6.test.domain -Confirm:$false

 

 

Disclaimer Please take the code and evolve it into a different project? Credit / Tag me on your project Twitter #StephenHackers

Any use of this code is at your own risk. Remember bulk automation jobs require the right resources to be available.

This project & code was based on :
https://communities.vmware.com/thread/315193
Which progressed to : https://communities.vmware.com/thread/436734

Part 1 /  Part 2

Bulk Virtual Machines Deployment and Zero Clicks Part 2

Based on the CSV file used to create the VMs, re-use the CSV to control
Part 1 / Part 2

What else can I do now?

  • Delete Computer Objects from Active Directory
  • Bulk guest shutdown
  • Bulk power on virtual machines
  • Bulk power off virtual machines
  • Bulk Delete Virtual Machines from disk
  • Bulk change Computer Object OU

 

  • Delete Computer Objects from Active Directory

# Delete Computer Objects from Active Directory

$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv

foreach ($item in $vmlist) {

$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$resourcepool = $item.resourcepool

Remove-ADComputer -Identity $vmname -Confirm:$false

}

 

  • Bulk guest shutdown

# Guest power down is a gracefull shutdown of the VMs
# VM requires VMware Tools to be installed on the VMs

Connect-VIServer vc6.test.domain

$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv

foreach ($item in $vmlist) {

$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$resourcepool = $item.resourcepool

#Guest Shutdown VM
Shutdown-VMGuest $vmname
}
disconnect-VIServer vc6.test.domain -Confirm:$false

 

  • Bulk power on virtual machines

# Bulk power on virtual machines

Connect-VIServer vc6.test.domain

$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv

foreach ($item in $vmlist) {

$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$resourcepool = $item.resourcepool

# POWER ON vms
Start-VM $vmname
}
disconnect-VIServer vc6.test.domain

 

  • Bulk power off virtual machines

# Bulk Power OFF VMs (Big Button OFF the VMs)
Connect-VIServer vc6.test.domain

$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv

foreach ($item in $vmlist) {

$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$resourcepool = $item.resourcepool

#PowerOFF VM (Big Button OFF the VM)
Stop-VM $vmname -Confirm:$false

}

Disconnect-VIServer vc6.test.domain -Confirm:$false

 

  • Bulk Delete Virtual Machines from disk

# Delete Virtual Machines from disk
# VM should be already powered off

Connect-VIServer vc6.test.domain

$vmlist = Import-CSV .\Bulk_VMs_Deploy.csv

foreach ($item in $vmlist) {

$template = $item.template
$datastore = $item.datastore
$vmhost = $item.vmhost
$custspec = $item.custspec
$vmname = $item.vmname
$ipaddr = $item.ipaddress
$subnet = $item.subnet
$gateway = $item.gateway
$pdns = $item.pdns
$resourcepool = $item.resourcepool

Remove-VM -VM $vmname -DeleteFromDisk -Confirm:$false
}
disconnect-VIServer vc6.test.domain -Confirm:$false

 

  • Bulk change Computer Object OU#Move and AD Computer Object to a specific OU from a CSV file
    $vmlist = Import-CSV .\Bulk_VMs_Deploy.csvforeach ($item in $vmlist) {$template = $item.template
    $datastore = $item.datastore
    $vmhost = $item.vmhost
    $custspec = $item.custspec
    $vmname = $item.vmname
    $ipaddr = $item.ipaddress
    $subnet = $item.subnet
    $gateway = $item.gateway
    $pdns = $item.pdns
    $resourcepool = $item.resourcepoolGet-ADComputer $vmname|Move-ADObject -TargetPath “OU=VM,DC=TEST,DC=DOMAIN”
    }

 

Disclaimer Please take the code and evolve it into a different project? Credit / Tag me on your project Twitter #StephenHackers

Any use of this code is at your own risk. Remember bulk automation jobs require the right resources to be available.

Part 1 / Part 2

Check if an AD user has a roaming profile configured (PowerShell) – OneScript Team

Check if an AD user has a roaming profile configured (PowerShell)

This script can query specified active directory users whether or not a roaming profile was configured.

Scenarios

IT admins may care about which users has a roaming profile was configured. This script can help IT admins check whether or not a roaming profile was configured.

Script

Step 1: Run the script in the Windows PowerShell Console, type the command: Import-Module <Script Path> at the prompt.  For example, type Import-Module C:\Script\CheckIfProfileExists.psm1

This is shown in the following figure.

Step 2: Type the command Get-Help Get-OSCADUserRoamingProfile -Full to display the entire help file for this function, such as the syntax, parameters, or examples.

OneScript Team