понедельник, 14 марта 2022 г.

Removing Shadow VMs on vCloud

Removing Shadow VMs


##==  ==##

# Removing Shadow VMs on a specific Datastore

# ToDo - Choose Datastore with OGV !


$ErrorActionPreference = "SilentlyContinue" 

$WarningPreference = "SilentlyContinue" 


cls

$User = $env:UserName

$SecurePass = Read-Host -AsSecureString "Please enter password for '$User' and wait..."

$Pass = [System.Runtime.InteropServices.Marshal]::PtrToStringAuto([System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($SecurePass))

If(-not($Pass)){Write-Host "You must enter a password!" -ForegroundColor Yellow -BackgroundColor DarkRed; Write-Host; Break}

$vCloud = "vCloud"

$vCenter = "VCRD"


##== Connecting to vCloud ==##

Disconnect-CIServer * -Confirm:$false 

$CIServerConnected = ""

$CIServerConnected = Connect-CIServer -Server $vCloud -User $User -Password $Pass # -Org $Org | Out-Null

If(-not($CIServerConnected)){Write-Host "$vCloud is NOT Connected! `nPlease check your password, locked user account or permissions!" -BackgroundColor DarkRed  -ForegroundColor Yellow; Write-Host; Break}

Else{Write-Host "$vCloud is Connected!" -ForegroundColor Yellow -BackgroundColor DarkRed}

Write-Host 


##== Connecting to vCenter ==##

Disconnect-VIServer * -Confirm:$false 

$VIServerConnected = ""

$VIServerConnected = Connect-VIServer -Server $vCenter -User $User -Password $Pass

If(-not($VIServerConnected)){Write-Host "$vCenter is NOT Connected!" -BackgroundColor DarkRed -ForegroundColor Yellow}

Else{Write-Host "vCenter is Connected!" -ForegroundColor Yellow -BackgroundColor DarkRed}

Write-Host 


##== Getting info for four NF400N Datastores from vCenter ==##

$AllDatastoreS = Get-Datastore

$ChosenDatastoreS = $AllDatastoreS | Where{$_.Name -like "Datastore_NF400N_*"} | sort -Property FreeSpaceGB | select -First 5

Write-Host "Five Datastores with the least free space" -BackgroundColor DarkGreen

$ChosenDatastoreS

Write-Host 


##== Searching for Shadow VMs on NF400N Datastores ==##

$ShadowVMs400 = Search-Cloud AdminShadowVM -Filter DatastoreName==Datastore_NF400N_*

$ShadowVMs400Count = $ShadowVMs400.Count

Write-Host "There are $ShadowVMs400Count ShadowVMs in NF400N Cluster" -BackgroundColor DarkGreen


##== Counting Shadow VMs by Datastore ==##

foreach ($Datastore in $ChosenDatastoreS)

    {

    $ShadowVMsDatastore = $ShadowVMs400 | where{$_.DatastoreName -like $Datastore}

    $VMsCount = $ShadowVMsDatastore.Count

    If($VMsCount){Write-Host "$Datastore = $VMsCount VMs" -BackgroundColor Green}

    }

Write-Host 


##== Choosing specific Datastore and creating filter sentence for searching Shadow VMs on this Datastore ==##

$DatastoreNumber = ""

[Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic') > $NULL

$DatastoreNumber = [Microsoft.VisualBasic.Interaction]::InputBox("Choose Datastore number for deleting ShadowVMs (or press Enter for exit): ", "Datastore Number")


###=CHOOSE DATASTORE OR EXIT FROM THE SCRIPT IF A NUMBER IS NOT CHOOSEN=###

Write-Host 

$ChosenDatastore = -join("Datastore_NF400N_"+$DatastoreNumber)

$FilterDatastore = "DatastoreName==$ChosenDatastore"

Write-Host 


##== Exit from script by Datastore ==##

If(!$DatastoreNumber){Write-Host "NO Datastore is choosen.`n`nSCRIPT IS FINISHED!" -ForegroundColor Yellow -BackgroundColor DarkCyan; break}

Else{Write-Host "You selected '$ChosenDatastore'" -BackgroundColor Green}


##== Searching Shadow VMs on the specific Datastore ==##

$ShadowVMsOnDatastore = Search-Cloud AdminShadowVM -Filter $FilterDatastore

### $ShadowVMsOnDatastoreCount = $ShadowVMsOnDatastore.name.Count

$ShadowVMsOnDatastoreCount = $ShadowVMsOnDatastore.Count

##== Exit from script if NO ShadowVMs is found/defined on the selected Datastore ==##

If(!$ShadowVMsOnDatastoreCount){Write-Host "Wrong choice, there are NO Shadow VMs on '$ChosenDatastore'. I am exiting!" -BackgroundColor DarkGreen; Break}

Write-Host "There are $ShadowVMsOnDatastoreCount Shadow VMs on '$ChosenDatastore'. Select some or all to kill..." -BackgroundColor DarkGreen

Write-Host 


$VMsNamesBefore = $ShadowVMsOnDatastore.Name

$ShadowVMsDelete = $ShadowVMsOnDatastore | OGV -Title "Shadow VMs On '$ChosenDatastore'" -PassThru

$ShadowVMsNames = ($ShadowVMsDelete).Name

If(!$ShadowVMsNames){Write-Host "Seems you are afraid to kill the Shadow VMs :) `nYou should know it is your choice. I am exiting!" -BackgroundColor DarkGreen; Break}

Write-Host "Shadow VMs selected for deleting:" -BackgroundColor Green

$ShadowVMsNames

Write-Host 


##== Deleting Shadow VMs on the selected Datastore ==##

foreach ($shadowVM in $ShadowVMsDelete)

    {

    $shadowVMName = $shadowVM.Name

    Write-Host "Deleting '$shadowVMName'" -BackgroundColor DarkBlue -ForegroundColor Magenta

    $ShadowDelete = $shadowVM | Get-CIView

    $ShadowDelete.delete()

    }


Write-Host 

$ShadowVMsOnDatastore = Search-Cloud -QueryType AdminShadowVM

$ShadowVMsOnDatastore = $ShadowVMsOnDatastore | where {$_.DatastoreName -like $ChosenDatastore}

$VMsCountAfter = $ShadowVMsOnDatastore.Count

$VMsDifference = $ShadowVMsOnDatastoreCount - $VMsCountAfter


Write-Host $ChosenDatastore -BackgroundColor Black

Write-Host "From $ShadowVMsOnDatastoreCount Shadow VMs you choosed $VMsDifference to delete." -BackgroundColor DarkRed

Write-Host "After deleting there are $VMsCountAfter Shadow VMs left." -BackgroundColor DarkRed

Write-Host 

Write-Host "GREAT JOB '$env:UserName' !" -ForegroundColor Yellow -BackgroundColor DarkRed

Write-Host 


# del $env:TEMP\ShadowDeletedLog.csv

# $VMsDifference | Export-Csv $env:TEMP\ShadowDeletedLog.csv -NoTypeInformation -UseCulture

# ii $env:TEMP\ShadowDeletedLog.csv

##==  ==##




пятница, 11 марта 2022 г.

Unlock AD user with PowerShell

First you need to be Domain Administrator.

To find all locked accounts in Active Directory you may use this simple command:

Search-ADAccount -LockedOut 

To unlock specific user use this command:

Unlock-ADAccount "Username"

To unlock all found accounts you can pipe results of the command to another command:

Search-ADAccount -LockedOut | Unlock-ADAccount

If you want to unlock several users (not only one and not all of them) you may use Out-GridView command which gives the possibility for some sort of GUI menu.

Search-ADAccount -LockedOut | OGV -Title "Choose the accounts for unlocking" -PassThru | Unlock-ADAccount

Put attention to -PassThru  - it gives the possibility to choose one or more objects and then piping them to the next command which is in our case unlocks the chosen users.

In the most of cases these simple commands could be enough, but if you want to be cool you may do some scripting.

##== START ==##

cls

$LockedAcc = ""

$LockedAccCount = ""

$User = $env:UserName


Write-Host "'$User', I am seeking for Locked Accounts....." -BackgroundColor Green

$LockedAcc = Search-ADAccount -LockedOut

$LockedAcc | Select Name, SamAccountName, LockedOut

$LockedAccCount = ($LockedAcc | measure).Count


Write-Host

If(!($LockedAcc)){Write-Host "Great! I cannot find any!" -ForegroundColor Yellow -BackgroundColor DarkRed; Write-Host; Break}

Else{Write-Host "Found $LockedAccCount Locked Account(s)" -BackgroundColor Red}

$LockedAcc | OGV -PassThru | Unlock-ADAccount


##== CHECK ==##

Write-Host "Post-check for Locked Accounts..."

Start-Sleep 5


$LockedAcc2 = ""

$LockedAcc2Count = ""


$LockedAcc2 = Search-ADAccount -LockedOut

$LockedAcc2Count = ($LockedAcc2 | measure).Count


If($LockedAcc2){Write-Host "Still $LockedAcc2Count Account(s) is/are Locked" -BackgroundColor Red}

Else{Write-Host "Great! You successfully unlocked accounts." -BackgroundColor Red}

$LockedAcc2 | Select Name, SamAccountName, LockedOut


##== END ==##




суббота, 5 марта 2022 г.

Useful Links

 How to Allow Multiple RDP Sessions in Windows 10 and 11?

http://woshub.com/how-to-allow-multiple-rdp-sessions-in-windows-10/

Install RSAT on Windows 10 with PowerShell

PowerShell

Get-WindowsCapability -Name RSAT* -Online | Add-WindowsCapability -Online